For many projects, I like to define a simple functional logging configuration.
sub InitLogger {
my $logfile = shift;
my $logconf = q|
log4perl.logger.[package_name] = INFO, Log
+File
log4perl.appender.LogFile = Log::Log4perl::Appende
+r::File
log4perl.appender.LogFile.filename = LOGFILE
log4perl.appender.LOG.mode = append
log4perl.appender.LogFile.layout = PatternLa
+yout
log4perl.appender.LogFile.layout.ConversionPattern = %d %5p> %
+m%n
|;
$logconf =~ s/LOGFILE/$logfile/;
Log::Log4perl->init( \$logconf );
return Log::Log4perl::get_logger("[package_name");
} # end InitLogger();
I set the log4perl.appender.LogFile.filename option to an arbitrary value 'LOGNAME' so that I can replace it in the string later. Not sure why but I've found this to be more stable than using variable substitution inside the config string itself.
If my projects grows to encompass several program files with almost identical log specs (except maybe for filename), I can pull the function into a library and share
PJ
use strict; use warnings; use diagnostics;
-
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.
|