PHP utility collection with hybrid and fluent APIs.

⌈⌋ branch:  hybrid7 libraries


Check-in [0627bb6e05]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
SHA1 Hash:0627bb6e0562e0bb45c59df1b2469b9676c86c82
Date: 2015-02-16 20:14:30
User: mario
Comment:Prepare macro preprocessing build.
Tags And Properties
  • branch=trunk inherited from [da152c56bc]
  • sym-trunk inherited from [da152c56bc]
Context
2015-02-16
20:15
[037bab0b64] Add ArrayObject compatiblity (arrayExchange, getArrayCopy). (user: mario, tags: trunk)
20:14
[0627bb6e05] Prepare macro preprocessing build. (user: mario, tags: trunk)
2015-01-13
18:50
[5cd584fcc8] Separate ł::$app to be static, but ł()->section= and $min_prio to be logger-group specific property. Reorder class defaults, compact introduction text. Fix error handler context usage as :vars, apply :backtrace manually. (user: mario, tags: trunk)
Changes

Changes to logstruck.php.

6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
...
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
...
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
...
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
...
508
509
510
511
512
513
514
515
516
517
518
519
520







521
522
523
524
525
526
527
 * api: php
 * type: handler
 * category: logging
 * state: experimental
 * depends: php:sqlite >= 3.7, php >= 5.4
 * autoexec: true
 * config:
 *   { type: var, name: "ł::$db", description: "SQLite storage location" }
 *   { type: var, name: "ł::$app", description: "default application name" }
 *   { type: var, name: "ł::$section", description: "current application module/section" }
 * license: Public Domain
 * doc: https://fossil.include-once.org/hybrid7/wiki/log
 * 
 * 
 * Implements a terse but parametric logging API for structured journaling.
 * Primarily meant for userland and application-level inspection.
 * 
................................................................................
     * @return int     log-event id
     */
    public function __invoke($vars) {

        // from defaults
        $event = array_merge($this->template, [
            "timestamp" => microtime(TRUE),
            "section" => self::$section,
            "p" => $this->p, // parent
            "g" => $this->g, // group
        ]);

        // all this just to turn token/array/string list into structurized blob
        $this->map_params($event, array_merge($this->iparams, $vars));

................................................................................
    public function move_context_params(& $event) {
        $extra = array_diff_key($event, array_flip(self::$fields));
        $event["context"] = array_merge_recursive($event["context"], $extra);
        $event = array_diff_key($event, $extra);
    }
    


    /**
     * Database pushing of event data.
     * ‾‾‾‾‾‾‾‾
     * This SQLite implementation is rather slow. It reopens and closes the handle
     * to prevent lockups. Ideally this should be switched out for fluentd/rsyslog
     * interfacing. → But this won't be made configurable. Instead brach versions
     * of this script will implement different targets. (A plain JSON dump might be
................................................................................
    public function store($data) {

        // JSONify arrays
        $data = array_map("self::_json", $data);
        $data = array_intersect_key($data, array_flip(array_diff(self::$fields, ["i"])));
        
        // create handle, connect to SQLite file
        $db = new PDO("sqlite::memory:", NULL, NULL, [PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING]);
        $db->query("ATTACH '{$this->db}' AS `logdb`");
        $db->beginTransaction();
        
        // bind values
        $keys = array_keys($data);
        $s = $db->prepare(
            "INSERT OR ROLLBACK INTO log " .
                   "(`" . join("`, `", $keys) . "`) " .     // :?
................................................................................
            $db->commit();
            $p = $db->lastInsertId();
        }
        else {
            $p = $this->fallback_stderr($data);
        }

        // close store
        $db->query("DETACH `logdb`");
        $db = NULL;
        return $p;
    }










    /**
     * Set $template values through `ł()->section = "xyz"`
     *
     * @param  string   Field name
     * @param  mixed    Default value







|
|
<







 







<







 







|







 







|
<







 







|
<
<


<
>
>
>
>
>
>
>







6
7
8
9
10
11
12
13
14

15
16
17
18
19
20
21
...
312
313
314
315
316
317
318

319
320
321
322
323
324
325
...
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
...
485
486
487
488
489
490
491
492

493
494
495
496
497
498
499
...
505
506
507
508
509
510
511
512


513
514

515
516
517
518
519
520
521
522
523
524
525
526
527
528
 * api: php
 * type: handler
 * category: logging
 * state: experimental
 * depends: php:sqlite >= 3.7, php >= 5.4
 * autoexec: true
 * config:
 *   { class: var, type: str, name: "ł::$app", description: "default application name" }
 *   { class: prop, type: str, name: "ł()->db", description: "SQLite storage location" }

 * license: Public Domain
 * doc: https://fossil.include-once.org/hybrid7/wiki/log
 * 
 * 
 * Implements a terse but parametric logging API for structured journaling.
 * Primarily meant for userland and application-level inspection.
 * 
................................................................................
     * @return int     log-event id
     */
    public function __invoke($vars) {

        // from defaults
        $event = array_merge($this->template, [
            "timestamp" => microtime(TRUE),

            "p" => $this->p, // parent
            "g" => $this->g, // group
        ]);

        // all this just to turn token/array/string list into structurized blob
        $this->map_params($event, array_merge($this->iparams, $vars));

................................................................................
    public function move_context_params(& $event) {
        $extra = array_diff_key($event, array_flip(self::$fields));
        $event["context"] = array_merge_recursive($event["context"], $extra);
        $event = array_diff_key($event, $extra);
    }
    

#ifdef LOGBE_SQLITE
    /**
     * Database pushing of event data.
     * ‾‾‾‾‾‾‾‾
     * This SQLite implementation is rather slow. It reopens and closes the handle
     * to prevent lockups. Ideally this should be switched out for fluentd/rsyslog
     * interfacing. → But this won't be made configurable. Instead brach versions
     * of this script will implement different targets. (A plain JSON dump might be
................................................................................
    public function store($data) {

        // JSONify arrays
        $data = array_map("self::_json", $data);
        $data = array_intersect_key($data, array_flip(array_diff(self::$fields, ["i"])));
        
        // create handle, connect to SQLite file
        static $db = new PDO("sqlite:{$this->db}", NULL, NULL, [PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING]);

        $db->beginTransaction();
        
        // bind values
        $keys = array_keys($data);
        $s = $db->prepare(
            "INSERT OR ROLLBACK INTO log " .
                   "(`" . join("`, `", $keys) . "`) " .     // :?
................................................................................
            $db->commit();
            $p = $db->lastInsertId();
        }
        else {
            $p = $this->fallback_stderr($data);
        }

        // return new event id


        return $p;
    }

#elif defined(LOGBE_JSON)
#include <_store_json.inc>
#elif defined(LOGBE_STASH)
#include <_store_logstash.inc>
#elif defined(LOGBE_CEE)
#include <_store_rsyslog.inc>
#endif


    /**
     * Set $template values through `ł()->section = "xyz"`
     *
     * @param  string   Field name
     * @param  mixed    Default value