A logfmt formatter for Monolog
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src/Monolog/Formatter
tests/Monolog/Formatter
.gitignore
CHANGELOG.md
LICENSE
README.md
composer.json
composer.lock
phpunit.xml

README.md

monolog-logfmt

A logfmt formatter for Monolog.

Installation

The formatter can be installed using Composer:

$ composer require petert82/monolog-logfmt

Requirements

  • PHP 5.4.0 or later.

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

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

Key Content
ts Timestamp.
lvl Log level name.
chan Channel name.
msg Log message.

All of these keys can be customised by passing the appropriate parameters to the formatter's constructor. For example:

$tsKey = 'date';
$levelKey = 'level';
$channelKey = 'channel';
$msgKey = 'message';
$formatter = new LogfmtFormatter($tsKey, $levelKey, $channelKey, $msgKey);

Logs formatted using this formatter would look like this:

date=2017-11-19T19:00:00+00:00 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

License

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