PHPackages                             beter/yii2-beter-logging - 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. beter/yii2-beter-logging

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

beter/yii2-beter-logging
========================

Bulletproof logging for enterprise yii2 projects: monolog 2.x integration, custom LogTarget with delayed queue, pretty console handler, logstash via udp and tcp with deep yii2 integration.

2.0.2(3y ago)66.7k↑50%2[1 PRs](https://github.com/BETER-CO/yii2-beter-logging/pulls)1MITPHPPHP &gt;=7.4

Since May 22Pushed 2y ago2 watchersCompare

[ Source](https://github.com/BETER-CO/yii2-beter-logging)[ Packagist](https://packagist.org/packages/beter/yii2-beter-logging)[ RSS](/packages/beter-yii2-beter-logging/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (4)Versions (11)Used By (1)

yii2-beter-logging
==================

[](#yii2-beter-logging)

Bulletproof logging for enterprise yii2 projects.

monolog 2.x integration, custom LogTarget with delayed queue, pretty console handler, logstash via udp and tcp with deep yii2 integration.

[![CLI colors](https://raw.githubusercontent.com/BETER-CO/yii2-beter-logging/master/doc/assets/cli_colors.jpg)](https://raw.githubusercontent.com/BETER-CO/yii2-beter-logging/master/doc/assets/cli_colors.jpg)

Features:

- uses monolog under the hood;
- implements custom [log Target](https://www.yiiframework.com/doc/api/2.0/yii-log-target) to pass log entries to monolog;
- allows handlers chaining if logstash handler fails, so no more loses of log entries;
- allows tracking statistics of log handlers;
- adds [log context](doc/logging-with-context.md) for messages and exceptions;
- doesn't turn off the whole log Target on errors;
- allows switching off handlers if they fail specific amount of times;
- supports stdout/stderr with colors;
- supports logstash tcp and udp transport;
- native support of yii log features.

Related packages you need to try:

- [beter/exception-with-context](https://packagist.org/packages/beter/exception-with-context) is used by `yii2-beter-logging` package to bring support of context data storing in the exception. [Check the doc](doc/logging-with-context.md).
- [yii2-log-request-response](https://packagist.org/packages/beter/yii2-log-request-response) package contains component the component that logs user info, request related info, headers, execution time and so on for requests and response in CLI scripts and WEB.

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

[](#installation)

The preferred way to install this extension is through [composer](https://getcomposer.org/).

Either run

```
composer require beter/yii2-beter-logging

```

or add

```
"beter/yii2-beter-logging": "~1.0.0" // add the latest version

```

to the require section of your composer.json.

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

[](#configuration)

To use this extension, you have to configure it in your application configuration.

### Configure Yii2 log component

[](#configure-yii2-log-component)

Add `Beter\Yii2\Logging\ProxyLogTarget` class to the list of your log Targets

```
'log' => [
    'traceLevel' => YII_DEBUG ? 3 : 0,
    'flushInterval' => 1,
    'targets' => [
        // other log targets

        'monolog-proxy' => [
            'class' => Beter\Yii2\Logging\ProxyLogTarget::class,
            'targetLogComponent' => [
                'componentName' => 'monolog',
                'logChannel' => 'main'
            ],
            'exportInterval' => 1,
            'categories' => [],
            'except' => [
                'yii\web\UrlManager::parseRequest',
                'yii\web\UrlRule::parseRequest',
            ],
            'levels' => ['error', 'warning', 'info', 'trace'],
        ],

        // other log targets
    ],
]

```

`ProxyLogTarget` extends [yii\\log\\Target](https://www.yiiframework.com/doc/api/2.0/yii-log-target) and supports all settings that yii\\log\\Target has, but with limitations:

- `exportInterval` must be 1. `yii2-beter-logging` always resets this setting to 1 and notifies about this. See [details](#further-reading).
- `logVars`, `maskVars` and `prefix` settings will be ignored and will be reset to empty arrays and null values. See [details](#further-reading).

The only additional setting is `targetLogComponent` section. This is not standard setting for yii2 log Target. This section is mandatory and it connects `Beter\Yii2\Logging\ProxyLogTarget` with `Beter\Yii2\Logging\MonologComponent`.

`Beter\Yii2\Logging\MonologComponent` may be configured with few monolog channels, but `ProxyLogTarget`requires to specify the only one.

> If you need more monolog channels you may setup few `ProxyLogTarget`'s.

Check further doc sections for more details.

### Configure MonologComponent

[](#configure-monologcomponent)

Log Target passes log entries to `Beter\Yii2\Logging\MonologComponent` and then `Beter\Yii2\Logging\MonologComponent` passes them to [`Monolog\Logger`](https://github.com/Seldaek/monolog/blob/2.x/doc/01-usage.md).

`Beter\Yii2\Logging\MonologComponent` configures custom handlers shipped with `yii2-beter-logging`.

The list of handlers supported:

- `logstash`
- `standard_stream`
- `firephp`

So, configure `Beter\Yii2\Logging\MonologComponent` class. Don't forget to use *the same name of the component and monolog channel name* as was specified in `targetLogComponent` setting of the `Beter\Yii2\Logging\ProxyLogTarget` (`"monolog"` and `"main"` in this example).

```
'monolog' => [
    'class' => Beter\Yii2\Logging\MonologComponent::class,
    'channels' => [
        'main' => [
            'handler' => [
                [
                    'name' => 'logstash',
                    'label' => 'logstash',
                    'level' => 'debug',
                    'bubble' => true,
                    'host' => '1.2.3.4', // or host.address.com
                    'port' => 5045,
                    'socket_transport' => 'tcp',
                    'persistent' => false,
                    'socket_timeout' => 1,
                    'writing_timeout' => 1,
                    'connection_timeout' => 1,
                    'max_handle_errors_before_disabling' => 3,
                    'formatter' => [
                        'name' => 'logstash',
                        'trace_depth' => 10,
                    ]
                ],
                [
                    'name' => 'standard_stream',
                    'stream' => 'php://stderr',
                    'level' => 'debug',
                    'bubble' => true,
                    'formatter' => [
                        'name' => 'console',
                        'colorize' => true,
                        'indentSize' => 2,
                        'trace_depth' => 10,
                    ]
                ],
                [
                    'name' => 'firephp',
                    'bubble' => false,
                    'formatter' => [
                        'name' => 'wildfire',
                    ]
                ],
            ],
            'processor' => [
                [
                    'name' => 'basic_processor',
                    'env' => YII_ENV, // dev, prod, etc
                    'app' => 'myapp',
                    'service' => 'api',
                    'exec_type' => 'web', // or cli, or etc
                    'host' => gethostname(), // or set it as you want
                ],
                [
                    'name' => 'correlation_id_processor',
                    'length' => 32,
                    'search_in_headers' => true, // true if not set explicitly
                    'header_name' => 'X-Request-Id', // 'X-Request-Id' if not set explicitly
                ],
            ],
        ],
    ],
],

```

### Configure boostrap order

[](#configure-boostrap-order)

To not lose any log entries you need to [boostrap](https://www.yiiframework.com/doc/guide/2.0/en/structure-applications#bootstrap)`Beter\Yii2\Logging\MonologComponent` before any other component. `log` component must be the second component in the list.

```
// other settings

'bootstrap' => [
    'monolog',
    'log',
    'authManager',
    // other components
],

// other settings

```

### Other configurations

[](#other-configurations)

Don't forget to configure all your environments like `cli` and so on.

Usage
-----

[](#usage)

Just use `log` component and `Yii::log()`-related methods as usual

```
Yii::error('Error here', 'application'); // application is a category name
or
Yii::info('Info here', __METHOD__); // if you call from method

```

Further reading
---------------

[](#further-reading)

- [Development and testing](doc/development-and-testing.md)
- [Logging in CLI](doc/logging-in-cli.md)
- [Logging with a context](doc/logging-with-context.md)

TBD:

- Handlers, Formatters and Processors
- Delayed errors
- Extending

###  Health Score

32

—

LowBetter than 69% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity29

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 97.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 ~30 days

Recently: every ~57 days

Total

9

Last Release

1260d ago

Major Versions

1.2.2 → 2.0.02022-06-14

PHP version history (2 changes)1.0.0PHP &gt;=7.2.5

2.0.2PHP &gt;=7.4

### Community

Maintainers

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

---

Top Contributors

[![WoZ](https://avatars.githubusercontent.com/u/85790?v=4)](https://github.com/WoZ "WoZ (42 commits)")[![verpeta](https://avatars.githubusercontent.com/u/2664508?v=4)](https://github.com/verpeta "verpeta (1 commits)")

---

Tags

logloggerlogginglogstashmonologyiiyii-monologyii2yii2-extensionyii2-loggeryii2-logstashyii2-monologlogconsoleloggeryii2yiimonologlogstashyii2-logyii2-loggeryii2 monologyii monologyii logstash

### Embed Badge

![Health badge](/badges/beter-yii2-beter-logging/health.svg)

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

###  Alternatives

[matomo/matomo

Matomo is the leading Free/Libre open analytics platform

21.7k38.9k](/packages/matomo-matomo)[craftcms/cms

Craft CMS

3.6k3.6M3.1k](/packages/craftcms-cms)[tempest/framework

The PHP framework that gets out of your way.

2.2k34.4k15](/packages/tempest-framework)[illuminate/log

The Illuminate Log package.

6225.3M622](/packages/illuminate-log)[flow-php/flow

PHP ETL - Extract Transform Load - Data processing framework

85036.3k](/packages/flow-php-flow)[index0h/yii2-log

Many Yii2 log targets

48210.0k](/packages/index0h-yii2-log)

PHPackages © 2026

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