PHPackages                             ryudith/mezzio-custom-log - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Logging &amp; Monitoring](/categories/logging)
4. /
5. ryudith/mezzio-custom-log

ActiveLibrary[Logging &amp; Monitoring](/categories/logging)

ryudith/mezzio-custom-log
=========================

Library/middleware custom log for Mezzio.

00PHP

Since Jul 2Pushed 3y ago1 watchersCompare

[ Source](https://github.com/ryudith/mezzio-custom-log)[ Packagist](https://packagist.org/packages/ryudith/mezzio-custom-log)[ RSS](/packages/ryudith-mezzio-custom-log/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependenciesVersions (1)Used By (0)

**mezzio-custom-log**
=====================

[](#mezzio-custom-log)

**`Ryudith\MezzioCustomLog`** is middleware to auto create custom log based on request.

**Installation**
----------------

[](#installation)

To install run command :

```
$ composer require ryudith/mezzio-custom-log
```

**Usage**
---------

[](#usage)

#### **Add `Ryudith\MezzioCustomLog\ConfigProvider`** to **`config/config.php`**

[](#add-ryudithmezziocustomlogconfigprovider-to-configconfigphp)

```
...

$aggregator = new ConfigAggregator([
    ...
    \Laminas\Diactoros\ConfigProvider::class,

    \Ryudith\MezzioCustomLog\ConfigProvider::class,  // pipe(ErrorHandler::class);
    $app->pipe(CustomLogMiddleware::class);  //  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**
------------------------

[](#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 :

1. **`format`**
    is format output log, `json` or `plain`.
2. **`data_delimiter`**
    is data delimiter if use `plain` format.
3. **`log_file`**
    is log file location directory.
4. **`default_message`**
    is default message for log, except for exception which the message is exception message.
5. **`save_log_level`**
    is log level that need to save.
6. **`notify_log`**
    is flag to notify when some level log occur, default using php mail.
7. **`notify_interval_time`**
    is interval time to send notify email log, default is 3600 or 1 hour.
8. **`notify_log_level`**
    is which log level should send notification.
9. **`log_notify_email`**
    is email address to send notification grouped by log level, log level control by `notify_log_level` configuration.
10. **`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.
11. **`log_storage_class`**
    is storage class name if want to use custom storage handler.
12. **`log_notify_class`**
    is notification class name if want to use custom notification handler.

Enable Helper
-------------

[](#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/zip` to 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.

1. **`helper_whitelist_ip`**
    is string single IP address value to access helper, default is `127.0.0.1`.
2. **`log_dir`**
    is log directory location, default is `./data/log` from your Mezzio project directory.
3. **`zip_log_filename`**
    is log output ZIP file name, default `'./data/zip/log_'.date('Ymd').'.zip'`.
4. **`back_days_param_name`**
    is URL query parameter name for how many days back from `base_date_param_name` parameter name value.
5. **`base_date_param_name`**
    is URL query parameter name for end date (exclusive) to pack log files.

> ### Change default configuration web helper value
>
> [](#change-default-configuration-web-helper-value)
>
> Create new file `config/autoload/customlog.local.php` and add :
>
> ```
>
