PHP utility collection with hybrid and fluent APIs.

⌈⌋ ⎇ branch:  hybrid7 libraries


Artifact [9f35ca3508]

Artifact 9f35ca3508906b38be9cff495704d8d3bfbcacbb:

Attachment "_logs_store_json.php" to wiki page [log] added by mario 2015-01-10 16:12:58.
<?php
# type: partial



    /**
     * JSON store
     * ‾‾‾‾‾‾‾‾‾‾
     * Instead of pushing log events in a proper store, here they're just appended
     * line-wise as JSON blobs.
     *
     * Event relationships are stored with relative IDs. That is a local counter
     * starts at 1 for each process. Therefore the resulting json-lines-dump will
     * contain multiple event groups, each starting with `i=1,g=1,p=0`.
     * That's still suitable to be merged (via group-wise transactions basically)
     * into a proper datastore.
     *
     * @param  array   log event data
     * @return int     event id
     */
    public function store($data) {
    
        // local event id counter
        static $i = 1;

        // `p` parent id is usually injected by event collector __invoke()
        if (empty($data["p"])) { $data["p"] = 0; }
        // `g` is always 1, when just one event group is used per process
        $data["g"] = 1;
        $data["i"] = $i;

        // trivial storing, JSON without literal newlines
        file_put_contents($this->db, json_encode($data, JSON_UNESCAPED_SLASHES) . "\n", FILE_APPEND | LOCK_EX);
        
        // return 
        return $i++;
    }