kba-team/cakephp-graylog

Graylog engine for CakePHP

Installs: 2 843

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 1

Open Issues: 1

Type:cakephp-plugin

v2.2.0 2020-10-15 06:55 UTC

README

License: MIT Packagist Version Build Status Maintainability Test Coverage

Graylog log engine for CakePHP 3.x

Usage

composer require kba-team/cakephp-graylog
<?php
\Cake\Core\Configure::write('Log.graylog', [
    'className' => \kbATeam\CakePhpGraylog\Log\Engine\GraylogLog::class,
    'levels' => [\Psr\Log\LogLevel::EMERGENCY, \Psr\Log\LogLevel::ALERT, \Psr\Log\LogLevel::CRITICAL],
    'host' => 'graylog.example.com',
    'port' => 12201,
    'scheme' => 'udp',
    'facility' => 'MyAppName',
    'append_backtrace' => true,
    'append' => [
        //append the contents of $_POST JSON encoded to the message body
        'POST' => static function () {
             if (!empty($_POST)) {
                $obfuscator = new \kbATeam\GraylogUtilities\Obfuscator();
                //Replace the value of all keys named 'password' with 'xxx'.
                $obfuscator->addKey('password');
                $obfuscator->setObfuscationString('xxx');
                //JSON encode the POST variable for readability.
                return json_encode(
                    $obfuscator->obfuscate($_POST),
                    JSON_PRETTY_PRINT
                );
             }
             return null;
        }
    ],
    'additional' => [
        //Add field 'current_user' to the GELF message.
        'current_user' => static function () {
             return AuthComponent::user('username');
        }
    ]
]);

Possible configuration parameters are:

  • scheme Currently TCP or UDP connections to Graylog are supported. Default: udp
  • host The hostname of the Graylog server. Default: 127.0.0.1
  • port The port, the Graylog server listens to. Default: 12201
  • url A connection URL in format <scheme>://<host>:<port>. This will overwrite any other settings.
  • chunk_size The size of the UDP packages. Default: \Gelf\Transport\UdpTransport::CHUNK_SIZE_LAN
  • ssl_options An instance of \Gelf\Transport\SslOptions defining SSL settings for TCP connections. Default: null
  • facility The logging facility. Default: CakePHP.
  • append_backtrace Append a backtrace to the message? Default: true
  • append Array of anonymous functions (actually anything that is_callable()). Their return strings get appended to the message body.
  • additional Array of anonymous functions (actually anything that is_callable()). Their return values get added as additional fields to the GELF message.
  • levels Array of log level, that will be sent to Graylog. See \Psr\Log\LogLevel for all possible values. Default: all of them.

Further reading