PHPackages                             leinonen/yii2-monolog - 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. leinonen/yii2-monolog

AbandonedArchivedYii2-extension[Logging &amp; Monitoring](/categories/logging)

leinonen/yii2-monolog
=====================

Monolog extension for Yii2

1.0.1(8y ago)8112.2k↑225%6[1 issues](https://github.com/lordthorzonus/yii2-monolog/issues)[3 PRs](https://github.com/lordthorzonus/yii2-monolog/pulls)MITPHPPHP ^7.0

Since Feb 14Pushed 4y ago1 watchersCompare

[ Source](https://github.com/lordthorzonus/yii2-monolog)[ Packagist](https://packagist.org/packages/leinonen/yii2-monolog)[ RSS](/packages/leinonen-yii2-monolog/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (2)Dependencies (5)Versions (6)Used By (0)

[![Latest Stable Version](https://camo.githubusercontent.com/e95df2fb4b6a577a7d4741ac68ff0cd1caeb1dc0e4bd0ef022292aa82a4cec5b/68747470733a2f2f706f7365722e707567782e6f72672f6c65696e6f6e656e2f796969322d6d6f6e6f6c6f672f762f737461626c65)](https://packagist.org/packages/leinonen/yii2-monolog)[![Total Downloads](https://camo.githubusercontent.com/c9fbf07da2fec4a55dea39898bd543c05e8b2cae48d31d2089ef19fb520bcaeb/68747470733a2f2f706f7365722e707567782e6f72672f6c65696e6f6e656e2f796969322d6d6f6e6f6c6f672f646f776e6c6f616473)](https://packagist.org/packages/leinonen/yii2-monolog)[![Latest Unstable Version](https://camo.githubusercontent.com/6810b32cc8e7659eb579d102e719464c96c7d20185fc32ec6f6c01391f0d8561/68747470733a2f2f706f7365722e707567782e6f72672f6c65696e6f6e656e2f796969322d6d6f6e6f6c6f672f762f756e737461626c65)](https://packagist.org/packages/leinonen/yii2-monolog)[![License](https://camo.githubusercontent.com/e26912c57a21e051b1f32e304e02531ba115dbd53afa8da67791247fc55dce9f/68747470733a2f2f706f7365722e707567782e6f72672f6c65696e6f6e656e2f796969322d6d6f6e6f6c6f672f6c6963656e7365)](https://packagist.org/packages/leinonen/yii2-monolog)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/fcda71cb0ccf05cef5726ee68c3ad18ff431073525d7ea05082d7de14105f3c8/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6c6f726474686f727a6f6e75732f796969322d6d6f6e6f6c6f672f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/lordthorzonus/yii2-monolog/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/d9785f25d11421e32398fd653c009cfe20c3071304ed0fb2562e277b7d074df9/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6c6f726474686f727a6f6e75732f796969322d6d6f6e6f6c6f672f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/lordthorzonus/yii2-monolog/?branch=master)[![Build Status](https://camo.githubusercontent.com/0e16bdb55f21f6a085f82e53f4dbb44d18491529be48f243e38a16b3141639c0/68747470733a2f2f7472617669732d63692e6f72672f6c6f726474686f727a6f6e75732f796969322d6d6f6e6f6c6f672e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/lordthorzonus/yii2-monolog)[![SensioLabsInsight](https://camo.githubusercontent.com/c127ea4457e8cea6858ce62e2b038c60fa6e102b18c08c54f93a85a5aa72df94/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f34346132653066332d636465362d343862392d623438342d3832343361363431343564652f6d696e692e706e67)](https://insight.sensiolabs.com/projects/44a2e0f3-cde6-48b9-b484-8243a64145de)

Yii2 Monolog
------------

[](#yii2-monolog)

Yii2 Monolog allows one to use [Monolog](https://github.com/Seldaek/monolog) easily with Yii2. For instructions regarding Monolog itself please refer to the [documentation](https://github.com/Seldaek/monolog/blob/master/doc/01-usage.md).

Table of contents
=================

[](#table-of-contents)

- [Monolog Usage](https://github.com/Seldaek/monolog/blob/master/doc/01-usage.md)
- [Installation](#installation)
- [Configuration](#configuration)
    - [Channels](#channels)
    - [Handlers](#handlers)
    - [Formatters](#formatters)
    - [Processors](#processors)
    - [Configuring Handlers/Formatters/Processors after creation](#configuring-handlersformattersprocessors-after-creation)
- [Usage](#usage)
    - [Using the component as a Yii's log target](#using-the-component-as-a-yiis-log-target)
    - [Using the component standalone](#using-the-component-standalone)

Installation
------------

[](#installation)

Require this package, with [Composer](https://getcomposer.org/), in the root directory of your project.

```
composer require leinonen/yii2-monolog
```

Configuration
-------------

[](#configuration)

Configure the `leinonen\Yii2Monolog\Yii2Monolog` as a bootstrapped component in your application config.

An example configuration of one log channel called `myLoggerChannel` with a basic StreamHandler and a UidProcessor:

```
use leinonen\Yii2Monolog\MonologTarget;
use leinonen\Yii2Monolog\Yii2Monolog;
use Monolog\Handler\StreamHandler;
use Monolog\Processor\UidProcessor;

...
[
    'bootstrap' => ['monolog'],
    'components' => [
        ...
        'monolog' => [
            'class' => Yii2Monolog::class,
            'channels' => [
                'myLoggerChannel' => [
                    'handlers' => [
                        StreamHandler::class => [
                            'path' => '@app/runtime/logs/someLog.log',
                        ],
                    ],
                    'processors' => [
                        UidProcessor::class,
                    ],
                ],
            ],
        ],
    ],
    ...
]
```

### Channels

[](#channels)

To see the core concepts about Monolog channels check the [Offical Documentation for Monolog](https://github.com/Seldaek/monolog/blob/master/doc/01-usage.md#core-concepts).

This component allows registering multiple channels with `channel name` =&gt; ` configuration array` key value pairs with the `channels` configuration key.

#### Main channel

[](#main-channel)

The component automatically registers a main channel which is used when requesting `Psr\Log\LoggerInterface` from the DI container or when fetching a Logger from the component without specifying a channel name.

The main channel is configurable with the configuration key `mainChannel`

```
[
    'components' => [
        ...
        'monolog' => [
            'class' => Yii2Monolog::class,
            'channels' => [
                'myFirstChannel' => [
                    ...
                ],
                'someOtherAwesomeChannel' => [
                    ...
                ],
            ],
            'mainChannel' => 'someOtherAwesomeChannel'
        ]
    ]
]
```

If the main channel is null or not specified at all, the first channel from the channels list will be used as the main channel. With the example config above it would be `myFirstChannel`.

### Handlers

[](#handlers)

The package supports all official and 3rd party handlers for Monolog. It uses `leinonen\Yii2Monolog\CreationStrategies\ReflectionStrategy` by default in background to figure out the config values which the handler is to be constructed with. The handlers are defined with a config key `handlers` in the Monolog configuration. All the handlers are resolved through Yii's DI container making it easier to implement your own custom handlers.

Example handler configuration with a stack of two handlers:

```
[
    ...
        'monolog' => [
            'channels' => [
                'myLoggerChannel' => [
                    'handlers' => [
                        SlackbotHandler::class => [
                            'slackTeam' => 'myTeam',
                            'token' => 'mySecretSlackToken',
                            'channel' => 'myChannel',
                        ],
                        RotatingFileHandler::class => [
                            'path' => '@app/runtime/logs/myRotatinglog.log',
                            'maxFiles' => 10,
                        ],
                    ],
                ],
            ],
        ],
    ...
]
```

You can find the available handlers from the [Monolog\\Handler namespace](https://github.com/Seldaek/monolog/tree/master/src/Monolog/Handler)

#### Yii2 specific handlers

[](#yii2-specific-handlers)

The package also provides a specific creation strategies for couple of handlers to help integrating with Yii2.

##### StreamHandler

[](#streamhandler)

The `path` config value is resolved through Yii's `getAlias()` method making it possible to use aliases such as `@app` in the config. Use this instead of `stream`

##### RotatingFileHandler

[](#rotatingfilehandler)

The `path` config value is resolved through Yii's `getAlias()` method making it possible to use aliases such as `@app` in the config. Use this instead of `filename`

### Formatters

[](#formatters)

The package supports all official and 3rd party formatters for Monolog. It uses `leinonen\Yii2Monolog\CreationStrategies\ReflectionStrategy` by default in background to figure out the config values which the formatter is to be constructed with. All the formatters are resolved through Yii's DI container making it easier to implement your own custom formatters.

It's possible to configure a custom formatter for each handler. The formatter is configured with a `formatter` key in the handler's config array:

```
'handlers' => [
    StreamHandler::class => [
        'path' => '@app/runtime/logs/myLog.log',
        'formatter' => [
            LineFormatter::class => [
                'format' => "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n",
                'dateFormat' => "Y-m-d\TH:i:sP"
            ]
        ]
    ]
]
```

You can find available formatters from the [Monolog/Formatter](https://github.com/Seldaek/monolog/blob/master/src/Monolog/Formatter) namespace.

### Processors

[](#processors)

The package supports all official and 3rd party processors for Monolog. It uses `leinonen\Yii2Monolog\CreationStrategies\ReflectionStrategy` by default in background to figure out the config values which the processor is to be constructed with. All the processors are resolved through Yii's DI container making it easier to implement your own custom processors.

The processors can be defined globally for one target or specifically for a handler. Processors are configured with a `processors` key in the config array with an array of callables:

```
[
    ...
        'monolog' => [
            'channels' => [
                'myLoggerChannel' => [
                    'processors' => [
                        GitProcessor::class,
                        function ($record) {
                            $record['myCustomData'] = 'test';

                            return $record;
                        },
                    ],
                ],
            ],
        ],
    ...
]
```

Or config to a specific handler:

```
...
'handlers' => [
    StreamHandler::class => [
    'path' => '@app/runtime/logs/myLog.log',
    'processors' => [
        GitProcessor::class,
        function ($record) {
            $record['myCustomData'] = 'test';

            return $record;
        }
    ]
]
```

You can find available processors from the [Monolog/Processor](https://github.com/Seldaek/monolog/blob/master/src/Monolog/Processor) namespace.

### Configuring Handlers/Formatters/Processors after creation

[](#configuring-handlersformattersprocessors-after-creation)

For further customisation it's possible to specify a `configure` key for all classes that are created with `leinonen\Yii2Monolog\CreationStrategies\ReflectionStrategy` . The configure key must be a callable which receives the created class instance and config. It also has to return the instance. It is called just after the class has been resolved from Yii's DI container. For example it's possible to customize `Monolog\Handler\RotatingFileHandler`'s filename format:

```
'handlers' => [
    RotatingFileHandler::class => [
        'path' => 'something',
        'maxFiles' => 2,
        'configure' => function (RotatingFileHandler $handler, $config) {
            $handler->setFilenameFormat('myprefix-{filename}-{date}', 'Y-m-d');

            return $handler;
        }
    ],
]
```

Usage
-----

[](#usage)

### Using the component as a Yii's log target

[](#using-the-component-as-a-yiis-log-target)

If you want to integrate this component into an existing project which utilizes Yii's own logger, you can configure channels as log targets easily. [See Yii's documentation about log targets here](http://www.yiiframework.com/doc-2.0/guide-runtime-logging.html#log-targets).

Example configuration:

```
use leinonen\Yii2Monolog\MonologTarget;
use leinonen\Yii2Monolog\Yii2Monolog;

[
    'bootstrap' => ['monolog', 'log'],
    'components' => [
        ...
        'monolog' => [
            'class' => Yii2Monolog::class,
            'channels' => [
                'myFirstChannel' => [
                    ...
                ],
                'someOtherAwesomeChannel' => [
                    ...
                ],
            ],
            'mainChannel' => 'someOtherAwesomeChannel'
        ],
        'log' => [
            'targets' => [
                [
                    'class' => MonologTarget::class,
                    'channel' => 'myFirstChannel',
                    'levels' => ['error', 'warning']
                ],
            ]
        ]
    ]
]
```

In this case all the Yii's loggers messages will go through the handler / processor stack of `myFirstChannel` logger without touching any of your existing code.

```
\Yii::warning('hello');
\Yii::error('world!');
```

If you leave the channel configuration out the target will use the main channel configured in the component.

### Using the component standalone

[](#using-the-component-standalone)

If you want to not use Yii's logger at all it's possible to use this component as a completely standalone logger.

Fetching a specific logger from the component:

```
 $myChannelLogger = Yii::$app->monolog->getLogger('myChannel');
 $myChannelLogger->critical('help me!');

 $mainChannelLogger = Yii::$app->monolog->getLogger();
 $mainChannelLogger->notice('This was a log message through the main channel');
```

As the main channel is registered as the implementation of the `Psr\Log\LoggerInterface` you can also use constructor injection in your controllers:

```
class SuperController
{
    private $logger;

    public function __construct($id, $module, LoggerInterface $logger, $config = [])
    {
        $this->logger = $logger;
        parent::__construct($id, $module, $config);
    }

    public function actionExample()
    {
        $this->logger->notice('Action Example was called');
    }
}
```

###  Health Score

36

—

LowBetter than 79% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity37

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 91.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~71 days

Total

2

Last Release

2983d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/b6a5d10ce7b5286c44580e2437115587c0930424cfbaa20cf397011837987fac?d=identicon)[lordthorzonus](/maintainers/lordthorzonus)

---

Top Contributors

[![lordthorzonus](https://avatars.githubusercontent.com/u/8689671?v=4)](https://github.com/lordthorzonus "lordthorzonus (33 commits)")[![Wirone](https://avatars.githubusercontent.com/u/600668?v=4)](https://github.com/Wirone "Wirone (2 commits)")[![Vladnev](https://avatars.githubusercontent.com/u/11948038?v=4)](https://github.com/Vladnev "Vladnev (1 commits)")

---

Tags

loggerlogginglogging-librarymonologmonolog-libraryyii2yii2-extensionyii2-logging

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/leinonen-yii2-monolog/health.svg)

```
[![Health](https://phpackages.com/badges/leinonen-yii2-monolog/health.svg)](https://phpackages.com/packages/leinonen-yii2-monolog)
```

###  Alternatives

[craftcms/cms

Craft CMS

3.6k3.6M2.9k](/packages/craftcms-cms)[symfony/monolog-bridge

Provides integration for Monolog with various Symfony components

2.6k198.8M328](/packages/symfony-monolog-bridge)[matomo/matomo

Matomo is the leading Free/Libre open analytics platform

21.6k38.2k](/packages/matomo-matomo)[illuminate/log

The Illuminate Log package.

6225.0M602](/packages/illuminate-log)[tempest/framework

The PHP framework that gets out of your way.

2.2k31.1k12](/packages/tempest-framework)[honeybadger-io/honeybadger-laravel

Honeybadger Laravel integration

431.3M](/packages/honeybadger-io-honeybadger-laravel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
