Dear Monks,

Log::Log4perl is my preferred tool for logging and this time I want to access some values of its configuration from another application. I've read the documentation, but I didn't find any suitable method; so I started from parsing a configuration file, but very soon I realized that I also needed the variable expansion feature provided by Log4perl; after that I dived into the source and came back with the following class to accomplish the task:

package Log::Log4perl::Config::Standalone; use Log::Log4perl::Config; use Log::Log4perl::Config::PropertyConfigurator; use Carp; sub new { my ($class, $filename) = @_; confess "configuration '$filename' not found\n" unless -f $filename; my $cp = Log::Log4perl::Config::PropertyConfigurator->new; $cp->file( $filename ); my $self = { data => $cp->parse() }; return bless $self, $class; } sub value { my ($self, $path, $default) = @_; my @p = split /\./, $path; shift @p if $p[0] eq 'log4perl'; my $found = 1; my $r = $self->{data}; while (my $n = shift @p) { if (exists $r->{$n}) { $r = $r->{$n}; } else { $found = 0; } } return $found ? $r->{value} : $default; } 1; __END__
Given the following configuration file:
# filename: l4p.conf global_var = /var/log/some/file.log log4perl.rootLogger = DEBUG, LOGFILE log4perl.appender.LOGFILE = Log::Log4perl::Appender::File log4perl.appender.LOGFILE.filename = ${global_var} log4perl.appender.LOGFILE.mode = append log4perl.appender.LOGFILE.layout = PatternLayout log4perl.appender.LOGFILE.layout.ConversionPattern = %d{ISO8601} [%P] +%C %p %m%n
this code retrieves a value:
use Log::Log4perl::Config::Standalone; my $c = Log::Log4perl::Config::Standalone->new('./l4p.conf'); print $c->value('log4perl.appender.LOGFILE.filename'), "\n";
What do you think about it? How would you solve the same problem?

Thanks, Valerio


In reply to Accessing Log::Log4perl Configuration Values by valdez

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.