Reference

Service Manager

The zend-log package provides several components which can be used in combination with zend-servicemanager. These components make it possible to quickly setup a logger instance or to provide custom writers, filters, formatters, or processors.

LoggerAbstractServiceFactory

When you register the abstract factory called Zend\Log\LoggerAbstractServiceFactory, you will be able to setup loggers via the configuration. The abstract factory can be registered in the service manager using the following configuration:

// module.config.php
return [
    'service_manager' => [
        'abstract_factories' => [
            'Zend\Log\LoggerAbstractServiceFactory',
        ],
    ],
];

Users of zend-component-installer

If you are using zend-component-installer, you will have been prompted to install zend-log as a module or configuration provider when you installed zend-log. When you do, the abstract factory is automatically registered for you in your configuration.

zend-log as a module

If you are using zend-log v2.8 or later with a zend-mvc-based application, but not using zend-component-installer, you can register Zend\Log as a module in your application. When you do, the abstract service factory will be registered automatically.

Next, define your custom loggers in the configuration as follows:

// module.config.php
return [
    'log' => [
        'MyLogger' => [
            'writers' => [
                'stream' => [
                    'name' => 'stream',
                    'priority' => 1,
                    'options' => [
                        'stream' => 'php://output',
                        'formatter' => [
                            'name' => \Zend\Log\Formatter\Simple::class,
                            'options' => [
                                'format' => '%timestamp% %priorityName% (%priority%): %message% %extra%',
                                'dateTimeFormat' => 'c',
                            ],
                        ],
                        'filters' => [
                            'priority' => [
                                'name' => 'priority',
                                'options' => [
                                    'operator' => '<=',
                                    'priority' => \Zend\Log\Logger::INFO,
                                ],
                            ],
                        ],
                    ],
                ],
            ],
            'processors' => [
                'requestid' => [
                    'name' => \Zend\Log\Processor\RequestId::class,
                ],
            ],
        ],
    ],
];

The logger can now be retrieved via the service manager using the key used in the configuration (MyLogger):

/** @var \Zend\Log\Logger $logger */
$logger = $container->get('MyLogger');

For the formatter and the filters, only the name is required, the options have default values (the values set in this example are the default ones). When only the name is needed, a shorter format can be used:

// module.config.php
                    'options' => [
                        'stream' => 'php://output',
                        'formatter' => \Zend\Log\Formatter\Simple::class,
                        'filters' => [
                            'priority' => \Zend\Log\Filter\Priority::class,
                        ],
                    ],
];

Because the main filter is Priority, it can be set directly too:

// module.config.php
                        'filters' => \Zend\Log\Logger::INFO,
];

PsrLoggerAbstractServiceFactory

As with the LoggerAbstractServiceFactory above, you can use PsrLoggerAbstractServiceFactory to create PSR-3-conforming logger instances. Register it as an abstract factory in your configuration; as an example:

// module.config.php

use Zend\Log\PsrLoggerAbstractServiceFactory;

return [
    'service_manager' => [
        'abstract_factories' => [
            PsrLoggerAbstractServiceFactory::class,
        ],
    ],
];

Additionally, instead of using the log configuration key, you will use the key psr_log:

// module.config.php

use Zend\Log\Formatter\Simple;
use Zend\Log\Logger;
use Zend\Log\Processor\RequestId;

return [
    'psr_log' => [                   // <-- NOTE: key change!
        'MyLogger' => [
            'writers' => [
                'stream' => [
                    'name' => 'stream',
                    'priority' => 1,
                    'options' => [
                        'stream' => 'php://output',
                        'formatter' => [
                            'name' => Simple::class,
                            'options' => [
                                'format' => '%timestamp% %priorityName% (%priority%): %message% %extra%',
                                'dateTimeFormat' => 'c',
                            ],
                        ],
                        'filters' => [
                            'priority' => [
                                'name' => 'priority',
                                'options' => [
                                    'operator' => '<=',
                                    'priority' => Logger::INFO,
                                ],
                            ],
                        ],
                    ],
                ],
            ],
            'processors' => [
                'requestid' => [
                    'name' => RequestId::class,
                ],
            ],
        ],
    ],
];

Custom Writers, Formatters, Filters, and Processors

In the LoggerAbstractServiceFactory example above, a custom formatter (called MyFormatter) and a custom filter (called MyFilter) are used. These classes need to be made available to the service manager. It's possible to do this via custom plugin managers which have the names:

  • log_formatters
  • log_filters
  • log_processors
  • log_writers

Example

// module.config.php
return [
    'log_formatters' => [
        'factories' => [
            // ...
        ],
    ],
    'log_filters' => [
        'factories' => [
            // ...
        ],
    ],
    'log_processors' => [
        'factories' => [
            // ...
        ],
    ],
    'log_writers' => [
        'factories' => [
            // ...
        ],
    ],
];

Found a mistake or want to contribute to the documentation? Edit this page on GitHub!