Created
August 5, 2016 20:44
-
-
Save preaction/f84021b5170910e6454c648c6d2dae73 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
use v5.24; | |
use warnings; | |
use Log::Any; | |
use Log::Any::Adapter qw( Stdout ); | |
sub add_tags { | |
my ( $cat, $lvl, $msg ) = @_; | |
return "$msg foo=bar;baz=fuzz"; | |
} | |
$::log = Log::Any->get_logger; | |
sub foo { | |
$::log->info( "Hello" ); | |
} | |
sub bar { | |
local $::log = Log::Any->get_logger( filter => \&add_tags ); | |
foo(); | |
} | |
foo(); | |
bar(); | |
foo(); | |
This file contains hidden or 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
use v5.24; | |
use warnings; | |
$Mojolicious::VERSION = 0.0; | |
use Log::Any; | |
use Log::Any::Adapter; | |
use Log::Dispatch; | |
use Mojo::Log; | |
sub foo { | |
state $log = Log::Any->get_logger; | |
$log->info( "foo" ); | |
} | |
sub bar { | |
state $log = Log::Any->get_logger; | |
no warnings 'redefine'; | |
local *Mojo::Log::format = sub { | |
return sub { | |
my ( $time, $level, @lines ) = @_; | |
return map { "Bar: $_\n" } @lines; | |
}; | |
}; | |
foo(); | |
$log->info( 'Bar' ); | |
return sub { | |
$log->info( 'callback' ); | |
}; | |
} | |
my $mlog = Mojo::Log->new; | |
Log::Any::Adapter->set( 'MojoLog', logger => $mlog ); | |
foo(); | |
my $cb = bar(); | |
foo(); | |
# Incorrect, as the local has unwound itself | |
$cb->(); | |
This file contains hidden or 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
use v5.24; | |
use warnings; | |
$Mojolicious::VERSION = 0.0; | |
use Log::Any; | |
use Log::Any::Adapter; | |
use Log::Dispatch; | |
use Mojo::Log; | |
use Scope::Guard; | |
sub log_tag_scope { | |
my ( $log, $scope ) = @_; | |
my $old_format = $log->format; | |
my $guard = Scope::Guard->new( sub { $log->format( $old_format ) } ); | |
$log->format( sub { | |
my ( $time, $level, @lines ) = @_; | |
my $tags = join ";", map { "$_=$scope->{$_}" } keys %$scope; | |
@lines = map { "$_ $tags" } @lines; | |
$old_format->( $time, $level, @lines ); | |
} ); | |
return $guard; | |
} | |
sub log_tag_cb { | |
my ( $log, $scope, $cb ) = @_; | |
return sub { | |
my $l = log_tag_scope( $log, $scope ); | |
$cb->( @_ ); | |
}; | |
} | |
sub foo { | |
state $log = Log::Any->get_logger; | |
$log->info( "foo" ); | |
} | |
sub bar { | |
my ( $mlog ) = @_; | |
my $scope = { foo => 'bar' }; | |
my $g = log_tag_scope( $mlog, $scope ); | |
foo(); | |
$mlog->info( 'Bar' ); | |
return log_tag_cb( $mlog, $scope, sub { | |
$mlog->info( 'callback' ); | |
} ); | |
} | |
my $mlog = Mojo::Log->new; | |
Log::Any::Adapter->set( 'MojoLog', logger => $mlog ); | |
foo(); | |
my $cb = bar( $mlog ); | |
foo(); | |
$cb->(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment