Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Accessing Log::Log4perl Configuration Values

by whereiskurt (Friar)
on Mar 18, 2007 at 19:07 UTC ( [id://605380]=note: print w/replies, xml ) Need Help??


in reply to Accessing Log::Log4perl Configuration Values

I'm feeling some sort of 'O'Reilly tribute' <Saint Patty's just passed>

Happy St. Patty's (I'm actually a tad late, somewhere)

I have always found that to be an excellent log4perl article series, but as I refer to it I can't seem to see any reference to it. Here's a better source:

Variable Substitution (are you not a troll? :L)

" To avoid having to retype the same expressions over and over again, Log::Log4perl's configuration files support simple variable substitution. New variables are defined simply by adding

varname = value

lines to the configuration file before using

${varname}
afterwards to recall the assigned values. Here's an example:
layout_class = Log::Log4perl::Layout::PatternLayout layout_pattern = %d %F{1} %L> %m %n log4perl.category.Bar.Twix = WARN, Logfile, Screen log4perl.appender.Logfile = Log::Log4perl::Appender::File log4perl.appender.Logfile.filename = test.log log4perl.appender.Logfile.layout = ${layout_class} log4perl.appender.Logfile.layout.ConversionPattern = ${layout_patt +ern} log4perl.appender.Screen = Log::Log4perl::Appender::Screen log4perl.appender.Screen.layout = ${layout_class} log4perl.appender.Screen.layout.ConversionPattern = ${layout_patte +rn}

This is a convenient way to define two appenders with the same layout without having to retype the pattern definitions.

Update:

After rereading your post (I tend to do that) I think that perhaps this more inline with what you're getting at. This is analagous to writing log4perl eval statment - pick your favourite Templater for flavour. Kurt

Dirty Tricks Example (Sorry about the Troll comment :))

A simple example to cut-and-paste and get started:

use Log::Log4perl qw(get_logger); my $conf = q( log4perl.category.Bar.Twix = WARN, Logfile log4perl.appender.Logfile = Log::Log4perl::Appender::File log4perl.appender.Logfile.filename = test.log log4perl.appender.Logfile.layout = \ Log::Log4perl::Layout::PatternLayout log4perl.appender.Logfile.layout.ConversionPattern = %d %F{1} %L> +%m %n ); Log::Log4perl::init(\$conf); my $logger = get_logger("Bar::Twix"); $logger->error("Blah");

This will log something like:

2002/09/19 23:48:15 t1 25> Blah

to the log file 'test.log', which Log4perl will append to or create it if it doesn't exist already.

Replies are listed 'Best First'.
Re^2: Accessing Log::Log4perl Configuration Values
by valdez (Monsignor) on Mar 19, 2007 at 11:41 UTC

    Ciao whereiskurt,

    I know how to use Log4perl configuration files :)) The problem was different, let me rephrase it: I want to access l4p configuration values from outside l4p itself, how? An home-made parser is not the solution, as I should implement also variable substitution; on the other side, l4p Config classes are not so friendly. Now, I'm asking myself if the way I used the Log4perl classes is correct or not.

    Thanks, Valerio

      Valerio, after looking at your home node it's pretty clear that you would know how to use log4perl :) <kneels humbly>

      I wonder how you moved forward on this problem? It would seem there are only two ways to go: either you're building a config on the fly and using the log4perl::init call, or you're letting log4perl parse through a config already made and working with the log4perl->value madness. :)

      I wonder <outloud apparently> if slupring the 'l4p.conf' and munging it might really be your best option? I suspect that the variable substitution wouldn't be that tricky afterall... regards!

      Kurt

      PS: I realise that you said *do not* want yet another home made parser but here's one just incase anyone was interested (or incase you changed your mind :))

        Scenario: I have the same program running with different configurations; every instance of that program uses a different log4perl configuration file; what I need is a way to know where an instance logs.

        I really don't want to reinvent a l4p configuration parser: there is already one working :) In fact, what I wrote to parse a conf uses classes provided by Log4perl, but in a way probably not intended by the author, who did not release any documentation about this possible use of Log::Log4Perl::Config class and subclasses.

        Anyway, thanks again for your kind answers!

        Ciao, Valerio

Re^2: Accessing Log::Log4perl Configuration Values
by TomDLux (Vicar) on Jul 20, 2010 at 20:38 UTC

    Patty is a girl. Like Patty Hearst, for example, machine-gun and all.

    An Irish Patrick is Paddy.

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (8)
As of 2024-03-29 15:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found