PHPackages                             petert82/monolog-logfmt - 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. petert82/monolog-logfmt

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

petert82/monolog-logfmt
=======================

A logfmt formatter for Monolog

2.3.0(1y ago)9227.2k—2.1%10[2 PRs](https://github.com/petert82/monolog-logfmt/pulls)MITPHPPHP ^7.4 || ^8.0CI passing

Since Nov 21Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/petert82/monolog-logfmt)[ Packagist](https://packagist.org/packages/petert82/monolog-logfmt)[ RSS](/packages/petert82-monolog-logfmt/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (3)Versions (10)Used By (0)

monolog-logfmt
==============

[](#monolog-logfmt)

[![CI](https://github.com/petert82/monolog-logfmt/workflows/CI/badge.svg?event=push)](https://github.com/petert82/monolog-logfmt/workflows/CI/badge.svg?event=push)

A [logfmt](https://brandur.org/logfmt) formatter for [Monolog](https://github.com/Seldaek/monolog).

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

[](#installation)

The formatter can be installed using Composer:

```
$ composer require petert82/monolog-logfmt
```

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

[](#requirements)

- PHP 7.4.0 or later.

Usage
-----

[](#usage)

Simply set an instance of `Petert82\Monolog\Formatter\LogfmtHandler` as the formatter for the Handler that you wish to output logfmt formatted logs.

```
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Petert82\Monolog\Formatter\LogfmtFormatter;

$log = new Logger('name');
$handler = new StreamHandler('php://stdout', Logger::WARNING);
$handler->setFormatter(new LogfmtFormatter());
$log->pushHandler($handler);

$log->addError('Danger! High voltage!', ['voltage' => 9000]);
```

Running this example would output something like:

```
ts=2017-11-21T20:02:10+00:00 lvl=ERROR chan=name msg="Danger! High voltage!" voltage=9000

```

### Customisation

[](#customisation)

By default these keys will be used for the default log fields in the logfmt output:

KeyContent`ts`Timestamp.`lvl`Log level name.`chan`Channel name.`msg`Log message.All of these keys, and the format used for formatting logged `DateTimes`, can be customised by passing the appropriate parameters to the formatter's constructor. For example:

```
$tsKey = 'date';
$levelKey = 'level';
$channelKey = 'channel';
$msgKey = 'message';
$dateFormat = 'Ymd-His';
$formatter = new LogfmtFormatter($tsKey, $levelKey, $channelKey, $msgKey, $dateFormat);
```

Logs formatted using this formatter would look like this:

```
date=20171119-190000 level=INFO channel=app message=Message

```

The standard keys can also be excluded from the output by passing an empty string (or `null`) to the appropriate constructor param. For example, to include only the message:

```
$formatter = new LogfmtFormatter('', '', '', 'msg');
```

The formatter's output would now look like this:

```
msg=Message

```

### Structure Flattening

[](#structure-flattening)

By default, nested arrays and objects in the log context and extra fields are serialized as JSON strings. If you would prefer to instead flatten such structures into logfmt-compatible key-value pairs, set the `$flattenStructures` constructor parameter true:

```
// Default JSON serialization
$formatter = new LogfmtFormatter();
// Produces output like: user={"name":"John","roles":["admin","editor"]}

// With flattening enabled
$formatter = new LogfmtFormatter(
    'ts', 'lvl', 'chan', 'msg',
    DateTime::RFC3339, "\n",
    true // Enable flattening
);
// Outfut now looks like: user_name=John user_roles_0=admin user_roles_1=editor
```

Development
-----------

[](#development)

A `Makefile` is provided to test the library locally, the only requirement for this is that Docker be installed on your development machine. Simply run `make` in the project root to execute the test suite.

License
-------

[](#license)

Licensed under the MIT License - see the `LICENSE` file for details

###  Health Score

54

—

FairBetter than 97% of packages

Maintenance65

Regular maintenance activity

Popularity43

Moderate usage in the ecosystem

Community17

Small or concentrated contributor base

Maturity74

Established project with proven stability

 Bus Factor1

Top contributor holds 86.4% 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 ~447 days

Recently: every ~483 days

Total

7

Last Release

414d ago

Major Versions

1.1.0 → 2.0.02019-12-14

PHP version history (3 changes)1.0.0PHP &gt;=5.4.0

2.0.0PHP ^7.4

2.1.0PHP ^7.4 || ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/4d768e6a67335ba375ef2b20f1a1e4b1e50569b0124ddf40e18efa90ca7d0451?d=identicon)[petert82](/maintainers/petert82)

---

Top Contributors

[![petert82](https://avatars.githubusercontent.com/u/3646420?v=4)](https://github.com/petert82 "petert82 (51 commits)")[![lcobucci](https://avatars.githubusercontent.com/u/201963?v=4)](https://github.com/lcobucci "lcobucci (3 commits)")[![steggy1](https://avatars.githubusercontent.com/u/97985757?v=4)](https://github.com/steggy1 "steggy1 (2 commits)")[![derekmarcotte](https://avatars.githubusercontent.com/u/12298521?v=4)](https://github.com/derekmarcotte "derekmarcotte (1 commits)")[![dmagliola](https://avatars.githubusercontent.com/u/1516867?v=4)](https://github.com/dmagliola "dmagliola (1 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (1 commits)")

---

Tags

logloggingmonologlogfmt

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/petert82-monolog-logfmt/health.svg)

```
[![Health](https://phpackages.com/badges/petert82-monolog-logfmt/health.svg)](https://phpackages.com/packages/petert82-monolog-logfmt)
```

###  Alternatives

[inpsyde/wonolog

Monolog-based logging package for WordPress.

183617.9k7](/packages/inpsyde-wonolog)[logtail/monolog-logtail

Logtail handler for Monolog

233.2M3](/packages/logtail-monolog-logtail)[inpsyde/logzio-monolog

Logz.io integration for Monolog

191.2M1](/packages/inpsyde-logzio-monolog)[mero/yii2-monolog

The Monolog integration for the Yii framework.

42186.1k](/packages/mero-yii2-monolog)[mero/telegram-handler

Monolog handler to send log by Telegram

36113.3k](/packages/mero-telegram-handler)[lefuturiste/monolog-discord-handler

A simple monolog handler for support Discord webhooks

34111.6k4](/packages/lefuturiste-monolog-discord-handler)

PHPackages © 2026

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