Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Zend Log Laravel Package

zendframework/zend-log

Zend Log is a PHP logging component for writing messages to multiple backends (files, syslog, databases, email) with flexible formatting, filters, and priorities. Part of Zend Framework, it helps you capture, route, and manage application logs reliably.

View on GitHub
Deep Wiki
Context7

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](https://github.com/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' => [
            // ...
        ],
    ],
];
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport