The LaminasMonolog module integrates monolog/monolog as a laminas module via laminas/laminas-servicemanager.
Install eth8505/laminas-monolog
package via composer.
$ composer require eth8505/laminas-monolog
Load the module in your application.config.php
file like so:
<?php
return [
'modules' => [
'LaminasMonolog',
// ...
],
];
In your application config (usually located in config/autoload/monolog.global.php
), specify your monolog in the
monolog/loggers
key.
Each key (Log\MyApp
in the sample code) can contain a separate logger config and is available directly via the
service manager.
return [
'monolog' => [
'loggers' => [
'Log\MyApp' => [
'name' => 'default'
]
]
]
];
Each logger config is available direcly via the service manager.
$logger = $container->get('Log\MyApp');
Multiple handlers
can be added to a logger config via the handlers
key.
return [
'monolog' => [
'loggers' => [
'Log\MyApp' => [
'name' => 'default',
'handlers' => [
'stream' => [
'name' => StreamHandler::class,
'options' => [
'path' => 'data/log/myapp.log',
'level' => Logger::DEBUG
],
],
'fire_php' => [
'name' => ChromePHPHandler:class
]
]
]
]
]
];
Each handler can be configured with a formatter in order to specify a specific format. This can be useful whenlogging to logstash for example.
return [
'monolog' => [
'loggers' => [
'Log\MyApp' => [
'name' => 'default',
'handlers' => [
'stream' => [
'name' => StreamHandler::class,
'options' => [
'path' => 'data/log/myapp.log',
'level' => Logger::DEBUG
],
'formatter' => [
'name' => LogstashFormatter::class,
'options' => [
'applicationName' => 'myApp',
'systemName' => gethostname()
]
]
]
]
]
]
]
];
Processors can be used to enrich the logged data with additional data. The WebProcessor can for example be used to add the request URI and client IP to the log record.
return [
'monolog' => [
'loggers' => [
'Log\MyApp' => [
'name' => 'default'
'processors' => [
WebProcessor::class
]
]
]
]
];
When configuring handlers, formatters or processors, you can either specify a class name in string (or ::class constant) format
return [
'monolog' => [
'loggers' => [
'Log\MyApp' => [
'name' => 'default'
'processors' => [
WebProcessor::class
]
]
]
]
];
or alternatively in name/options array notation, where the options are translated into the respective classes constructor parameters by using Reflection based Named parameters.
return [
'monolog' => [
'loggers' => [
'Log\MyApp' => [
'name' => 'default'
'processors' => [
[
'name' => WebProcessor::class,
'options' => [
'extraFields' => [
'url' => 'REQUEST_URI',
'http_method' => 'REQUEST_METHOD',
'server' => 'SERVER_NAME'
]
]
]
]
]
]
]
];
Since this module creates everything via the service manager using plugin managers, custom handlers, processors and formatters can be easily registered, by adding them to the respective config keys
return [
'monolog' => [
'formatters' => [
factories' => [
MyCustomFormatter::class => MyCustomFormatterFactory::class
]
],
'handlers' => [
'factories' => [
MyCustomHandler::class => MyCustomHandlerFactory::class
]
],
'processors' => [
'factories' => [
MyCustomProcessor::class => MyCustomProcessorFactory::class
]
]
]
];
You can define default loggers and inherit from them in other loggers.
return [
'monolog' => [
'loggers' => [
'base' => [
// default logger config
],
'inherited' => [
'@extends' => 'base'
]
]
]
];
ℹ️ Even though recursion is supported here as of Version 1.0.3, it is limited to 10 levels and will
throw a LaminasMonolog\Exception\RuntimeException
if recursed any deeper.
See example config for details.
Thanks to neckeloo and his Monolog Module and enlitepro for their Enlite Monolog as they served as a template for this module.