Nikola Posa has a new post to his site showing you how to can combine Monolog for logging with Zend Service Manager, a component from the Zend Framework, defining the logger as a service that can be easily used (and re-used) across an application.
Without any doubt, Monolog and Zend Service Manager are two libraries that are almost always found in the composer.json file require section of my projects. In case you didn't know, Monolog is a PSR-3 compliant logging library that allows you to save logs to various storage types and web services, while Zend Service Manager is a PSR-11 compliant dependency injection container and a service locator implementation that facilitates management of application dependencies.
In this post I'm gonna show you how the two can work together.
He starts with an example of configuring the ServiceManager instance with a factory dependency that manually creates the Monolog logger inline. While this works, it's not idea, mixing configuration and functionality. He shows how to refactor the same functionality into a factory class that performs the same function but isolates it from the configuration. He then takes this further and separates out the environment-specific configuration from the handling and, finally, shows the creation of a more general logging factory that allows the definition of different kinds of loggers based on the need.