Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.

In reply to Avoiding Log4perl message duplication and better ways to use/cleanup code by chanakya

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-03-28 20:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found