macbre/monolog-utils

Additional formatters and processors for Monolog

3.1.0 2024-09-09 21:21 UTC

This package is auto-updated.

Last update: 2024-11-13 10:37:29 UTC


README

Latest Stable Version Coverage Status

Additional formatters and processors for Monolog 3.x. This package requires PHP 8.1+.

ExceptionProcessor

Allows you to pass an Exception instance as a part of $context - it will be automatically expanded to log exception class, message, code and a backtrace

try {
	// do something
}
catch (NastyException $ex) {
	$logger->error('Something bad happended', [
		'exception' => $ex
	]);
}

exception field will be expanded to something similar to:

{
	"context": {
		"exception": {
			"class": "NastyException",
			"message": "Things went wrong",
			"code": 42,
			"trace": [
				"/home/macbre/app/Foo.class.php:979",
				"/home/macbre/app/App.class.php:29",
				"/home/macbre/app/index.php:18"
			]
		}
	}
}

RequestIdProcessor

Automatically generates a unique, per request ID that is sent with every message log as request_id field in $extra.

JsonFormatter

JSON log formatter for elastic / Kibana.

An example entry:

$logger->error('Foo Bar', [
	'size' => 42
]);
{
	"@timestamp": "2023-04-18T08:25:23.123456+00:00",
	"message": "Foo Bar",
	"context": {
		"size": 42
	},
	"extra": {
		"request_id": "566c04c2f22693.59900054"
	},
	"severity": "error",
	"channel": "my.app",
	"source_host": "my.server.net"
}

Example

$logger = new Monolog\Logger('my.app');

$logger->pushProcessor(new Macbre\Logger\Processors\ExceptionProcessor());
$logger->pushProcessor(new Macbre\Logger\Processors\RequestIdProcessor());

// Syslog and JSON formatter for elastic / Kibana
$syslog = new Monolog\Handler\SyslogUdpHandler('127.0.0.1', 514, LOG_USER, Monolog\Logger::INFO);
$syslog->setFormatter(new Macbre\Logger\Formatters\JsonFormatter());
$logger->pushHandler($syslog);

// and now let's use the logger...
$logger->error('Foo Bar', [
	'exception' => new Exception('An error', 123),
	'size' => 42
]);