Skip to content
master
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 

README.md

monolog-logfmt

A logfmt formatter for Monolog.

Installation

The formatter can be installed using Composer:

$ composer require petert82/monolog-logfmt

Requirements

  • PHP 7.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, 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

License

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

About

A logfmt formatter for Monolog

Resources

License

Packages

No packages published

Languages

You can’t perform that action at this time.