PHPackages                             levacic/monolog-exception-with-context-processor - 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. levacic/monolog-exception-with-context-processor

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

levacic/monolog-exception-with-context-processor
================================================

A Monolog processor to automatically extract context from suitable exceptions

2.0.0(4mo ago)048.7k↑12.6%1MITPHPPHP ^8.1CI passing

Since Dec 31Pushed 4mo ago1 watchersCompare

[ Source](https://github.com/levacic/monolog-exception-with-context-processor)[ Packagist](https://packagist.org/packages/levacic/monolog-exception-with-context-processor)[ Docs](https://github.com/levacic/monolog-exception-with-context-processor)[ RSS](/packages/levacic-monolog-exception-with-context-processor/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (3)Versions (3)Used By (0)

Exception-with-context processor for Monolog
============================================

[](#exception-with-context-processor-for-monolog)

[![Packagist PHP Version Support](https://camo.githubusercontent.com/3050148152f9f44922ebb36b9d0db0bb1eb2b5ff3337a9f55e48aa480bdc583e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6c6576616369632f6d6f6e6f6c6f672d657863657074696f6e2d776974682d636f6e746578742d70726f636573736f72)](https://camo.githubusercontent.com/3050148152f9f44922ebb36b9d0db0bb1eb2b5ff3337a9f55e48aa480bdc583e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6c6576616369632f6d6f6e6f6c6f672d657863657074696f6e2d776974682d636f6e746578742d70726f636573736f72)[![Latest Stable Version](https://camo.githubusercontent.com/d942ad5a00253678cb8bbbf03341ed1c649e250f3b2a729d004fe3c31a849b15/68747470733a2f2f706f7365722e707567782e6f72672f6c6576616369632f6d6f6e6f6c6f672d657863657074696f6e2d776974682d636f6e746578742d70726f636573736f722f76)](https://packagist.org/packages/levacic/monolog-exception-with-context-processor)[![Packagist Downloads](https://camo.githubusercontent.com/7cea0f74a2cf229a8a85f30b1d7137bf348dc57c4ac829759425dbf6aaa41e7b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c6576616369632f6d6f6e6f6c6f672d657863657074696f6e2d776974682d636f6e746578742d70726f636573736f72)](https://camo.githubusercontent.com/7cea0f74a2cf229a8a85f30b1d7137bf348dc57c4ac829759425dbf6aaa41e7b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c6576616369632f6d6f6e6f6c6f672d657863657074696f6e2d776974682d636f6e746578742d70726f636573736f72)[![Packagist License](https://camo.githubusercontent.com/d6cfa65983d1413180a27e967bdc7e143a073fcf88ba3b773f861e628212c1f6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6c6576616369632f6d6f6e6f6c6f672d657863657074696f6e2d776974682d636f6e746578742d70726f636573736f72)](https://camo.githubusercontent.com/d6cfa65983d1413180a27e967bdc7e143a073fcf88ba3b773f861e628212c1f6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6c6576616369632f6d6f6e6f6c6f672d657863657074696f6e2d776974682d636f6e746578742d70726f636573736f72)[![CI](https://github.com/levacic/monolog-exception-with-context-processor/workflows/CI/badge.svg)](https://github.com/levacic/monolog-exception-with-context-processor/workflows/CI/badge.svg)[![Code Coverage](https://camo.githubusercontent.com/22be48e35dd92fc6c3d6cb57329af17d19ff054837a78af59d2c94d805ce69cf/68747470733a2f2f636f6465636f762e696f2f67682f6c6576616369632f6d6f6e6f6c6f672d657863657074696f6e2d776974682d636f6e746578742d70726f636573736f722f6272616e63682f6d61696e2f67726170682f62616467652e7376673f746f6b656e3d5550787936663354317a)](https://codecov.io/gh/levacic/monolog-exception-with-context-processor)

This package processes exceptions passed via the log record's `context` and extracts the exception chain with context into a key within the log record's `extra` section.

The chain includes all chained exceptions, but the context is only extracted from those that implement the [ExceptionWithContext](https://github.com/levacic/exception-with-context) interface.

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

[](#requirements)

- PHP ^8.1

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

[](#installation)

```
composer require levacic/monolog-exception-with-context-processor
```

Usage
-----

[](#usage)

### Configuration

[](#configuration)

The following should be done wherever you normally configure your logging infrastructure.

```
// Assuming a Monolog\Logger instance:
$monolog->pushProcessor(new ExceptionWithContextProcessor());
```

That's all it takes to setup the processor.

### Why would I do this?

[](#why-would-i-do-this)

This processor is pretty useless on its own. The main benefit of using it is when also using the [ExceptionWithContext](https://github.com/levacic/exception-with-context) interface to attach additional context to your exceptions.

This processor checks whether the log record context has an `exception` key set, and if its value is an exception (or rather, a `Throwable`). If it is, it will traverse that exception's chain (using `$exception->getPrevious()`), and extract the context for each of the exceptions in the chain that implement `ExceptionWithContext`.

The whole chain of exceptions with their contexts will be placed into an `exception_chain_with_context` key in the `extra` part of the log record. Each exception will have an `exception` key which is the class name of the exception, and a `context` key which is either the context returned by the exception if it implements `ExceptionWithContext`, or `null` otherwise.

### Example

[](#example)

Assume the following exception class:

```
