PHPackages                             open-telemetry/opentelemetry-logger-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. open-telemetry/opentelemetry-logger-monolog

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

open-telemetry/opentelemetry-logger-monolog
===========================================

OpenTelemetry Monolog handler.

1.2.0(1mo ago)91.6M↑27.8%14Apache-2.0PHPPHP ^8.1

Since Apr 26Pushed 1mo ago3 watchersCompare

[ Source](https://github.com/opentelemetry-php/contrib-logger-monolog)[ Packagist](https://packagist.org/packages/open-telemetry/opentelemetry-logger-monolog)[ Docs](https://opentelemetry.io)[ RSS](/packages/open-telemetry-opentelemetry-logger-monolog/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (9)Dependencies (30)Versions (10)Used By (14)

[![Releases](https://camo.githubusercontent.com/46e38a504120203bf7615645011bcf2bb834e03e8eb0bc8e0f4864c729fd5baf/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f72656c65617365732d707572706c65)](https://github.com/opentelemetry-php/contrib-logs-monolog/releases)[![Issues](https://camo.githubusercontent.com/b9b31135f113cdb6e2b662b4040276044ee0803567bc17688eaf4386f797ea50/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6973737565732d70696e6b)](https://github.com/open-telemetry/opentelemetry-php/issues)[![Source](https://camo.githubusercontent.com/e27dd1126a60abf1c26521d893d9f235ef342a76231c2428ddbc4651185bd626/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f736f757263652d636f6e747269622d677265656e)](https://github.com/open-telemetry/opentelemetry-php-contrib/tree/main/src/Logs/Monolog)[![Mirror](https://camo.githubusercontent.com/2f9050293ab0c0d9471e618215bc7417a63b6873c960e1605bddb69b1911fa4c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6d6972726f722d6f70656e74656c656d657472792d2d7068702d2d636f6e747269622d626c7565)](https://github.com/opentelemetry-php/contrib-logger-monolog)[![Latest Version](https://camo.githubusercontent.com/d2a4a2e3b7cfbe2aa8007ce57513bb07bbc816fa4fdca757f2a357a75a2c1b44/687474703a2f2f706f7365722e707567782e6f72672f6f70656e2d74656c656d657472792f6f70656e74656c656d657472792d6c6f676765722d6d6f6e6f6c6f672f762f756e737461626c65)](https://packagist.org/packages/open-telemetry/opentelemetry-logger-monolog/)[![Stable](https://camo.githubusercontent.com/08d7cca113c4d7333e21f7a6b2cccdd40b6a9b1dacc8289fefa4259684720453/687474703a2f2f706f7365722e707567782e6f72672f6f70656e2d74656c656d657472792f6f70656e74656c656d657472792d6c6f676765722d6d6f6e6f6c6f672f762f737461626c65)](https://packagist.org/packages/open-telemetry/opentelemetry-logger-monolog/)

This is a read-only subtree split of .

OpenTelemetry Monolog handler
=============================

[](#opentelemetry-monolog-handler)

A monolog handler for OpenTelemetry. See  for further documentation.

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

[](#requirements)

### API + SDK

[](#api--sdk)

This package depends on the OpenTelemetry API, but a configured [OpenTelemetry SDK](https://opentelemetry.io/docs/instrumentation/php/sdk/) should also be provided.

### Exporter

[](#exporter)

Usually logs are exported to a `receiver` via the `otlp` protocol in the `protobuf` format, via http or `gRPC`.

This requires:

- a `protobuf` implementation; either the protobuf extension or the `google/protobuf` package
- the `open-telemetry/exporter-otlp` package
- the `open-telemetry/transport-grpc` package, if using gRPC transport
- a PSR-7 and PSR-18 implementation, if using HTTP transport

### Receiver

[](#receiver)

Logs must be emitted to a receiver/system that understands the OpenTelemetry protocol, such as the [OpenTelemetry collector](https://opentelemetry.io/docs/collector/).

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

[](#installation)

```
composer require open-telemetry/opentelemetry-logger-monolog
```

Usage
-----

[](#usage)

The OpenTelemetry handler, configured with an OpenTelemetry `LoggerProvider`, is used to send Monolog `LogRecord`s to OpenTelemetry.

The `LoggerProvider` can be configured in a number of ways: manually, via an SDK Builder, or automatically (using environment/php.ini variables).

### Manual configuration

[](#manual-configuration)

Set up an SDK LoggerProvider and pass it to the handler:

```
$loggerProvider = new \OpenTelemetry\SDK\Logs\LoggerProvider(/* params */);
$handler = new \OpenTelemetry\Contrib\Logs\Monolog\Handler(
    $loggerProvider,
    'info',
    true,
);
```

### Automatic configuration

[](#automatic-configuration)

If you use [OpenTelemetry SDK autoloading](https://opentelemetry.io/docs/instrumentation/php/sdk/#autoloading), you can retrieve the global logger provider. That may be a no-op implementation if there was any misconfiguration.

See [autoload-sdk example](./example/autoload-sdk.php) for how to use autoloading with the OpenTelemetry SDK.

### Create a Logger

[](#create-a-logger)

Finally, add the handler to a Monolog logger:

```
$logger = new \Monolog\Logger(
    'name',
    [$handler],
);
$logger->info('hello world');
```

### Attributes Mode

[](#attributes-mode)

This OpenTelemetry handler will convert any `context` array or `extra` array in the `Monolog\LogRecord` to `OpenTelemetry\API\Logs\LogRecord` attributes. There are two options for handling conflicts between the classes.

*Note 1: Exceptions have special handling in both the PSR-3 spec and the OpenTelemetry spec. If a PHP `Throwable` is included in the `context` array with a key of `exception`, it will be added as `exception.` attributes to the OpenTelemetry Log Record.*

*Note 2: Both [Monolog](https://github.com/Seldaek/monolog/blob/main/src/Monolog/Formatter/NormalizerFormatter.php) and the [OpenTelemetry Protocol](https://github.com/open-telemetry/opentelemetry-php/blob/main/src/Contrib/Otlp/AttributesConverter.php) employ serialization algorithms when encoding attributes. This combination can lead to obtuse JSON blobs in the OTLP log records; this can be avoided by using only scalar values for attributes.*

By default, the attribute keys will be `context` and `extra`, along with `context.` and `extra.` prefixed keys with the individual array entries. Example:

```
$host = new stdClass();
$host->name = 'example.com';
$host->tcp = 80;
new Monolog\LogRecord(
    ...,
    context: [
        'foo' => 'bar',
        'baz' => 'bat',
    ],
    extra: [
        'host' => $host,
    ]
);

/**
 * becomes:
 *
 * OpenTelemetry\API\Logs\LogRecord (
 *     ...,
 *     attributes => array (
 *         context => array (
 *             foo => 'bar',
 *             baz => 'bat',
 *         )
 *         context.foo => 'bar'
 *         context.baz => 'bat'
 *         extra => array (
 *             host => array (
 *                 stdClass => array (
 *                     name => 'example.com'
 *                     tcp => 80
 *                 )
 *             )
 *         )
 *         extra.host => stdClass (
 *             name => 'example.com'
 *             tcp => 80
 *         )
 *     )
 * )
 */
```

Alternatively, if your `context` and `extra` keys do not conflict with OpenTelemetry Semantic Conventions for Attribute keys, you can set `OTEL_PHP_MONOLOG_ATTRIB_MODE=otel` and they will be sent directly as Attributes. Example:

```
new Monolog\LogRecord(
    ...,
    context: [
        'myapp.data.foo' => 'bar',
        'myapp.data.baz' => 'bat',
    ],
    extra: [
        'server.address' => 'example.com',
        'server.port' => 80,
    ]
);

/**
 * becomes:
 *
 * OpenTelemetry\API\Logs\LogRecord (
 *     ...,
 *     attributes => array (
 *         myapp.data.foo => 'bar'
 *         myapp.data.baz => 'bat'
 *         server.address => 'example.com'
 *         server.port => 80
 *      )
 * )
 */
```

###  Health Score

58

—

FairBetter than 98% of packages

Maintenance89

Actively maintained with recent releases

Popularity46

Moderate usage in the ecosystem

Community28

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

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

Recently: every ~222 days

Total

9

Last Release

55d ago

Major Versions

0.0.5 → 1.0.02023-10-17

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/4978962?v=4)[Brett McBride](/maintainers/brettmc)[@brettmc](https://github.com/brettmc)

![](https://avatars.githubusercontent.com/u/4099109?v=4)[Bob Strecansky](/maintainers/bobstrecansky)[@bobstrecansky](https://github.com/bobstrecansky)

---

Top Contributors

[![brettmc](https://avatars.githubusercontent.com/u/4978962?v=4)](https://github.com/brettmc "brettmc (5 commits)")[![ChrisLightfootWild](https://avatars.githubusercontent.com/u/106102472?v=4)](https://github.com/ChrisLightfootWild "ChrisLightfootWild (2 commits)")[![weslenteche](https://avatars.githubusercontent.com/u/54692047?v=4)](https://github.com/weslenteche "weslenteche (2 commits)")[![agoallikmaa](https://avatars.githubusercontent.com/u/3532037?v=4)](https://github.com/agoallikmaa "agoallikmaa (1 commits)")[![jerrytfleung](https://avatars.githubusercontent.com/u/8791493?v=4)](https://github.com/jerrytfleung "jerrytfleung (1 commits)")[![renovate[bot]](https://avatars.githubusercontent.com/in/2740?v=4)](https://github.com/renovate[bot] "renovate[bot] (1 commits)")[![siketyan](https://avatars.githubusercontent.com/u/12772118?v=4)](https://github.com/siketyan "siketyan (1 commits)")[![smaddock](https://avatars.githubusercontent.com/u/524425?v=4)](https://github.com/smaddock "smaddock (1 commits)")[![intuibase](https://avatars.githubusercontent.com/u/9551626?v=4)](https://github.com/intuibase "intuibase (1 commits)")[![bobstrecansky](https://avatars.githubusercontent.com/u/4099109?v=4)](https://github.com/bobstrecansky "bobstrecansky (1 commits)")[![cedricziel](https://avatars.githubusercontent.com/u/418970?v=4)](https://github.com/cedricziel "cedricziel (1 commits)")

---

Tags

logsopentelemetryotelhandlermonologopen-telemetry

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Psalm

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/open-telemetry-opentelemetry-logger-monolog/health.svg)

```
[![Health](https://phpackages.com/badges/open-telemetry-opentelemetry-logger-monolog/health.svg)](https://phpackages.com/packages/open-telemetry-opentelemetry-logger-monolog)
```

###  Alternatives

[open-telemetry/opentelemetry-auto-symfony

OpenTelemetry auto-instrumentation for Symfony

551.2M1](/packages/open-telemetry-opentelemetry-auto-symfony)[open-telemetry/opentelemetry-auto-pdo

OpenTelemetry auto-instrumentation for PDO

111.2M1](/packages/open-telemetry-opentelemetry-auto-pdo)[open-telemetry/opentelemetry-auto-wordpress

OpenTelemetry auto-instrumentation for Wordpress

17166.0k](/packages/open-telemetry-opentelemetry-auto-wordpress)

PHPackages © 2026

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