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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.