PHP code example of noxdk / monolog-journal-handler

1. Go to this page and download the library: Download noxdk/monolog-journal-handler 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/ */

    

noxdk / monolog-journal-handler example snippets



Monolog\Logger;
use Noxdk\MonologJournalHandler\JournalHandler;

$log = new Logger('MyLogger', [new JournalHandler()]);

$log->info('This is logged to the journal');

$log->info("This is logged to the journal\nand contains multiple\nlines");

$log->info('This is logged to the journal with custom tag', [
    'MY_TAG' => 'MyTag',
    'ANOTHER_TAG' => "With\nnewlines"
]);

$log->error('Some error occurred', [new Exception('Bad something')]);
json
{
    ...
    "SYSLOG_IDENTIFIER": "MyLogger",
    "_HOSTNAME": "host",
    "_UID": "1000",
    "MY_TAG": "MyTag",
    "ANOTHER_TAG": "With\nnewlines",
    "MESSAGE": "INFO: This is logged to the journal with custom tag {\"MY_TAG\":\"MyTag\",\"ANOTHER_TAG\":\"With\nnewlines\"} ",
    "PRIORITY": "6",
    "_CMDLINE": "php test.php",
    ...
}

nov 20 10:08:14 host MyLogger[1]: ERROR: Some error occurred ["[object] (Exception(code: 0): Bad something at /test.php:18)
                                  [stacktrace]
                                  #0 /test.php(13): TestClass->update()
                                  #1 /test.php(22): TestClass->__construct()
                                  #2 {main}
                                  "]