Slim v3 error log with option to log errors into both file and database

I am new to slim framework, using slim version 3 and want to implement error logs. I want to use both the type like want to log the errors to a file and some errors i want to add to the database tables. Which is the better way or how to do , please help me.

I would probably use Monolog if you are looking to send errors to multiple places. There is an example of its use in the Slim-Skeleton. The package is (1) required in composer, (2) given some settings, (3) added to the container, and you can see where it will be fired (4) in this route.

I had this same question. In the past I have written my own, clunky log class, but I’m trying to go with the flow here.

I like logging lowest level (INFO), ie: user activity, to the Database but log warning, errors, etc to a text file. And to complicate matters even more I usually create a single text file per day. I always thought this was the easiest way to see if the app was running smoothly.

I’ve been looking at the skeleton app but it appears to only log things to file: /…/logs/app.log

Any advice on this?

You can use Monolog to send different types of logs to different places by creating additional handlers.

For day based log files, your settings.php file (from the Slim-Skeleton) might look like this:

<?php

$logDate = new DateTime();

return [
    'settings' => [
        'displayErrorDetails' => true, // set to false in production
        'addContentLengthHeader' => false, // Allow the web server to send the content-length header
        // Renderer settings
        'renderer' => [
            'template_path' => __DIR__ . '/../templates/',
        ],
        // Monolog settings
        'logger' => [
            'name' => 'my-app',
            'path' => __DIR__ . '/../logs/' . $logDate->format('Y-m-d') . 'app.log',
        ],
    ],
];

$logDate->format('Y-m-d') . 'app.log’ … duh… stupid me :stuck_out_tongue_winking_eye:

I was looking at those handlers a week ago and didn’t see anything for mysql… I did find : https://github.com/waza-ari/monolog-mysql … I’ll see if I can get this going tomorrow and post a solution if I find it.

After close to 4 hours of tinkering I’ve got a solution … I’ve included the Monolog MySQL handler via composer: https://github.com/waza-ari/monolog-mysql

Here’s my latest working code: (would like feedback)

my dependencies.php

//database
$container['pdo'] = function($c){
    $db = $c['settings']['db'];
    $pdo = new PDO("mysql:host=" . $db['host'] . ";dbname=" . $db['dbname'], $db['user'], $db['pass']);
    return $pdo;
};
$container['db'] = function($c){
    //setup NOTORM
    $db = $c['settings']['db'];
    $pdo = new PDO("mysql:host=" . $db['host'] . ";dbname=" . $db['dbname'], $db['user'], $db['pass']);
    $db = new NotORM($pdo);
    $db->debug = true;
    return $db;
};

// monolog
$container['logger'] = function ($c) {
    $settings = $c->get('settings')['logger'];
    $logger = new Monolog\Logger($settings['name']);
    $logger->pushProcessor(new Monolog\Processor\UidProcessor());
    $logger->pushHandler(new Monolog\Handler\StreamHandler($settings['path'], $settings['level']));
    return $logger;
};
$container['dbLogger'] = function ($c) {
	$settings = $c->get('settings')['logger'];
	$mySQLHandler = new MySQLHandler\MySQLHandler($c->pdo, "log", array('id','msg'), \Monolog\Logger::DEBUG);
	$logger = new \Monolog\Logger($settings['name']);
	$logger->pushHandler($mySQLHandler);
	return $logger;
};

Then in a route I call:

$this->dbLogger->info("db info", array('id'=>0,'msg'=>md5(time())));

some notes about this hander…it creates the table for you and I ran into trouble trying to create my own table before hand.

The code works as expected but is this the proper Slim way to do things? I’m finding that all of my own solutions are generally the wrong way to execute, regardless of whether they work or not.

If it was merely working code I was after I would have written some spaghetti code and been done in 20 minutes.

-thanks

Does anybody managed Monolog to log the response? For me, it is important to have the Request details, but also the Response without using custom messages like $this->dbLogger->info(“db info”, array(‘id’=>0,‘msg’=>md5(time()))); .

Thank you in advance.