Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Integrete Exception::Class and Log::Log4perl ?

by jffry (Hermit)
on Dec 29, 2005 at 23:16 UTC ( [id://519904]=perlquestion: print w/replies, xml ) Need Help??

jffry has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

Sorry to post without researching first, but my mind is blown. I've been running the learn-integrate-decide-code mind loop on full blast lately. Stress at home doesn't help, either. Enough of my wimpy excuses. On to my pleading:

Does anyone have any idea how to integrate the Log:Log4perl (love this module, by the way!) logging with my exceptions in Exception:Class. That is, how could I take the output of

$logger->error('This is a bag thing you did!');
and put it into the 'error' attribute of my thrown exceptions? For example:
MyException::SysCmd::Fail->throw( 'error' => 'This is a bad thing you did.', );
What do I have to do? Write some sort of custom appender?

This wouldn't be a must-have. Just a nice-to-have, so I didn't code myself into a corner if this won't work out easily. Just in case you're concerned.

Thanks.

Replies are listed 'Best First'.
Re: Integrete Exception::Class and Log:Log4perl ?
by adrianh (Chancellor) on Dec 30, 2005 at 00:41 UTC
    That is, how could I take the output of $logger->error('This is a bag thing you did!'); and put it into the 'error' attribute of my thrown exceptions?

    Personally I would tend to do the reverse. As a developer I wouldn't expect a logging statement to throw an exception. Instead I would override throw() in your Exception::Class subclass to do some logging.

      Well, I would not think that putting the string 'This is a bad thing you did!' that is output by $logger->error() into the 'error' field of the thrown exception would be equivalent to having the logging statement actually throw the exception itself. I still think there is a way to write a Log4perl appender to accomplish what I want, so that my exception classes don't have to use Log4perl themselves.

      However, thanks to your suggestion I did come up with something easy. I overrode the full_message() exception object method to use Log4perl to output the 'error' field/message of the object. The message will get output again to stdout by the exception object but without all the fancy Log4perl formatting, but that is OK by me at this point.

      The other down side is that if I want my exception messages to have different logging levels (FATAL, ERR, WARN), then the exceptions must hand that off to Log4perl, also.

      #!/usr/bin/perl -w use strict; use warnings; package ThisException; { use Log::Log4perl; our $logger = Log::Log4perl->get_logger('test1'); use Exception::Class ( 'ThisException' => { description => 'Parent class.', }, ); sub full_message { my $self = shift; $logger->fatal($self->message); return $self->message; } } use Log::Log4perl; Log::Log4perl::init_and_watch('/etc/log4perl.conf', 'HUP'); our $logger = Log::Log4perl->get_logger('test1'); $logger->info('This is my INFO message!'); ThisException->throw( error => "This will be a Log4perl FATAL message.", ); exit 0;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://519904]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-25 17:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found