From 11db2deb14fa12a92f5195f29feb7de374da34ed Mon Sep 17 00:00:00 2001 From: Mikko Koivunalho Date: Fri, 5 May 2023 08:52:28 +0200 Subject: [PATCH] Move caller() parts to Log::Any::Adapter::Util Signed-off-by: Mikko Koivunalho --- lib/Log/Any/Adapter/Util.pm | 21 +++++++++++++++++++++ lib/Log/Any/Proxy.pm | 5 +---- t/hooks.t | 28 +++++++++++++++------------- 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/lib/Log/Any/Adapter/Util.pm b/lib/Log/Any/Adapter/Util.pm index f93da09..ba20f6a 100644 --- a/lib/Log/Any/Adapter/Util.pm +++ b/lib/Log/Any/Adapter/Util.pm @@ -180,6 +180,27 @@ sub make_method { *{ $pkg . "::$method" } = $code; } +=sub get_correct_caller + +Return the B information. +Use this sub routine only in a hook! + +Because caller stack is dependent on Log::Any internals +we provide it here. +If you are not using this sub routine from the root of +the hook call, use parameter B to specify the number +of stack layers you have. + +=cut + +sub get_correct_caller { + my ($nr_layers) = $_[0] // 2; + return (caller $nr_layers)[0] ne 'Log::Any::Proxy' + && (caller $nr_layers)[3] eq 'Log::Any::Proxy::__ANON__' + ? [ caller $nr_layers ] + : [ caller $nr_layers + 1 ]; +} + =sub require_dynamic (DEPRECATED) Given a class name, attempts to load it via require unless the class diff --git a/lib/Log/Any/Proxy.pm b/lib/Log/Any/Proxy.pm index d429e50..b253e98 100644 --- a/lib/Log/Any/Proxy.pm +++ b/lib/Log/Any/Proxy.pm @@ -96,12 +96,9 @@ foreach my $name ( Log::Any::Adapter::Util::logging_methods(), keys(%aliases) ) return if !$self->{adapter}->$is_realname && !defined wantarray; # Execute hook: build_context - my $caller = (caller 0)[0] ne 'Log::Any::Proxy' - && (caller 0)[3] eq 'Log::Any::Proxy::__ANON__' - ? [ caller 0 ] : [ caller 1 ]; my %items; foreach my $hook (@{ $self->{hooks}->{build_context} }) { - my %i = $hook->( $realname, $self->{category}, $caller, \%items); + my %i = $hook->( $realname, $self->{category}, \%items); @items{keys %i} = @i{keys %i}; } diff --git a/t/hooks.t b/t/hooks.t index 1a7bfee..f4987b1 100644 --- a/t/hooks.t +++ b/t/hooks.t @@ -4,6 +4,7 @@ use Test::More tests => 1; use Log::Any::Adapter; use Log::Any '$log'; +use Log::Any::Adapter::Util; use FindBin; use lib $FindBin::RealBin; @@ -12,10 +13,10 @@ 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", + $log->info('(info) some info'); + $log->infof( '(infof) more %s', 'info' ); + $log->infof( '(infof) info %s %s', { with => 'data' }, 'and more text' ); + $log->debug( "(debug) program started", { progname => "foo.pl", pid => 1234, perl_version => "5.20.0" } ); } @@ -27,7 +28,8 @@ create_normal_log_lines($log); pop @{ $log->hooks->{'build_context'} }; sub build_context { - my ($lvl, $cat, $caller, $data) = @_; + my ($lvl, $cat, $data) = @_; + my $caller = Log::Any::Adapter::Util::get_correct_caller(); my %ctx; $ctx{lvl} = $lvl; $ctx{cat} = $cat; @@ -40,30 +42,30 @@ sub build_context { is_deeply( \@TestAdapters::STRUCTURED_LOG, [ - { messages => ['some info'], level => 'info', category => 'main', + { messages => ['(info) some info'], level => 'info', category => 'main', data => [ { - 'line' => 15, + 'line' => 16, 'cat' => 'main', 'lvl' => 'info', 'file' => 't/hooks.t', 'n' => 1, }], }, - { messages => ['more info'], level => 'info', category => 'main', + { messages => ['(infof) more info'], level => 'info', category => 'main', data => [ { - 'line' => 16, + 'line' => 17, 'cat' => 'main', 'lvl' => 'info', 'file' => 't/hooks.t', 'n' => 1, }], }, - { messages => ['info {with => "data"} and more text'], + { messages => ['(infof) info {with => "data"} and more text'], level => 'info', category => 'main', data => [ { - 'line' => 17, + 'line' => 18, 'cat' => 'main', 'lvl' => 'info', 'file' => 't/hooks.t', @@ -71,13 +73,13 @@ is_deeply( }, ], }, - { messages => ['program started'], + { messages => ['(debug) program started'], level => 'debug', category => 'main', data => [ { perl_version => "5.20.0", progname => "foo.pl", pid => 1234, - 'line' => 18, + 'line' => 19, 'cat' => 'main', 'lvl' => 'debug', 'file' => 't/hooks.t',