Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Logging

Introduction

The framework uses Monolog by default for logging. To enable logging, use the LogWriter behavior trait in your class. The trait provides a $logger property — a Monolog logger accepting a message and a context array:

use Sukarix\Behaviours\LogWriter;

class MyService
{
    use LogWriter;

    public function process(string $id): void
    {
        $this->logger->info('Processing started', ['id' => $id]);
    }
}

Note

Initializing LogWriter

If the class does not extend the Tailored singleton class, ensure to call the initLogWriter method in the constructor.

Log Level

The minimum level is set with log.level (defaults to info):

[globals]
log.level = debug

Console Output

CLI applications can mirror the log to standard output by enabling log.console:

[globals]
log.console = true

When the optional bramus/monolog-colored-line-formatter package is installed, console output is coloured per level; otherwise a plain stream is used. The file handler stays active either way, so enabling this adds console output rather than replacing the log file.

Logging Naming Conventions

Logs follow these naming patterns:

  • Application Logs: app-yyyy-mm-dd.log
  • Application Error Logs: app-error-yyyy-mm-dd.log
  • CLI Logs: cli-yyyy-mm-dd.log
  • CLI Error Logs: cli-error-yyyy-mm-dd.log
  • Exception Logs: exception.log

Log Rotation

Logs are kept for 14 days by default, configurable via the log.keep setting (an integer in days). The Sukarix\Actions\Action\Clean action is responsible for rotating and cleaning logs.