-
Channels, (*8)
Creative usage of channels. Handler
and Processor
now can be bound to
different channels, also with channel name globbing., (*9)
By default, handlers and processors are bound to '*'
channel which globs
to all. But they also can be bound to channels like 'user.*'
or more
specific one 'user.login'
., (*10)
// bind to 'user.*' channels
$logger->addHandler('warning', new LogfileHandler('/log/user.log'), 'user.*');
// bind to 'system.*' channels
$logger->addHandler('error', new LogfileHandler('/log/system.log'), 'system.*');
// add user info only in 'user.*' channel
$logger->addProcessor(new UserProcessor(), 'user.*');
log messages can be sent to specific channels by using of with()
in front
of any logging related methods, such as log()
, warning()
etc., (*11)
$logger->with('user.login')->info('user {user.username} logged info');
The info()
method in the previous code will trigger user info being
inserted into context array by the UserProcessor
and being logged to file
/log/user.log
., (*12)
Note: Channel names are case insensitive., (*13)
Note: Same handler or processor can be bound to different channels. But
will be executed only ONCE in one log call., (*14)
With the support for logging to different channels, there is no need to
create multiple loggers in one application. By carefully designing your
channel hierachy, you may use one logger through out your site., (*15)
-
Priority, (*16)
Handlers and processors are now can injected into the logger with different
priorities (range from -100
to 100
, default is 0
)., (*17)
- Higher priority means executed first
// add user info at first
$logger->addProcessor(new UserProcessor(), 'user.*', 100);
// interpolate should be done last (just before executing handlers)
$logger->addProcessor(new InterpolateProcessor(), '*', -100);
- First in first out(executed) for same priority
Default priority value is 0
. The following handlers executed in the
order of their addition., (*18)
// log to file first
$logger->addHandler('debug', new LogfileHandler('/log/log.txt'));
// then log to mail
$logger->addHandler('debug', new MailHandler('admin@my.com'));
-
Simple callable interface, (*19)
Handlers, formatters, processors are now all using the single interface, (*20)
public function __invoke(LogEntryInterface $logEntry);
Which means, user may use predefined functions or other callables to servce
as handler, formatter or processor, as long as these callables take the
LogEntryInterface
as the parameter., (*21)
A quick handler as follows,, (*22)
function myHandler(LogEntryInterface $logEntry) {
// get formatted message
$formatted = $logEntry->getFormatted();
// write to my device ...
}
Or even,, (*23)
$logger->addHandler('error', function($log) {
// write the log to my device
}, 'user.*');
-
LogEntry, (*24)
In stead of using array as data type for the log message. The
LogEntryInterface
is defined to serve as default data type for logs., (*25)
You may even extend the LogEntry
class, and use it in your logger, (*26)
class MyLogEntry extends LogEntry
{
// ...
}
Use it in your logger as the prototype for all log messages,, (*27)
$entryPrototype = new MyLogEntry('channel','debug', 'message');
$logger = new Logger('MyApp', $entryPrototype);
-
LoggerInterface
related, (*28)
See PSR-3 for standard related APIs., (*29)
-
Phossa2\Logger\Logger
related, (*30)
__construct(string $defaultChannel = 'LOGGER', LogEntryInterface $logPrototype = null)
Create the logger., (*31)
with(string $channel): $this
Specify the channel for the comming logging method., (*32)
addHandler(string $level, callable $handler, string $channel = '*', int $priority = 0): $this
Add one handler to specified channel with the priority., (*33)
addProcessor(callable $processor, string $channel = '*', int $priority = 0): $this
Add one processor to specified channel with the priority., (*34)
removeHandler(callable|string $handlerOrClassname, $channel = '')
Remove the handler (or specify handler's classname) from the specified
channel. If $channel
is empty, then remove from all channels., (*35)
removeProcessor(callable|string $processorOrClassname, $channel = '')
Remove the processor (or specify processor's classname) from the specified
channel. If $channel
is empty, then remove from all channels., (*36)