TL;DR
dot-log is a powerful, easily customizable logging tool. Version 3.1.1 adds the ability to use datetime formatter strings right in the stream option of a log writer, and fixes an issue where caching dot-log configs caused logs to be written to a single file instead of being grouped by date.
Prerequisites
- dot-log installed and configured inside your application. If it's not installed, follow the steps in Logging with dot-log in Zend Expressive and Dotkernel.
- Otherwise, make sure you're using the latest version by running:
composer update dotkernel/dot-log
If your application logs messages to a single file with a static name (e.g. log/dk.log), you can skip the rest of this guide - logging will work as before.
Configuring the logger with Dotkernel
Your application should already have a config/autoload/error-handling.global.php file similar to:
<?php
return [
'dot-errorhandler' => [
'loggerEnabled' => true,
'logger' => 'dot-log.default_logger'
],
'dot_log' => [
'loggers' => [
'default_logger' => [
'writers' => [
'FileWriter' => [
'name' => 'stream',
'priority' => \Laminas\Log\Logger::ALERT,
'options' => [
'stream' => sprintf('%s/../../log/error-log-%s.log', __DIR__, date('Y-m-d')),
// explicitly log all messages
'filters' => [
'allMessages' => [
'name' => 'priority',
'options' => [
'operator' => '>=',
'priority' => \Laminas\Log\Logger::EMERG,
],
],
],
'formatter' => [
'name' => \Laminas\Log\Formatter\Json::class,
],
],
],
],
],
],
],
];
-
Locate every log writer by navigating to
dot_log -> loggers -> default_logger -> writers. -
For each writer, find the
streamoption containing the path to the log file. If it looks like:'stream' => sprintf('%s/../../log/error-log-%s.log', __DIR__, date('Y-m-d')) -
Replace it with:
'stream' => __DIR__ . '/../../log/error-log-{Y}-{m}-{d}.log' -
Clear the config cache:
php bin/clear-config-cache.php
If the log path uses other date format specifiers, adapt them accordingly - the full list is in the PHP date() manual.
Configuring the logger without Dotkernel
Locate the dot-log config (probably /config/autoload/log.global.php), which should look similar to the Dotkernel example but keyed under a custom logger name (e.g. my_logger) and may define multiple writers (e.g. a general file writer, a warnings-only writer, and a warnings-or-higher writer).
-
Locate every writer by navigating to
dot_log -> loggers -> my_logger -> writers. -
For each writer, find the
streamoption. If it looks like:'stream' => sprintf('%s/../../log/dk-%s.log', __DIR__, date('Y-m-d')) -
Replace it with:
'stream' => __DIR__ . '/../../log/error-log-{Y}-{m}-{d}.log', -
Make sure you clear your application's config before usage.
As before, adapt any other date format specifiers as needed - see the PHP date() manual.
Frequently Asked Questions
What does dot-log Version 3.1.1 add? +
Version 3.1.1 adds the ability to use datetime formatter strings directly in the stream option of your log writer, and it fixes an issue where caching dot-log configs caused logs to be written to the same file instead of being grouped by date.
How do I make sure I'm using the fix in Version 3.1.1? +
Make sure you're using the latest version of dot-log by running `composer update dotkernel/dot-log`.
Do I need to change anything if my logs are already written to a single static file? +
No. If your application logs messages in a single file with a static name (e.g. log/dk.log), you can skip the rest of the article - logging will work as before.
How do I group log files by date when using Dotkernel? +
In `config/autoload/error-handling.global.php`, locate every log writer by navigating to `dot_log->loggers->default_logger->writers` and replace a stream value like `sprintf('%s/../../log/error-log-%s.log', __DIR__, date('Y-m-d'))` with `__DIR__ . '/../../log/error-log-{Y}-{m}-{d}.log'`.
How do I group log files by date without Dotkernel? +
In your dot-log config (e.g. `/config/autoload/log.global.php`), locate every writer under `dot_log->loggers->my_logger->writers` and replace a dynamic stream value with a formatter string using `{Y}-{m}-{d}` placeholders.
What must I do after changing the stream configuration? +
Clear the config cache. In a Dotkernel application, run `php bin/clear-config-cache.php`; in a non-Dotkernel setup, make sure you clear your application's config before usage.