Utilities for and Loggers for PSR-3: Logger Interface.
- LogLevelFilter - LogLevelFilter is a PSR Logger that filters logs based on the log level. It can be used to filter out log messages that are not needed for a specific use case.
- LogLevelLoggerMux - LogLevelLoggerMux is a PSR Logger that multiplexes logs to different loggers based on the log level.
- LoggerVerbosityFilter - LoggerVerbosityFilter mutes log messages based on a given integer verbosity level.
- LoggerWithContext - LoggerWithContext is a logger that adds a given context to all log messages before delegating to another logger.
- MemoryLogger - MemoryLogger is a PSR Logger that stores all logs in local memory.
- MultiLogger - MultiLogger is a PSR Logger that delegates logs to multiple other loggers.
- StreamResourceLogger - StreamResourceLogger is a PSR Logger that writes to a stream resource.
- psr/log: ^1 || ^2 || ^3
- php: ^7.4 || ^8.0
Install the latest version with:
composer require 'corpus/loggers'
This example demonstrates how the loggers may be chained together to create complex interactions
<?php
use Corpus\Loggers\LoggerWithContext;
use Corpus\Loggers\LogLevelFilter;
use Corpus\Loggers\MemoryLogger;
use Corpus\Loggers\MultiLogger;
use Corpus\Loggers\StreamResourceLogger;
use Psr\Log\LogLevel;
require __DIR__ . '/../vendor/autoload.php';
$memoryLogger = new MemoryLogger;
$cliLogger = new StreamResourceLogger(
fopen('php://output', 'w')
);
$logger = new MultiLogger(
new LogLevelFilter(
(new LoggerWithContext($memoryLogger))->withContext([ 'Logger' => 'Number 1' ]),
[ Psr\Log\LogLevel::INFO ]
),
new LogLevelFilter(
(new LoggerWithContext($cliLogger))->withContext([ 'Logger' => 'Number 2' ]),
[ Psr\Log\LogLevel::INFO, LogLevel::DEBUG ]
),
new LogLevelFilter(
(new LoggerWithContext($cliLogger))->withContext([ 'Logger' => 'Number 3' ]),
[ Psr\Log\LogLevel::INFO, LogLevel::DEBUG ],
true // reverse filter - only log levels NOT in the array
)
);
$logger->info('Hello World', [ 'hello' => 'world' ]);
$logger->debug('How are you?', [ 'foo' => 'bar' ]);
$logger->error('I am fine', [ 'bar' => 'baz', 'baz' => 'qux' ]);
echo "\n--- dumping memory logger ---\n\n";
var_export($memoryLogger->getLogs());
2023-11-30T05:34:35+00:00 info: Hello World
Logger: 'Number 2'
hello: 'world'
2023-11-30T05:34:35+00:00 debug: How are you?
Logger: 'Number 2'
foo: 'bar'
2023-11-30T05:34:35+00:00 error: I am fine
Logger: 'Number 3'
bar: 'baz'
baz: 'qux'
--- dumping memory logger ---
array (
0 =>
array (
'level' => 'info',
'message' => 'Hello World',
'context' =>
array (
'Logger' => 'Number 1',
'hello' => 'world',
),
),
)
function withContext(array $context) : self
Returns a new instance with the given context
replacing the existing context.
function withAddedContext(array $context) : self
Returns a new instance with the given context
added to the existing context.
function withAdditionalLoggers(\Psr\Log\LoggerInterface ...$loggers) : self
withAdditionalLoggers returns a new instance with the given loggers
added to the list of loggers to delegate to.
function withAdditionalLoggers(\Psr\Log\LoggerInterface ...$loggers) : self
withAdditionalLoggers returns a new instance with the given loggers
added to the list of loggers to delegate to.
function withContext(array $context) : self
Returns a new instance with the given context
replacing the existing context.
function withAddedContext(array $context) : self
Returns a new instance with the given context
added to the existing context.
LoggerVerbosityFilter mutes log messages based on a given integer verbosity level.
By default,
- level 0 logs no messages.
- level 1 logs emergency, alert, critical, and error messages.
- level 2 logs emergency, alert, critical, error, warning, and notice messages.
- level 3 logs emergency, alert, critical, error, warning, notice, and info messages.
- level 4 or greater logs emergency, alert, critical, error, warning, notice, info, and debug messages.
The levels can be changed by passing a callback redefine the verbosity level for each log level.
The verbosity level can be changed by calling withVerbosity()
function __construct(\Psr\Log\LoggerInterface $logger [, int $verbosity = 0 [, ?callable $verbosityFromLevelCallback = null]])
- callable | null
$verbosityFromLevelCallback
- A callback that takes a Psr\Log\LogLevel log level string and returns an integer verbosity level. If null, the default callback will be used.
function withVerbosity(int $verbosity) : self
Returns a new instance with the specified verbosity level.
function withVerbosityFromLevelCallback(callable $verbosityFromLevelCallback) : self
Returns a new instance with the specified verbosity level callback.
LoggerWithContext is a logger that adds a given context to all log messages before delegating to another logger.
This is useful for adding context to all log messages, such as the current request ID, IP address or the current user ID.
function __construct(\Psr\Log\LoggerInterface $logger [, array $context = []])
Create a new LoggerWithContext instance with the given logger and context.
The given context will be added to all log messages.
- \Psr\Log\LoggerInterface
$logger
- The logger to delegate to. - array
$context
- The context to add to all log messages.
function withContext(array $context) : self
Returns a new instance with the given context
replacing the existing context.
function withAddedContext(array $context) : self
Returns a new instance with the given context
added to the existing context.
LogLevelFilter is a PSR Logger that filters logs based on the log level.
It can be used to filter out log messages that are not needed for a specific use case.
For example, you may want to log all messages at the DEBUG level to a file, but only log messages at the ERROR level or higher to the console.
This logger can be used to filter out the DEBUG messages from the console logger.
This logger accepts a list of log levels to filter, and a boolean indicating whether to exclude or include the given log levels.
function __construct(\Psr\Log\LoggerInterface $logger, array $levels [, bool $exclude = false])
- string[]
$levels
- The log levels to filter. - bool
$exclude
- Whether to exclude the given levels, or include them.
LogLevelLoggerMux is a PSR Logger that multiplexes logs to different loggers based on the log level.
function __construct([ ?\Psr\Log\LoggerInterface $defaultLogger = null [, ?\Psr\Log\LoggerInterface $emergencyLogger = null [, ?\Psr\Log\LoggerInterface $alertLogger = null [, ?\Psr\Log\LoggerInterface $criticalLogger = null [, ?\Psr\Log\LoggerInterface $errorLogger = null [, ?\Psr\Log\LoggerInterface $warningLogger = null [, ?\Psr\Log\LoggerInterface $noticeLogger = null [, ?\Psr\Log\LoggerInterface $infoLogger = null [, ?\Psr\Log\LoggerInterface $debugLogger = null]]]]]]]]])
- \Psr\Log\LoggerInterface | null
$defaultLogger
- The default logger to use for levels where no other logger is specified. If null, a Psr\Log\NullLogger will be used.
function withEmergencyLogger(\Psr\Log\LoggerInterface $logger) : self
Returns a new instance with the specified logger handling the Emergency log level.
function withAlertLogger(\Psr\Log\LoggerInterface $logger) : self
Returns a new instance with the specified logger handling the Alert log level.
function withCriticalLogger(\Psr\Log\LoggerInterface $logger) : self
Returns a new instance with the specified logger handling the Critical log level.
function withErrorLogger(\Psr\Log\LoggerInterface $logger) : self
Returns a new instance with the specified logger handling the Error log level.
function withWarningLogger(\Psr\Log\LoggerInterface $logger) : self
Returns a new instance with the specified logger handling the Warning log level.
function withNoticeLogger(\Psr\Log\LoggerInterface $logger) : self
Returns a new instance with the specified logger handling the Notice log level.
function withInfoLogger(\Psr\Log\LoggerInterface $logger) : self
Returns a new instance with the specified logger handling the Info log level.
function withDebugLogger(\Psr\Log\LoggerInterface $logger) : self
Returns a new instance with the specified logger handling the Debug log level.
MemoryLogger is a PSR Logger that stores all logs in local memory.
This is primarily useful for testing purposes.
<?php
namespace Corpus\Loggers;
class MemoryLogger {
public const KEY_LEVEL = 'level';
public const KEY_MESSAGE = 'message';
public const KEY_CONTEXT = 'context';
}
function getLogs() : array
getLogs returns all logs that have been logged to this logger.
- MemoryLogger::KEY_LEVEL : The log level
- MemoryLogger::KEY_MESSAGE : The log message
- MemoryLogger::KEY_CONTEXT : The log context
- array[]
function clearLogs() : void
clearLogs clears all logs that have been logged to this logger.
MultiLogger is a PSR Logger that delegates logs to multiple other loggers.
function __construct(\Psr\Log\LoggerInterface ...$loggers)
Create a new MultiLogger instance with the given loggers to delegate to.
function withAdditionalLoggers(\Psr\Log\LoggerInterface ...$loggers) : self
withAdditionalLoggers returns a new instance with the given loggers
added to the list of loggers to delegate to.
StreamResourceLogger is a PSR Logger that writes to a stream resource.
This is particularly useful for writing to STDERR or STDOUT, or to a file.
function __construct($resource)
- resource
$resource
- Writable stream resource
Throws: \Corpus\Loggers\Exceptions\LoggerArgumentException
- If the given resource is not a stream
Throws: \Corpus\Loggers\Exceptions\LoggerInitException
- If the given resource is not writable