ryudith / mezzio-custom-log
Library/middleware custom log for Mezzio.
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/ryudith/mezzio-custom-log
Requires
- php: ^8.0
- laminas/laminas-diactoros: ^2.7
- psr/container: ^1.0
- psr/http-server-middleware: ^1.0
- psr/log: ^3.0
- symfony/console: ^6
Requires (Dev)
- guzzlehttp/guzzle: ^7.4.5
- phpunit/phpunit: ^9.5.11
This package is auto-updated.
Last update: 2025-10-28 11:09:43 UTC
README
Ryudith\MezzioCustomLog is middleware to auto create custom log based on request.
Installation
To install run command :
$ composer require ryudith/mezzio-custom-log
Usage
Add Ryudith\MezzioCustomLog\ConfigProvider to config/config.php
... $aggregator = new ConfigAggregator([ ... \Laminas\Diactoros\ConfigProvider::class, \Ryudith\MezzioCustomLog\ConfigProvider::class, // <= add this line // Swoole config to overwrite some services (if installed) class_exists(\Mezzio\Swoole\ConfigProvider::class) ? \Mezzio\Swoole\ConfigProvider::class : function (): array { return []; }, // Default App module config App\ConfigProvider::class, // Load application config in a pre-defined order in such a way that local settings // overwrite global settings. (Loaded as first to last): // - `global.php` // - `*.global.php` // - `local.php` // - `*.local.php` new PhpFileProvider(realpath(__DIR__) . '/autoload/{{,*.}global,{,*.}local}.php'), // Load development config if it exists new PhpFileProvider(realpath(__DIR__) . '/development.config.php'), ], $cacheConfig['config_cache_path']); ...
Add Ryudith\MezzioCustomLog\CustomLogMiddleware to config/pipeline.php
... return function (Application $app, MiddlewareFactory $factory, ContainerInterface $container): void { // The error handler should be the first (most outer) middleware to catch // all Exceptions. $app->pipe(ErrorHandler::class); $app->pipe(CustomLogMiddleware::class); // <= add this line ... };
You must place
$app->pipe(CustomLogMiddleware::class)after$app->pipe(ErrorHandler::class)because custom log also add to exception listener so can log when exception occur.
Custom Configuration
Configuration is locate in vendor/ryudith/mezzio-custom-log/ConfigProvider.php :
... return [ 'dependencies' => [ 'factories' => [ CustomLogMiddleware::class => CustomLogMiddlewareFactory::class, LogHandler::class => LogHandlerFactory::class, ], ], 'mezzio_custom_log' => [ 'format' => 'json', 'data_delimiter' => '|', 'log_file' => './data/log/log_'.date('Ymd'), 'default_message' => 'https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/{responseCode}', 'save_log_level' => ['ALERT', 'ERROR', 'CRITICAL', 'EMERGENCY',], 'notify_log' => false, 'notify_interval_time' => 3600, 'notify_log_level' => ['EMERGENCY', 'ALERT', 'CRITICAL', 'ERROR',], 'log_notify_email' => [ 'EMERGENCY' => [ 'admin@localhost.com', 'dev@localhost.com', ], 'ERROR' => [ 'sysadmin@localhost.com' ], ], 'log_notify_from' => 'skynet@localhost.com', 'log_storage_class' => null, 'log_notify_class' => null, ], ]; ...
Detail :
-
format
is format output log,jsonorplain. -
data_delimiter
is data delimiter if useplainformat. -
log_file
is log file location directory. -
default_message
is default message for log, except for exception which the message is exception message. -
save_log_level
is log level that need to save. -
notify_log
is flag to notify when some level log occur, default using php mail. -
notify_interval_time
is interval time to send notify email log, default is 3600 or 1 hour. -
notify_log_level
is which log level should send notification. -
log_notify_email
is email address to send notification grouped by log level, log level control bynotify_log_levelconfiguration. -
log_notify_from
is email address to verify notification email come from, if you using default php mail or use notification email in general.When you use email gateway service like sendinblue or sendgrid or any other service, make sure this value is your register valid email as sender to avoid block by provider.
-
log_storage_class
is storage class name if want to use custom storage handler. -
log_notify_class
is notification class name if want to use custom notification handler.
Enable Helper
To enable helper add the following items to factories configuration (usually in config/dependencies.global.php) :
... 'factories' => [ ... // add this to enable web version helper Ryudith\MezzioCustomLog\Helper\ZipWebHelperHandler::class => Ryudith\MezzioCustomLog\Helper\ZipWebHelperHandlerFactory::class, // add this to enable cli version helper Ryudith\MezzioCustomLog\Helper\ZipCliHelper::class => Ryudith\MezzioCustomLog\Helper\ZipCliHelperFactory::class, ... ] ...
Then for web helper register helper to routes.php
$app->get('/customlog/zip', ZipWebHelperHandler::class);
You can change
/customlog/zipto any route path.
For web helper you can change default configuration values as listed below by add array key to mezzio_custom_log configuration, also don't forget to add your IP to whitelist to be able access helper.
-
helper_whitelist_ip
is string single IP address value to access helper, default is127.0.0.1. -
log_dir
is log directory location, default is./data/logfrom your Mezzio project directory. -
zip_log_filename
is log output ZIP file name, default'./data/zip/log_'.date('Ymd').'.zip'. -
back_days_param_name
is URL query parameter name for how many days back frombase_date_param_nameparameter name value. -
base_date_param_name
is URL query parameter name for end date (exclusive) to pack log files.
Change default configuration web helper value
Create new file
config/autoload/customlog.local.phpand add :<?php declare(strict_types=1); return [ 'mezzio_custom_log' => [ 'helper_whitelist_ip' => '127.0.0.1', 'log_dir' => './data/log', 'zip_log_filename' => './data/zip/log_'.date('Ymd').'.zip', 'back_days_param_name' => 'days', 'base_date_param_name' => 'date', ], ];Then you can access helper with URL http://localhost:8080//customlog/zip?days=10&date=2022-06-12 00:00:00 with your whitelist IP for example. The URl tell to ZIP log from date
2022-06-12 00:00:00to 10 days before or from >=2022-06-02 00:00:00to <2022-06-12 00:00:00and will fail if there is no log file.
For CLI helper you need to register helper command to laminas-cli commands configuration usually locate in file config/autoload/mezzio.global.php :
... 'laminas-cli' => [ 'commands' => [ ... 'your:command' => Ryudith\MezzioCustomLog\Helper\ZipCliHelper::class, ... ], ], ...
Change
your:commandto your own choice command.
If you fail run command
composer run mezzio your:command helpfor example, try use run commandvendor/bin/laminas --ansi your:command help.