PHPackages                             yiisoft/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. [Framework](/categories/framework)
4. /
5. yiisoft/log

ActiveLibrary[Framework](/categories/framework)

yiisoft/log
===========

Yii Logging Library

2.2.1(3mo ago)43966.1k↓29.1%21[3 issues](https://github.com/yiisoft/log/issues)[2 PRs](https://github.com/yiisoft/log/pulls)20BSD-3-ClausePHPPHP ^8.0CI passing

Since Feb 11Pushed 1w ago15 watchersCompare

[ Source](https://github.com/yiisoft/log)[ Packagist](https://packagist.org/packages/yiisoft/log)[ Docs](https://www.yiiframework.com/)[ GitHub Sponsors](https://github.com/sponsors/yiisoft)[ OpenCollective](https://opencollective.com/yiisoft)[ RSS](/packages/yiisoft-log/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (4)Dependencies (19)Versions (13)Used By (20)

 [ ![Yii](https://camo.githubusercontent.com/8317c17418b39410a660f5149071d26c5023c0d5fb2b7ebb771324812f666d73/68747470733a2f2f796969736f66742e6769746875622e696f2f646f63732f696d616765732f7969695f6c6f676f2e737667) ](https://github.com/yiisoft)

Yii Logging Library
===================

[](#yii-logging-library)

[![Latest Stable Version](https://camo.githubusercontent.com/d4a2a37669f13092c3b440eca26da98a85cfcae0ce132edd91b32684a74e6458/68747470733a2f2f706f7365722e707567782e6f72672f796969736f66742f6c6f672f76)](https://packagist.org/packages/yiisoft/log)[![Total Downloads](https://camo.githubusercontent.com/c2e3f865dda2c5347f7bc34c275244303f3442382e8689b93cf839c85123ba72/68747470733a2f2f706f7365722e707567782e6f72672f796969736f66742f6c6f672f646f776e6c6f616473)](https://packagist.org/packages/yiisoft/log)[![Build status](https://github.com/yiisoft/log/actions/workflows/build.yml/badge.svg)](https://github.com/yiisoft/log/actions/workflows/build.yml)[![Code coverage](https://camo.githubusercontent.com/2918c471bb817df86ee254b92d37fa652f12bdab67de394a40c7b71ac45b71eb/68747470733a2f2f636f6465636f762e696f2f67682f796969736f66742f6c6f672f67726170682f62616467652e7376673f746f6b656e3d3443535043524d47514d)](https://codecov.io/gh/yiisoft/log)[![Mutation testing badge](https://camo.githubusercontent.com/84e1247659b74c698d7a0258b06ed24289970f69aac4de7b1a11f1186f4cb330/68747470733a2f2f696d672e736869656c64732e696f2f656e64706f696e743f7374796c653d666c61742675726c3d687474707325334125324625324662616467652d6170692e737472796b65722d6d757461746f722e696f2532466769746875622e636f6d253246796969736f66742532466c6f672532466d6173746572)](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/log/master)[![static analysis](https://github.com/yiisoft/log/workflows/static%20analysis/badge.svg)](https://github.com/yiisoft/log/actions?query=workflow%3A%22static+analysis%22)[![type-coverage](https://camo.githubusercontent.com/bc472c096cd9a1bcabb3c970bce771304cd70bcb93661336e19a6768d4006a96/68747470733a2f2f73686570686572642e6465762f6769746875622f796969736f66742f6c6f672f636f7665726167652e737667)](https://shepherd.dev/github/yiisoft/log)

This package provides a [PSR-3](https://www.php-fig.org/psr/psr-3/) compatible logging library. It is used extensively in the [Yii Framework](https://www.yiiframework.com/) but it can also be used as a separate package.

The logger sends or passes messages to multiple targets. Each target may filter these messages according to their severity level, and category, and then export them to some medium such as a file, an email or a syslog.

Requirements
------------

[](#requirements)

- PHP 8.0 or higher.

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

[](#installation)

The package can be installed with [Composer](https://getcomposer.org):

```
composer require yiisoft/log
```

General usage
-------------

[](#general-usage)

Creating a logger:

```
/**
 * List of class instances that extend the \Yiisoft\Log\Target abstract class.
 *
 * @var \Yiisoft\Log\Target[] $targets
 */
$logger = new \Yiisoft\Log\Logger($targets);
```

Writing logs:

```
$logger->emergency('Emergency message', ['key' => 'value']);
$logger->alert('Alert message', ['key' => 'value']);
$logger->critical('Critical message', ['key' => 'value']);
$logger->warning('Warning message', ['key' => 'value']);
$logger->notice('Notice message', ['key' => 'value']);
$logger->info('Info message', ['key' => 'value']);
$logger->debug('Debug message', ['key' => 'value']);
```

### PSR-3 Message Placeholders

[](#psr-3-message-placeholders)

The logger is [PSR-3](https://www.php-fig.org/psr/psr-3/) compatible and supports message placeholders with additional enhancements. Placeholders in the message string are replaced with values from the context array:

```
$logger->info('User {username} logged in from {ip}', [
    'username' => 'john_doe',
    'ip' => '192.168.1.1',
]);
// Logs: "User john_doe logged in from 192.168.1.1"
```

Placeholder names must be enclosed in curly braces `{placeholder}` and correspond to keys in the context array.

#### Nested Context Values

[](#nested-context-values)

As an enhancement beyond the PSR-3 specification, the logger supports accessing nested array values using dot notation:

```
$logger->info('User {user.name} with ID {user.id} performed action', [
    'user' => [
        'id' => 123,
        'name' => 'John Doe',
    ],
]);
// Logs: "User John Doe with ID 123 performed action"
```

#### Supported Data Types

[](#supported-data-types)

Placeholders can handle various data types:

- **Strings and numbers**: Rendered as-is
- **null**: Rendered as an empty string
- **Stringable objects**: Converted using `__toString()`
- **Arrays and objects**: Rendered as formatted strings using VarDumper

```
$logger->warning('Failed to process order {order_id}', [
    'order_id' => 12345,
]);

$logger->error('Invalid data: {data}', [
    'data' => ['key' => 'value'],
]);
```

#### Context Data Preservation

[](#context-data-preservation)

The context array is preserved in the log message and can be used by log targets for filtering, formatting, or exporting. This allows you to pass structured data alongside human-readable messages:

```
$logger->info('Payment processed', [
    'amount' => 99.99,
    'currency' => 'USD',
    'transaction_id' => 'txn_123456',
    'user_id' => 42,
]);
```

### Message Flushing and Exporting

[](#message-flushing-and-exporting)

Log messages are collected and stored in memory. To limit memory consumption, the logger will flush the recorded messages to the log targets each time a certain number of log messages accumulate. You can customize this number by calling the `\Yiisoft\Log\Logger::setFlushInterval()` method:

```
$logger->setFlushInterval(100); // default is 1000
```

Each log target also collects and stores messages in memory. Message exporting in a target follows the same principle as in the logger. To change the number of stored messages, pass the `exportInterval` constructor parameter:

```
$target = new \Yiisoft\Log\StreamTarget(exportInterval: 100); // default is 1000
```

The `setExportInterval()` setter is deprecated; use the constructor parameter instead. The same applies to `setCategories()`, `setExcept()`, `setLevels()`, `setFormat()`, `setPrefix()`, `setTimestampFormat()`and `setEnabled()`.

> Note: All message flushing and exporting also occurs when the application ends.

### Logging targets

[](#logging-targets)

This package contains two targets:

- `Yiisoft\Log\PsrTarget` - passes log messages to another [PSR-3](https://www.php-fig.org/psr/psr-3/) compatible logger.
- `Yiisoft\Log\StreamTarget` - writes log messages to the specified output stream.

Extra logging targets are implemented as separate packages:

- [Database](https://github.com/yiisoft/log-target-db)
- [Email](https://github.com/yiisoft/log-target-email)
- [File](https://github.com/yiisoft/log-target-file)
- [Syslog](https://github.com/yiisoft/log-target-syslog)

### Context providers

[](#context-providers)

Context providers are used to provide additional context data for log messages. You can define your own context provider in the `Logger` constructor:

```
$logger = new \Yiisoft\Log\Logger(contextProvider: $myContextProvider);
```

Out of the box, the following context providers are available:

- `SystemContextProvider` — adds system information (time, memory usage, trace, default category);
- `CommonContextProvider` — adds common data;
- `CompositeContextProvider` — allows combining multiple context providers.

By default, the logger uses the built-in `SystemContextProvider`.

#### `SystemContextProvider`

[](#systemcontextprovider)

The `SystemContextProvider` adds the following data to the context:

- `time` — current Unix timestamp with microseconds (float value);
- `trace` — array of call stack information;
- `memory` — memory usage in bytes.
- `category` — category of the log message (always "application").

`Yiisoft\Log\ContextProvider\SystemContextProvider` constructor parameters:

- `traceLevel` — how much call stack information (file name and line number) should be logged for each log message. If the traceLevel is greater than 0, a similar number of call stacks will be logged at most. Note that only application call stacks are counted.
- `excludedTracePaths` — array of paths to exclude from tracing when tracing is enabled with `traceLevel`.

An example of custom parameters' usage:

```
$logger = new \Yiisoft\Log\Logger(
    contextProvider: new Yiisoft\Log\ContextProvider\SystemContextProvider(
        traceLevel: 3,
        excludedTracePaths: [
            '/vendor/yiisoft/di',
        ],
    ),
);
```

#### `CommonContextProvider`

[](#commoncontextprovider)

The `CommonContextProvider` allows the adding of additional common information to the log context, for example:

```
$logger = new \Yiisoft\Log\Logger(
    contextProvider: new Yiisoft\Log\ContextProvider\CommonContextProvider([
       'environment' => 'production',
    ]),
);
```

#### `CompositeContextProvider`

[](#compositecontextprovider)

The `CompositeContextProvider` allows the combining of multiple context providers into one, for example:

```
$logger = new \Yiisoft\Log\Logger(
    contextProvider: new Yiisoft\Log\ContextProvider\CompositeContextProvider(
        new Yiisoft\Log\ContextProvider\SystemContextProvider(),
        new Yiisoft\Log\ContextProvider\CommonContextProvider(['environment' => 'production'])
    ),
);
```

### Configuring `LoggerInterface` in Yii3

[](#configuring-loggerinterface-in-yii3)

In a Yii3 application, `Psr\Log\LoggerInterface` is resolved through the DI container. To use `Yiisoft\Log\Logger` as the implementation, add the binding to your application's DI config (e.g. `config/common/di/logger.php`):

```
use Psr\Log\LoggerInterface;
use Yiisoft\Definitions\ReferencesArray;
use Yiisoft\Log\Logger;
use Yiisoft\Log\StreamTarget;
use Yiisoft\Log\Target\File\FileTarget;

return [
    LoggerInterface::class => [
        'class' => Logger::class,
        '__construct()' => [
            'targets' => ReferencesArray::from([
                FileTarget::class,
                StreamTarget::class,
            ]),
        ],
    ],
];
```

Each target listed in `ReferencesArray::from()` is resolved by the DI container as a separate service. Target packages like [yiisoft/log-target-file](https://github.com/yiisoft/log-target-file) ship their own `di.php` and `params.php` configs that are merged automatically by the [config plugin](https://github.com/yiisoft/config), so `FileTarget` works out of the box with default settings (writes to `@runtime/logs/app.log` with rotation). `StreamTarget` from this package writes to `php://stdout` by default and requires no extra configuration.

To use only `StreamTarget` without the file target package:

```
use Psr\Log\LoggerInterface;
use Yiisoft\Definitions\ReferencesArray;
use Yiisoft\Log\Logger;
use Yiisoft\Log\StreamTarget;

return [
    LoggerInterface::class => [
        'class' => Logger::class,
        '__construct()' => [
            'targets' => ReferencesArray::from([
                StreamTarget::class,
            ]),
        ],
    ],
];
```

When using the [yiisoft/config](https://github.com/yiisoft/config) plugin, the shipped event configs are loaded automatically. The package provides `events-web.php` and `events-console.php` files that define event handlers to flush logs after the HTTP response is emitted and when a console command terminates.

Documentation
-------------

[](#documentation)

- Guide: [Russian - Русский](docs/guide/ru/README.md)
- [Yii guide to logging](https://github.com/yiisoft/docs/blob/master/guide/en/runtime/logging.md)
- [Internals](docs/internals.md)

If you need help or have a question, the [Yii Forum](https://forum.yiiframework.com/c/yii-3-0/63) is available. You may also check out other [Yii Community Resources](https://www.yiiframework.com/community).

License
-------

[](#license)

The Yii Logging Library is free software. It is released under the terms of the BSD License. Please see [`LICENSE`](./LICENSE.md) for more information.

Maintained by [Yii Software](https://www.yiiframework.com/).

Support the project
-------------------

[](#support-the-project)

[![Open Collective](https://camo.githubusercontent.com/a2b15f8e2268d4e3842e00d41ff7a57cce2ad8bd8d8769c5dc4fa05a546a4f62/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4f70656e253230436f6c6c6563746976652d73706f6e736f722d3765616466313f6c6f676f3d6f70656e253230636f6c6c656374697665266c6f676f436f6c6f723d376561646631266c6162656c436f6c6f723d353535353535)](https://opencollective.com/yiisoft)

Follow updates
--------------

[](#follow-updates)

[![Official website](https://camo.githubusercontent.com/d6b0929173e28cc627430d2519ca1853466a70f37395877eaf4820cb3e1e1909/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f506f77657265645f62792d5969695f4672616d65776f726b2d677265656e2e7376673f7374796c653d666c6174)](https://www.yiiframework.com/)[![Twitter](https://camo.githubusercontent.com/d077c362ac639792171af8bc002ee827816733dfc0925f70b557e6d151022226/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f747769747465722d666f6c6c6f772d3144413146323f6c6f676f3d74776974746572266c6f676f436f6c6f723d314441314632266c6162656c436f6c6f723d3535353535353f7374796c653d666c6174)](https://twitter.com/yiiframework)[![Telegram](https://camo.githubusercontent.com/4e38dd12535575c39c65bea7119b95e663abb2d1f4e3d669a27bbda07ef603f0/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f74656c656772616d2d6a6f696e2d3144413146323f7374796c653d666c6174266c6f676f3d74656c656772616d)](https://t.me/yii3en)[![Facebook](https://camo.githubusercontent.com/48204e301b34b29b0815854544f04c337fc0692096cab35e9a1f8c53a42c2307/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f66616365626f6f6b2d6a6f696e2d3144413146323f7374796c653d666c6174266c6f676f3d66616365626f6f6b266c6f676f436f6c6f723d666666666666)](https://www.facebook.com/groups/yiitalk)[![Slack](https://camo.githubusercontent.com/1a3645ba1c97e6684d0349bc478201e1621ba0d3efad516d81035364d442bad7/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f736c61636b2d6a6f696e2d3144413146323f7374796c653d666c6174266c6f676f3d736c61636b)](https://yiiframework.com/go/slack)

###  Health Score

64

—

FairBetter than 99% of packages

Maintenance89

Actively maintained with recent releases

Popularity52

Moderate usage in the ecosystem

Community44

Growing community involvement

Maturity65

Established project with proven stability

 Bus Factor3

3 contributors hold 50%+ of commits

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 ~207 days

Recently: every ~350 days

Total

10

Last Release

103d ago

Major Versions

1.0.4 → 2.0.02022-05-22

PHP version history (2 changes)1.0.0PHP ^7.4|^8.0

2.0.0PHP ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/261a6249c6f605f3956a2fae40fbb813f6b2e1e6f2bf806180c851a965426e54?d=identicon)[cebe](/maintainers/cebe)

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

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

![](https://www.gravatar.com/avatar/99106256c24a8cb23871b99fa90e48f37f1aa71608c185759b7d2a88683a5918?d=identicon)[hiqsol](/maintainers/hiqsol)

---

Top Contributors

[![samdark](https://avatars.githubusercontent.com/u/47294?v=4)](https://github.com/samdark "samdark (67 commits)")[![vjik](https://avatars.githubusercontent.com/u/525501?v=4)](https://github.com/vjik "vjik (33 commits)")[![machour](https://avatars.githubusercontent.com/u/304450?v=4)](https://github.com/machour "machour (33 commits)")[![hiqsol](https://avatars.githubusercontent.com/u/11820365?v=4)](https://github.com/hiqsol "hiqsol (24 commits)")[![devanych](https://avatars.githubusercontent.com/u/20116244?v=4)](https://github.com/devanych "devanych (23 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (8 commits)")[![terabytesoftw](https://avatars.githubusercontent.com/u/42547589?v=4)](https://github.com/terabytesoftw "terabytesoftw (7 commits)")[![xepozz](https://avatars.githubusercontent.com/u/6815714?v=4)](https://github.com/xepozz "xepozz (6 commits)")[![WarLikeLaux](https://avatars.githubusercontent.com/u/48706973?v=4)](https://github.com/WarLikeLaux "WarLikeLaux (5 commits)")[![luizcmarin](https://avatars.githubusercontent.com/u/67489841?v=4)](https://github.com/luizcmarin "luizcmarin (4 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (4 commits)")[![Copilot](https://avatars.githubusercontent.com/in/1143301?v=4)](https://github.com/Copilot "Copilot (3 commits)")[![fcaldarelli](https://avatars.githubusercontent.com/u/4108673?v=4)](https://github.com/fcaldarelli "fcaldarelli (3 commits)")[![Fantom409](https://avatars.githubusercontent.com/u/14968877?v=4)](https://github.com/Fantom409 "Fantom409 (3 commits)")[![sankaest](https://avatars.githubusercontent.com/u/21160342?v=4)](https://github.com/sankaest "sankaest (2 commits)")[![viktorprogger](https://avatars.githubusercontent.com/u/7670669?v=4)](https://github.com/viktorprogger "viktorprogger (1 commits)")[![olegbaturin](https://avatars.githubusercontent.com/u/15981018?v=4)](https://github.com/olegbaturin "olegbaturin (1 commits)")[![damasco](https://avatars.githubusercontent.com/u/1377554?v=4)](https://github.com/damasco "damasco (1 commits)")[![dood-](https://avatars.githubusercontent.com/u/10099592?v=4)](https://github.com/dood- "dood- (1 commits)")[![DplusG](https://avatars.githubusercontent.com/u/11989901?v=4)](https://github.com/DplusG "DplusG (1 commits)")

---

Tags

hacktoberfestlogloggerloggingpsr-3yii3logpsr-3frameworkloggeryii

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm, Rector

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/yiisoft-log/health.svg)

```
[![Health](https://phpackages.com/badges/yiisoft-log/health.svg)](https://phpackages.com/packages/yiisoft-log)
```

###  Alternatives

[laravel/framework

The Laravel Framework.

34.8k543.8M20.1k](/packages/laravel-framework)[symfony/symfony

The Symfony PHP framework

31.4k87.2M2.2k](/packages/symfony-symfony)[cakephp/cakephp

The CakePHP framework

8.9k19.5M1.8k](/packages/cakephp-cakephp)[shopware/platform

The Shopware e-commerce core

3.4k1.5M3](/packages/shopware-platform)[tempest/framework

The PHP framework that gets out of your way.

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

The Telegram bot library that doesn't drive you nuts

737290.3k8](/packages/nutgram-nutgram)

PHPackages © 2026

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