|
From: BENNING, M. <mar...@at...> - 2012-10-18 07:58:04
|
I fixed the problem for me by replacing logcroak with $logger->error("$e"); croak($e);
I also would suggest to introduce a flag to avoid breaking existing code.
Since the option will be documented in the pod the behavior should be clear.
Markus
-----Ursprüngliche Nachricht-----
Von: Mike Schilli [mailto:m...@pe...]
Gesendet: Donnerstag, 18. Oktober 2012 09:29
An: BENNING, Markus
Cc: log...@li...
Betreff: Re: [log4perl-devel] logcroak always stringifies when croak()ing
On Mon, 15 Oct 2012, BENNING, Markus (ext) wrote:
> Shouldn't logcroak log a stringified version and call croak on the
> value you passed to it instead of calling croak on the log message?
I guess that makes sense if you're using it for throwing exceptions, but I've never used it that way and that's the reason why logcroak() (and its
companions) is using Log::Log4perl::Logger::warning_render() to format the message before it passes it to croak() et al.
Of course, that's not easy to change now, because some people rely on the feature, but as a horrible hack, you could use something like
{ no warnings qw( redefine );
*Log::Log4perl::Logger::warning_render = sub {
return $_[1];
}
}
We should probably provide a flag you can set. What do you think?
> The docs say: "Finally, there's the Carp functions that do just what
> the Carp functions do, but with logging:"
Fixed!
https://github.com/mschilli/log4perl/commit/56c95a2d131678c4908785695fac0e56175c7b44
--
-- Mike
Mike Schilli
m...@pe...
> I noticed that logcroak (die,confess...) always die()s with a
> stringified version of what you pass to it.
>
> Here's an example:
>
> ---
> #!/usr/bin/env perl
>
> package Status;
>
> use Moose;
>
> use Log::Log4perl qw(:easy);
> Log::Log4perl->easy_init($ERROR);
>
> use Carp;
>
> use overload
> q{""} => sub { $_[0]->as_string },
> fallback => 1;
>
> has 'logger' => (
> is => 'ro', isa => 'Log::Log4perl::Logger', lazy => 1,
> default => sub {
> return( Log::Log4perl->get_logger('Status') );
> },
> );
>
> has code => ( is => 'ro', isa => 'Int', required => 1 ); has message
> => ( is => 'ro', isa => 'Str', required => 1 );
>
> sub as_string {
> my ($self) = @_;
> return ( sprintf( 'Status: %s (%s)', $self->message, $self->code )
> ); }
>
> sub throw {
> my $self = shift;
> croak($self);
> }
>
> sub throw_log4perl {
> my $self = shift;
> $self->logger->logcroak($self); }
>
> package main;
>
> use Data::Dumper;
>
> my $s = Status->new( code => 500, message => 'Foobar');
>
> eval { $s->throw };
> print Dumper($@);
>
> eval { $s->throw_log4perl };
> print Dumper($@);
> --- END
>
> --- Output:
> $VAR1 = bless( {
> 'message' => 'Foobar',
> 'code' => 500
> }, 'Status' );
> 2012/10/15 12:19:41 ESB Error: Foobar (500) at log4perl-test.pl line
> 50
> $VAR1 = 'ESB Error: Foobar (500) at log4perl-test.pl line 50 ';
> ---
>
|