PHPackages                             exileed/yii2-monolog2 - 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. exileed/yii2-monolog2

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

exileed/yii2-monolog2
=====================

Monolog2 extension for Yii2

2.0.0(4y ago)12.4kMITPHPPHP ^7.4 || ^8.0

Since Feb 14Pushed 4y agoCompare

[ Source](https://github.com/exileed/yii2-monolog2)[ Packagist](https://packagist.org/packages/exileed/yii2-monolog2)[ RSS](/packages/exileed-yii2-monolog2/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (6)Versions (7)Used By (0)

[![Latest Stable Version](https://camo.githubusercontent.com/5415eba7d4ade898ef920fbe013ff02c11b8a1e7d545fd2c24c1f5bd6ce1b776/68747470733a2f2f706f7365722e707567782e6f72672f6578696c6565642f796969322d6d6f6e6f6c6f67322f762f737461626c65)](https://packagist.org/packages/exileed/yii2-monolog2)[![Total Downloads](https://camo.githubusercontent.com/e0e50f20b02591e24565d616c08258a9178e0d337565ad80207bced0a08f8c31/68747470733a2f2f706f7365722e707567782e6f72672f6578696c6565642f796969322d6d6f6e6f6c6f67322f646f776e6c6f616473)](https://packagist.org/packages/exileed/yii2-monolog2)[![Latest Unstable Version](https://camo.githubusercontent.com/4b7beac44176f8b8640e79a1927018334b888864bc2f6fb483be2859d2c67726/68747470733a2f2f706f7365722e707567782e6f72672f6578696c6565642f796969322d6d6f6e6f6c6f67322f762f756e737461626c65)](https://packagist.org/packages/exileed/yii2-monolog2)[![License](https://camo.githubusercontent.com/945ab9a3e5d0834eb35a7caa06b45536e271a065877df1fbde1bd193da44c5da/68747470733a2f2f706f7365722e707567782e6f72672f6578696c6565642f796969322d6d6f6e6f6c6f67322f6c6963656e7365)](https://packagist.org/packages/exileed/yii2-monolog2)[![test](https://github.com/exileed/yii2-monolog2/actions/workflows/main.yml/badge.svg)](https://github.com/exileed/yii2-monolog2/actions/workflows/main.yml)

Yii2 Monolog2
-------------

[](#yii2-monolog2)

Yii2 Monolog2 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 exileed/yii2-monolog2
```

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

35

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor1

Top contributor holds 89.2% 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 ~675 days

Total

3

Last Release

1712d ago

Major Versions

1.0.1 → 2.0.02021-10-26

PHP version history (2 changes)1.0.0PHP ^7.0

2.0.0PHP ^7.4 || ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/9e92b6ee31e63ba56754db82b0fe82106c4e766dd161a0fd6c59cc4acaa23bd5?d=identicon)[exileed](/maintainers/exileed)

---

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)")[![exileed](https://avatars.githubusercontent.com/u/942898?v=4)](https://github.com/exileed "exileed (1 commits)")[![Vladnev](https://avatars.githubusercontent.com/u/11948038?v=4)](https://github.com/Vladnev "Vladnev (1 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/exileed-yii2-monolog2/health.svg)

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

###  Alternatives

[craftcms/cms

Craft CMS

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

Provides integration for Monolog with various Symfony components

2.6k203.8M358](/packages/symfony-monolog-bridge)[matomo/matomo

Matomo is the leading Free/Libre open analytics platform

21.7k38.9k](/packages/matomo-matomo)[illuminate/log

The Illuminate Log package.

6225.3M623](/packages/illuminate-log)[tempest/framework

The PHP framework that gets out of your way.

2.2k34.4k15](/packages/tempest-framework)[naoray/laravel-github-monolog

Log driver to store logs as github issues

10823.1k](/packages/naoray-laravel-github-monolog)

PHPackages © 2026

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