parli / json-logger
PSR-3 JSON logger
Installs: 5 834
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 3
Requires
- php: ^8.0
- psr/log: ^1.0 || ^2.0 || ^3.0
Requires (Dev)
- phpstan/phpstan: ^0.12.46
- phpstan/phpstan-phpunit: ^0.12.16
- phpstan/phpstan-strict-rules: ^0.12.5
- phpunit/phpunit: ^9.3
- squizlabs/php_codesniffer: ^3.5
This package is auto-updated.
Last update: 2024-09-09 13:03:32 UTC
README
A PSR-3 JSON log formatter. This outputs a format designed around Datadog's logging system, using their predefined attributes for the structure.
Usage
This library only formats logs, and does not write them.
It must be used in conjunction with another PSR-3 which actually writes the logs somewhere useful.
(For us, this is stdout
due to deployment in a containerized environment.)
$writer = new \SomePsr3Logger(); $logger = new \Parli\JsonLogger\JsonLogger($writer); // ... $logger->error('Error message {info}', [ 'info' => $someMoreInfo, 'exception' => $someThrowable, ]);
Note: the log writer will not receive the $context
that this library receives.
This library interpolates the context into the JSON message before passing a fully-formatted JSON string to the log writer.
Exception logging
This library looks for Throwable
s in the exception
key of $context
, per PSR-3 section 1.3.
If found, it will automatically fill in the error attributes for integration with Datadog's log display system.
It is RECOMMENDED that you always pass caught exceptions to the logger's $context
(e.g. ->error('...', ['exception' => $e])
).
It is further RECOMMENDED that you do not do additional log interpolation.
Example:
try { // ... } catch (Throwable $e) { $logger->error('Caught exception in worker with input {input}', [ 'input' => $input, 'exception' => $e, ]); }
COUNTEREXAMPLE:
// Do NOT do this: try { // ... } catch (Throwable $e) { $logger->error('Caught exception in worker with input {input} - {message}: {trace}', [ 'input' => $input, 'message' => $e->getMessage(), 'trace' => $e->getTraceAsString(), 'exception' => $e, ]); }