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

Avoiding Log4perl message duplication and better ways to use/cleanup code

by chanakya (Friar)
on Jul 26, 2009 at 16:09 UTC ( [id://783344]=perlquestion: print w/replies, xml ) Need Help??

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

Greetings All,

I'm introducing Logging mechanism into my application using Log::Log4perl
Below is the structure of my application.
bin/ conf/ lib/ABC::Parser.pm lib/ABC::Parser2.pm lib/ABC::Parser3.pm
I'm able to get the log mechanism working using the module, but i'm not able to get the "DEBUG"
statements working and also facing "message duplication" issues in the log file

First let me show my code so that I can tell what I want to achieve

log4perl.conf
[log4perl] log4perl.logger= INFO, Logfile, ERROR, DEBUG, Screen log4perl.appender.Logfile = Log::Log4perl::Appender::File log4perl.appender.Logfile.filename = sub { return get_log(); } log4perl.appender.Logfile.mode = append log4perl.appender.Logfile.layout = Log::Log4perl::Layout::PatternLayou +t log4perl.appender.Logfile.layout.ConversionPattern = %d %p> %m%n log4perl.appender.ERROR= Log::Log4perl::Appender::File log4perl.appender.ERROR.filename = sub { return get_log(); } log4perl.appender.ERROR.mode = append log4perl.appender.ERROR.layout = Log::Log4perl::Layout::PatternLayout log4perl.appender.ERROR.layout.ConversionPattern = %d %p> %m%n log4perl.appender.DEBUG= Log::Log4perl::Appender::File log4perl.appender.DEBUG.filename = sub { return get_log(); } log4perl.appender.DEBUG.mode = append log4perl.appender.DEBUG.layout = Log::Log4perl::Layout::PatternLayout log4perl.appender.DEBUG.layout.ConversionPattern = %d %p> %m%n log4perl.appender.Screen = Log::Log4perl::Appender::Screen log4perl.appender.Screen.stderr = 0 log4perl.appender.Screen.layout = Log::Log4perl::Layout::PatternLayout log4perl.appender.Screen.layout.ConversionPattern = %d %p> %m%n [Modules level setup] log4perl.logger.main = INFO log4perl.logger.ABC::Parser = INFO log4perl.logger.ABC::Parser1 = INFO log4perl.logger.ABC::Parser2 = INFO
ABC::Parser/pm
package ABC::Parser; use strict; use warnings; use File::Slurp 'read_file'; use Data::Dumper; use Log::Log4perl qw(:easy); Log::Log4perl::init( "../conf/log4perl.conf" ); my $logger = Log::Log4perl::get_logger("ABC::Parser"); sub get_data($) { my ($params) = @_; my $raw_file = $params->{file}; my @raw_data = read_file($raw_file, err_mode => 'carp'); my $data = {}; $logger->info("done parsing"); $logger->warn("error statement"); $logger->debug("DEbug statement"); return $data; } 1;
test_parser.pl
#!/usr/bin/perl -w use strict; use Getopt::Long; use Log::Log4perl qw(:easy); use ABC::Parser; use vars qw($debug); GetOptions( "debug|x" => \$debug, "help|h" => \&usage, ); ##Initializations Log::Log4perl::init( "../conf/log4perl.conf" ); my $logger = Log::Log4perl::get_logger(); my $data = ABC::Parser::get_data({file => "/tmp/test.data"}); $logger->error("could not get parsed data") if(!$data); $logger->info("Created output"); $logger->debug("$data") if ($debug); sub get_log{ use File::Basename; use POSIX qw(strftime); my $now_string = strftime("%Y-%m-%d", localtime); my $log = sprintf "%s.$now_string.info.log", basename( $0, '.pl' ) +; return $log; } __END__
Below is the script output
-bash-3.00$ ./test_parser.pl 2009/07/26 06:13:29 INFO> Starting ./test_parser.pl 2009/07/26 06:13:29 INFO> done parsing 2009/07/26 06:13:29 WARN> error 2009/07/26 06:13:29 INFO> Created output file
Below is the output written to the log file, which has repeated statements
2009/07/26 06:13:29 INFO> Starting ./parse_xheader.pl 2009/07/26 06:13:29 INFO> Starting ./parse_xheader.pl 2009/07/26 06:13:29 INFO> Starting ./parse_xheader.pl 2009/07/26 06:13:29 INFO> done parsing 2009/07/26 06:13:29 INFO> done parsing 2009/07/26 06:13:29 INFO> done parsing 2009/07/26 06:13:29 WARN> error 2009/07/26 06:13:29 WARN> error 2009/07/26 06:13:29 WARN> error 2009/07/26 06:13:29 INFO> Created output file 2009/07/26 06:13:29 INFO> Created output file 2009/07/26 06:13:29 INFO> Created output file
Below are the changes which I want to carry out, please let me know how to achieve this

* Cleanup the log4perl.conf, so that all INFO, ERROR and DEBUG statements go into a single log file
without the "message duplication", which is happening currently.
* How to specify the "logDirectory" path to a perl variable in log4perl.conf, so that this can be easily changed.
* I want to enable "DEBUG" statements, when the user passes "-d" parameter to the perl script (or there own perl scr and also log them to the log file
* How to enable "DEBUG" statements in "Parser2.pm" by default.

Thanks in advance for your time.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-04-20 06:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found