-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
User can define a function to be called at a specific time. At the moment, there is only one hook: build_context. It is called right before passing the message and structured data to the Adapter. The hook makes using the context easier. Instead of defining the context everytime when you call a logging function, e.g. `$log->debugf()`, you can use the hook to define repeating information, like file name and line or timestamp. Because the hooks support several hooked sub routines, you can add more context temporarily when you need it. User can define the hooks either when `use`ing Log::Any or at any point afterwards. There can be several subroutines for each hook. They are executed in the order the hooks are added. Examples: use Log::Any q($log), hooks => { build_context => [ \&build_context, ] }; push @{ $log->hooks->{'build_context'} }, \&build_more_context; Signed-off-by: Mikko Koivunalho <[email protected]>
- Loading branch information
Showing
4 changed files
with
138 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
use strict; | ||
use warnings; | ||
use Test::More tests => 1; | ||
|
||
use Log::Any::Adapter; | ||
use Log::Any '$log'; | ||
|
||
use FindBin; | ||
use lib $FindBin::RealBin; | ||
use TestAdapters; | ||
|
||
sub create_normal_log_lines { | ||
my ($log) = @_; | ||
|
||
$log->info('some info'); | ||
$log->infof( 'more %s', 'info' ); | ||
$log->infof( 'info %s %s', { with => 'data' }, 'and more text' ); | ||
$log->debug( "program started", | ||
{ progname => "foo.pl", pid => 1234, perl_version => "5.20.0" } ); | ||
|
||
} | ||
|
||
Log::Any::Adapter->set('+TestAdapters::Structured'); | ||
|
||
push @{ $log->hooks->{'build_context'} }, \&build_context; | ||
create_normal_log_lines($log); | ||
pop @{ $log->hooks->{'build_context'} }; | ||
|
||
sub build_context { | ||
my ($lvl, $cat, $caller, $data) = @_; | ||
my %ctx; | ||
$ctx{lvl} = $lvl; | ||
$ctx{cat} = $cat; | ||
$ctx{file} = $caller->[1]; | ||
$ctx{line} = $caller->[2]; | ||
$ctx{n} = 1; | ||
return %ctx; | ||
} | ||
|
||
is_deeply( | ||
\@TestAdapters::STRUCTURED_LOG, | ||
[ | ||
{ messages => ['some info'], level => 'info', category => 'main', | ||
data => [ { | ||
'line' => 15, | ||
'cat' => 'main', | ||
'lvl' => 'info', | ||
'file' => 't/hooks.t', | ||
'n' => 1, | ||
}], | ||
}, | ||
{ messages => ['more info'], level => 'info', category => 'main', | ||
data => [ { | ||
'line' => 16, | ||
'cat' => 'main', | ||
'lvl' => 'info', | ||
'file' => 't/hooks.t', | ||
'n' => 1, | ||
}], | ||
}, | ||
{ messages => ['info {with => "data"} and more text'], | ||
level => 'info', | ||
category => 'main', | ||
data => [ | ||
{ | ||
'line' => 17, | ||
'cat' => 'main', | ||
'lvl' => 'info', | ||
'file' => 't/hooks.t', | ||
'n' => 1, | ||
}, | ||
], | ||
}, | ||
{ messages => ['program started'], | ||
level => 'debug', | ||
category => 'main', | ||
data => [ | ||
{ | ||
perl_version => "5.20.0", progname => "foo.pl", pid => 1234, | ||
'line' => 18, | ||
'cat' => 'main', | ||
'lvl' => 'debug', | ||
'file' => 't/hooks.t', | ||
'n' => 1, | ||
} | ||
] | ||
}, | ||
], | ||
'identical output of normal log lines when using structured log adapter' | ||
); |