Logging

This document assumes you’ve installed hiraeth/bootstrap or hiraeth/monolog. If you haven’t your mileage may vary. See the installation docs for more information.

Logging in Hiraeth is performed using Monolog which adhere’s to the PSR-3 FIG standard. You can receive an instance of the logger by typehinting Psr\Log\LoggerInterface. While this will inject the Monolog logger instance directly, it’s also possible to use the Hiraeth\Application instance which will proxy log related method calls to the logger.

Setting Log Level

The log level can be set in the .env with the LOGGING key:

LOGGING = error

If no .env exists or if the LOGGING key is missing, the default log level will be warning. Log level names are not case sensitive, however, should represent the constant names on Psr\Log\Loglevel.

Handlers

By default, hiraeth/monolog will configure a single Monolog\Handler\RotatingFileHandler which writes to storage/logs. Handlers can be modified or added in config/loggers. The basic structure of this file is as follows:

[logger]

    disabled = false

    class = Acme\Logger\Handler

    [&.options]

    ;    argument = value

On boot, the delegate which creates the logger will look for configured [logger] instances and instantiate their configured class with the arguments provided under [&.options].

Options for the class are named arguments. That is to say, the key for each option must match the parameter name on the class. These options should only contain non-object dependency information for the injector. If your handler requires additional object dependencies, the dependency injector will attempt to instantiate them automatically and use any delegates for those classes.

Using the Logger

To use the logger, you can typhint either Psr\Log\LoggerInterface or Hiraeth\Application. Each log level has its own method for logging at that log level:

$logger->error('This goes to handlers handling the error level')

Adding Context

You can add context to the logged error by providing an array as the second argument:

$logger->error('Failed updating user', ['id' => $user_id])