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
Comment:Prepare macro preprocessing build.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 0627bb6e0562e0bb45c59df1b2469b9676c86c82
User & Date: mario 2015-02-16 20:14:30
Context
2015-02-16
20:15
Add ArrayObject compatiblity (arrayExchange, getArrayCopy). check-in: 037bab0b64 user: mario tags: trunk
20:14
Prepare macro preprocessing build. check-in: 0627bb6e05 user: mario tags: trunk
2015-01-13
18:50
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. check-in: 5cd584fcc8 user: mario tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to logstruck.php.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
/**
 * title: logStruck
 * description: SQlite group/structured logging backend
 * version: 0.1.1
 * 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.
 * 












<
|
|







1
2
3
4
5
6
7
8
9
10
11
12

13
14
15
16
17
18
19
20
21
<?php
/**
 * title: logStruck
 * description: SQlite group/structured logging backend
 * version: 0.1.1
 * 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.
 * 
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
     * @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));








<







312
313
314
315
316
317
318

319
320
321
322
323
324
325
     * @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));

467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520






521
522
523
524
525
526
527
    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
     * workable, but requires post-processing to reconstruct the event hierarchy.)
     *
     * @param  array   log event data
     * @return int     event id
     */
    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) . "`) " .     // :?
            "VALUES (:" . join(", :",  $keys) . ") "        // ::
        );
        
        // store and commit
        if ($s and $s->execute($data)) {
            $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







|



















|
<



















|
<
<


|
>
>
>
>
>
>







465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492

493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512


513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
    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
     * workable, but requires post-processing to reconstruct the event hierarchy.)
     *
     * @param  array   log event data
     * @return int     event id
     */
    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) . "`) " .     // :?
            "VALUES (:" . join(", :",  $keys) . ") "        // ::
        );
        
        // store and commit
        if ($s and $s->execute($data)) {
            $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