PHP code example of petert82 / monolog-logfmt

1. Go to this page and download the library: Download petert82/monolog-logfmt library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

petert82 / monolog-logfmt example snippets


use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Petert82\Monolog\Formatter\LogfmtFormatter;

$log = new Logger('name');
$handler = new StreamHandler('php://stdout', Logger::WARNING);
$handler->setFormatter(new LogfmtFormatter());
$log->pushHandler($handler);

$log->addError('Danger! High voltage!', ['voltage' => 9000]);

$tsKey = 'date';
$levelKey = 'level';
$channelKey = 'channel';
$msgKey = 'message';
$dateFormat = 'Ymd-His';
$formatter = new LogfmtFormatter($tsKey, $levelKey, $channelKey, $msgKey, $dateFormat);

$formatter = new LogfmtFormatter('', '', '', 'msg');

// Default JSON serialization
$formatter = new LogfmtFormatter();
// Produces output like: user={"name":"John","roles":["admin","editor"]}

// With flattening enabled
$formatter = new LogfmtFormatter(
    'ts', 'lvl', 'chan', 'msg',
    DateTime::RFC3339, "\n",
    true // Enable flattening
);
// Outfut now looks like: user_name=John user_roles_0=admin user_roles_1=editor

ts=2017-11-21T20:02:10+00:00 lvl=ERROR chan=name msg="Danger! High voltage!" voltage=9000