On Fri, 6 Mar 2009, Ronald Fischer wrote:

> I understand how to do it in a config file, but I don't see how to do
> it in my code. Probably it is not possible at all with easy_init
> (which I would like to continue to use)

easy_init() and init() are using the same underlying internal Log4perl
API functions, so it doesn't matter which one you're using.

>    # This is my current initialization
>    Log::Log4perl->easy_init(
>      # ... Details left out
>    );
>    # Now define the request-specific logger
>
>    use Log::Log4perl::Layout;
>    use Log::Log4perl::Level;
>    my $req_logger= Log::Log4perl->get_logger(???);

Since you don't want categories, use the root logger here:

      my $req_logger= Log::Log4perl->get_logger("");


>    my $req_appender =  Log::Log4perl::Appender->new(
>       "Log::Log4perl::Appender::File",
>       name => "???",

You can omit the name.

>       mode => 'clobber',
>       utf8 => 1,
>       create_at_logtime => 1
>       filename  => \&get_current_logfile); # ???

You don't want to pass a reference here, you want to call the function
right here:

        filename  => get_current_logfile() );

and define it like

     sub get_current_logfile {
         return "logfile.dat";
     }

or whatever fance logic you want it to use, but it needs to return the
name of the file.

So, a working version of what you want would look something like this:

     use strict;
     use Log::Log4perl qw(:easy);

     # This is my current initialization
     Log::Log4perl->easy_init($DEBUG);

     use Log::Log4perl::Layout;
     use Log::Log4perl::Level;
     my $req_logger= Log::Log4perl->get_logger("");
     my $req_appender =  Log::Log4perl::Appender->new(
        "Log::Log4perl::Appender::File",
        mode => 'clobber',
        utf8 => 1,
        create_at_logtime => 1,
        filename  => get_current_logfile());
     $req_appender->layout(Log::Log4perl::Layout::SimpleLayout->new());
     $req_logger->add_appender($req_appender);

     DEBUG "waah";

     ###########################################
     sub get_current_logfile {
     ###########################################
        return "logfile.dat";
     }

One more thing: A logger (the root logger in your case) can only have one 
level. So if you initialize with easy_init($DEBUG), that's the level it's 
gonna have. If you want different levels for the file appender you're 
setting up manually, use appender thresholds or a filter.

Hope that helps!

-- Mike

Mike Schilli
m...@perlmeister.com

------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
log4perl-devel mailing list
log4perl-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/log4perl-devel

Reply via email to