Adding a logging service provider using Monolog

In the Part 7, I looked at how the Github service provider could access configuration and command line input parameters using the DI container. In this tutorial I’m going to look at how I added a logging service provider.

The source code for this tutorial is available in under v1.2 tag on Github.

Adding Monolog to composer.json

Okay, let me explain what I’m trying to do here. My application has an out method that allows me to send a message to standard output. That’s all well and good, but what do I do for other areas of the code. I don’t want to be passing the application around just to echo some text. So I thought to myself, why not use a logger and just “log” to standard output. Yeah, that could work.

Apart from that, logging is usually an important part of any application for numerous purposes covering anything from debugging to system performance evaluation. The Joomla Framework does include a Log package that was ported from the Joomla Platform, but, to be perfectly honest, there is a better option.

Monolog is a very popular and feature rich logging framework and it’s one of those cases where it’s so good and well supported, I don’t think the Joomla Framework needs to bother supporting its own logging package anymore.

Monolog is available on Packagist so we can install it with Composer. It follows the PSR-3 logging standard (so it is interchangeable with any other PSR-3 logging package if you stick to the PSR-3 interfaces).

To get the code, I’ve simply added the Monolog package to my composer.json file and run update.

    "monolog/monolog" : "1.6.*"

Adding the logging service provider

With the Monolog code download, it’s a simple exercise to run up another service provider.

namespace Tagaliser\Providers;

use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

class LoggerServiceProvider implements ServiceProviderInterface
{
    public function register(Container $container)
    {
        $container->share('logger', function(Container $c) {

            $logger = new Logger('Tagaliser');

            $logger->pushHandler(new StreamHandler('php://stdout'));

            return $logger;
        }, true);
    }
}

As you can see I’ve chosen a very basic handler - send all logging to standard output. You could really go to town with respect to logging, sending different types of log levels (warnings, errors, etc) to different places (files or databases). You could also add configuration variables to the application that let you configure the logging service to a more granular level.

Registering the logging service provider

Finally, I need to register the logging service provider in the applications initialise method as follows:

$container->registerServiceProvider(new Providers\LoggerServiceProvider);

End of Part 8

In the next tutorial I’m going to look at how I can extend the Github package API, but in a way that could be easily contributed back to the Joomla Framework.

Advanced Test Driven Development for Node - Part 1

Part 1 of my attempt to port Robert C. Martin's talk '8LU:Advanced Concepts in TDD' to Node. Continue reading

Semantic versioning for retail software

Published on December 11, 2014

Better Grunt files (for organised developers)

Published on December 02, 2014