An easy-to-use library used to manage multiple Monolog channels from a single class.
This project is open source and available under the MIT License.
- PHP
^8.0
(Tested up to8.4
)
composer require bayfrontmedia/multi-logger
NOTE: All exceptions thrown by Multi-Logger extend Bayfront\MultiLogger\Exceptions\MultiLoggerException
, so you can choose to catch exceptions as narrowly or broadly as you like.
Multi-Logger exists in order manage multiple Monolog channels from a single class.
In some cases, you may still need to interact with the Monolog\Logger
object directly, and Multi-Logger allows you to do that via the getChannel method.
A Logger
instance must be passed to the constructor, and will automatically be set as the default and current channel.
To aid in consistency when referencing log channels, the Bayfront\MultiLogger\ChannelName
class contains constants with suggested channel names, including:
APP
AUDIT
CLI
DATABASE
CONTROLLER
DEV
ERROR
HEALTH
HTTP
JOB
MODEL
NOTIFICATION
OPS
PRIVILEGES
PROD
QA
QUEUE
REQUEST
RESPONSE
ROUTER
SCHEDULE
SECURITY
STAGING
STORAGE
Example:
use Bayfront\MultiLogger\ChannelName;
use Bayfront\MultiLogger\Log;
use Monolog\Logger;
use Monolog\Handler\FirePHPHandler;
$app_channel = new Logger(ChannelName::APP);
$app_channel->pushHandler(new FirePHPHandler());
$log = new Log($app_channel);
Logging events
Description:
Return array of channel names.
Parameters:
- (None)
Returns:
- (array)
Description:
Return name of default channel.
Parameters:
- (None)
Returns:
- (string)
Description:
Return name of current channel.
Parameters:
- (None)
Returns:
- (string)
Description:
Add a logger instance as a new channel with the same name.
If an existing instance exists with the same name, it will be overwritten.
Parameters:
$logger
(object):Monolog\Logger
object
Returns:
- (self)
Example:
use Bayfront\MultiLogger\ChannelName;
use Monolog\Logger;
use Monolog\Handler\FirePHPHandler;
$audit_channel = new Logger(ChannelName::AUDIT);
$audit_channel->pushHandler(new FirePHPHandler());
$log->addChannel($audit_channel);
Description:
Does channel name exist?
Parameters:
$channel
(string)
Returns:
- (bool)
Example:
if ($log->isChannel(ChannelName::APP)) {
// Do something
}
Description:
Returns Logger
instance for a given channel.
Parameters:
$channel = ''
(string): Name of channel to return. If empty string, the current channel will be returned.
Returns:
- (object):
Monolog\Logger
object
Throws:
Bayfront\MultiLogger\Exceptions\ChannelNotFoundException
Example:
try {
$app_channel = $log->getChannel(ChannelName::APP);
} catch (ChannelNotFoundException $e) {
die($e->getMessage());
}
Description:
Set the channel name to be used for the next logged event.
By default, all logged events will be logged to the default channel used in the constructor.
Parameters:
$channel
(string)
Returns:
- (self)
Throws:
Bayfront\MultiLogger\Exceptions\ChannelNotFoundException
Example:
try {
$log->channel(ChannelName::AUDIT)->info('This is an informational log message.');
} catch (ChannelNotFoundException $e) {
die($e->getMessage());
}
Description:
System is unusable.
Parameters:
$message
(string)$context
(array)
Returns:
- (void)
Description:
Action must be taken immediately.
Example: Entire website down, database unavailable, etc. This should trigger the SMS alerts and wake you up.
Parameters:
$message
(string)$context
(array)
Returns:
- (void)
Description:
Critical conditions.
Example: Application component unavailable, unexpected exception.
Parameters:
$message
(string)$context
(array)
Returns:
- (void)
Description:
Runtime errors that do not require immediate action but should typically be logged and monitored.
Parameters:
$message
(string)$context
(array)
Returns:
- (void)
Description:
Exceptional occurrences that are not errors.
Example: Use of deprecated APIs, poor use of an API, undesirable things that are not necessarily wrong.
Parameters:
$message
(string)$context
(array)
Returns:
- (void)
Description:
Normal but significant events.
Parameters:
$message
(string)$context
(array)
Returns:
- (void)
Description:
Interesting events.
Example: User logs in, SQL logs.
Parameters:
$message
(string)$context
(array)
Returns:
- (void)
Description:
Detailed debug information.
Parameters:
$message
(string)$context
(array)
Returns:
- (void)
Description:
Logs with an arbitrary level.
Parameters:
$level
(mixed)$message
(string)$context
(array)
Returns:
- (void)