Using papertrail logging in Slim Framework v3 with MonoLog

I was looking for a good way to group logs from different webapplications into one managable place, without too much configuration work on the different webservers, since some run on shared hosting, and some have different setups.

I came across Paper Trail App A hosted log management tool, which gives you the ability to aggegrate logs from different places into one location, and here you can filter and group different webservers together, and search the log history.

I’m not a big fan of saving sensitive data to a cloud based solution, so I chose not to send any server logs etc to papertrail, only debugging logs for my own use, without sensitive data, so I can easily track if all systems are running (I only log if crons are running, how many results certain functions are parsing, and if any errors occured).

If you want to use papertrail as your central logging server with slim framework V3 you can add this to your dependencies.php file:

// monolog
$container['logger'] = function ($c) {
    $settings = $c->get('settings')['logger'];
    
    $output = "%channel%.%level_name%: %message%";
    $formatter = new Monolog\Formatter\LineFormatter($output);
    
    $logger = new Monolog\Logger($settings['name']);
    $syslogHandler = new Monolog\Handler\SyslogUdpHandler("PAPERTRAIL_APP_URL", 'YOUR_ASSIGNED_PORT');
    $syslogHandler->setFormatter($formatter);
    $logger->pushHandler($syslogHandler);

And add this to your settings.php:

 // Monolog settings
        'logger' => [
            'name' => 'paperTrail',
            'level' => \Monolog\Logger::DEBUG,
        ],

Then you can easily log data inside your classes with:

$this->ci->logger->addInfo("New order was placed");

Leave a Reply

Your email address will not be published.