bayfrontmedia / multi-logger
An easy-to-use library used to manage multiple Monolog channels from a single class.
Requires
- php: ^8.0
- monolog/monolog: ^3.4
This package is auto-updated.
Last update: 2024-12-16 16:38:14 UTC
README
An easy-to-use library used to manage multiple Monolog channels from a single class.
License
This project is open source and available under the MIT License.
Author
Requirements
- PHP
^8.0
Installation
composer require bayfrontmedia/multi-logger
Usage
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);
Public methods
Logging events
getChannels
Description:
Return array of channel names.
Parameters:
- (None)
Returns:
- (array)
getDefaultChannel
Description:
Return name of default channel.
Parameters:
- (None)
Returns:
- (string)
getCurrentChannel
Description:
Return name of current channel.
Parameters:
- (None)
Returns:
- (string)
addChannel
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);
isChannel
Description:
Does channel name exist?
Parameters:
$channel
(string)
Returns:
- (bool)
Example:
if ($log->isChannel(ChannelName::APP)) {
// Do something
}
getChannel
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());
}
channel
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());
}
emergency
Description:
System is unusable.
Parameters:
$message
(string)$context
(array)
Returns:
- (void)
alert
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)
critical
Description:
Critical conditions.
Example: Application component unavailable, unexpected exception.
Parameters:
$message
(string)$context
(array)
Returns:
- (void)
error
Description:
Runtime errors that do not require immediate action but should typically be logged and monitored.
Parameters:
$message
(string)$context
(array)
Returns:
- (void)
warning
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)
notice
Description:
Normal but significant events.
Parameters:
$message
(string)$context
(array)
Returns:
- (void)
info
Description:
Interesting events.
Example: User logs in, SQL logs.
Parameters:
$message
(string)$context
(array)
Returns:
- (void)
debug
Description:
Detailed debug information.
Parameters:
$message
(string)$context
(array)
Returns:
- (void)
log
Description:
Logs with an arbitrary level.
Parameters:
$level
(mixed)$message
(string)$context
(array)
Returns:
- (void)