8/17/2017 - 3:56 AM

Monolog In Your Application

Monolog In Your Application

If you’re not already familiar with Monolog, it’s an excellent logging framework for PHP applications, which is why I’m going to use it here. First of all, get the Monolog library installed via Composer

php composer.phar require monolog/monolog

The dependency is named logger and the code to add it looks like this:

$container['logger'] = function($c) {
    $logger = new \Monolog\Logger('my_logger');
    $file_handler = new \Monolog\Handler\StreamHandler("../logs/app.log");
    return $logger;

With the logger in place, I can use it from inside my route code with a line like this:

$this->logger->addInfo("Something interesting happened");