Log::Any::Plugin::ContextStack - Stack of context items that get prepended to each log message
version 0.012
# Set up some kind of logger. use Log::Any::Adapter; Log::Any::Adapter->set('SomeAdapter'); # Apply the Levels plugin to your logger use Log::Any::Plugin; Log::Any::Plugin->add('ContextStack'); # In your modules use Log::Any qw($log); $log->push_context('foo'); $log->info('Hello'); # [foo] Hello $log->push_context('bar'); $log->info('Hello'); # [foo:bar] Hello $log->pop_context; $log->info('Hello'); # [foo] Hello { # Must capture return value from push_scoped_context my $ctx = $log->push_scoped_context('bar', 'baz'); $log->info('Hello'); # [foo:bar:baz] Hello # $ctx goes out of scope, automatically popping baz and bar } $log->info('Hello'); # [foo] Hello
It can be useful to include some bits of context with your log messages. This plugin allows you to push and pop context on a stack, and have it automatically prepended to each log message.
Configuration values are passed as key-value pairs when adding the plugin: Log::Any::Plugin->add('ContextStack', stringify => \&my_stringifier);
Stringify function to turn the context stack into a string. Receives a single arrayref argument, and should return a single string. If there are no items on the context stack, this function won't be called. You don't need to handle the empty array case.
Default stringifier renders ['foo', 'bar'] as "[foo:bar]".
['foo', 'bar']
"[foo:bar]"
There are no methods in this package which should be directly called by the user. Use Log::Any::Plugin->add() instead.
Private method called by Log::Any::Plugin->add()
The following methods are injected into the adapter class.
Push one or more items onto the context stack.
Pop a single item off the context stack.
Push one or more items onto the context stack, and return a Scope::Guard object that pops the same amount when it goes out of scope.
Stephen Thirlwall <sdt@cpan.org>
This software is copyright (c) 2022, 2019, 2017, 2015, 2014 by Stephen Thirlwall.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Log::Any::Plugin, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Log::Any::Plugin
CPAN shell
perl -MCPAN -e shell install Log::Any::Plugin
For more information on module installation, please visit the detailed CPAN module installation guide.