CakePHP 4 – Logging to Syslog

The documentation at https://book.cakephp.org/4/en/core-libraries/logging.html#logging-to-syslog recommends…

Login

Blog History

The documentation at https://book.cakephp.org/4/en/core-libraries/logging.html#logging-to-syslog recommends logging to Syslog in production by adding the following snippet to config/bootstrap.php

Log::setConfig('default', [
    'engine' => 'Syslog'
]);

If you add a prefix to the config then you can grep or filter by SYSLOG_IDENTIFIER

Log::setConfig('default', [
    'engine' => 'Syslog',
    'prefix' => 'cakephp-dev'
]);

Then use journalctl to view the CakePHP logs filtered by the prefix

# grep
journalctl -e | grep cakephp-dev

# filter by prefix value set in config
journalctl SYSLOG_IDENTIFIER=cakephp-dev

# see all the fields in KEY=VALUE format you can use verbose and filter by all sorts of keys.
journalctl -o verbose -e SYSLOG_IDENTIFIER=cakephp-dev

# output 
Wed 2023-01-11 02:28:19.615991 UTC [s=1220877691a64abc83752e36d1411f47;i=18bd3e;b=762c830fc3a945eaaa464ab9e9b0b2ea;m=3fd607d9c6;t=5f1f3c0ec5d26;x=6cb0d10db7f6476b]
    _TRANSPORT=syslog
    _SELINUX_CONTEXT=unconfined
    _SYSTEMD_SLICE=system.slice
    _MACHINE_ID=b3f558bdd7b146148e28f41f32c34d7b
    _HOSTNAME=tgn-wms-vm01
    _CAP_EFFECTIVE=0
    PRIORITY=4
    _BOOT_ID=762c830fc3a945eaaa464ab9e9b0b2ea
    SYSLOG_FACILITY=1
    MESSAGE=warning: DebugKit is disabling itself as your host `ofs.toggen.com.au` is not in the known safe list of top-level-domains (localhost, invalid, test, example, local). If you would like to force >
    _PID=47409
    _UID=33
    _GID=33
    _COMM=php-fpm8.1
    _EXE=/usr/sbin/php-fpm8.1
    _CMDLINE="php-fpm: pool www" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ">
    _SYSTEMD_CGROUP=/system.slice/php8.1-fpm.service
    _SYSTEMD_UNIT=php8.1-fpm.service
    _SYSTEMD_INVOCATION_ID=0de3502228954139b7ceb0c999ec8710
    SYSLOG_IDENTIFIER=cakephp-dev
    SYSLOG_TIMESTAMP=Jan 11 02:28:19
    _SOURCE_REALTIME_TIMESTAMP=1673404099615991

If you don't want to globally replace the logging config as above you can omit adding the above setConfig snippet and you can change config/app.php and replace FileLog::class with SyslogLog::class

<?php
use Cake\Log\Engine\SyslogLog;

$prefix = 'cakephp-dev';

return [
    // ... snippage
    'Log' => [
        'debug' => [
            'prefix' => $prefix,
            'className' => SyslogLog::class,
            'path' => LOGS,
            'file' => 'debug',
            'url' => env('LOG_DEBUG_URL', null),
            'scopes' => false,
            'levels' => ['notice', 'info', 'debug'],
        ],
        'error' => [
            'prefix' => $prefix,
            'className' => SyslogLog::class,
            'path' => LOGS,
            'file' => 'error',
            'url' => env('LOG_ERROR_URL', null),
            'scopes' => false,
            'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'],
        ],
        // To enable this dedicated query log, you need set your datasource's log flag to true
        'queries' => [
            'prefix' => $prefix,
            'className' => SyslogLog::class,
            'path' => LOGS,
            'file' => 'queries',
            'url' => env('LOG_QUERIES_URL', null),
            'scopes' => ['queriesLog'],
        ],
        //... snippage
    ],
];

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.