On Mon, 18 Jun 2007, Quinn Weaver wrote:

> I noticed I can't call layout() on Log::Dispatch appenders:
>
> my $syslog_appender = Log::Log4perl::appender_by_name( 'MyAppender' );
> my $layout = $syslog_appender->layout();
>
> Can't locate object method "layout" via package "Log::Dispatch::Syslog" at ...
>
> Is there any way around this?  Would it be worthwhile to write a set of
> wrappers to Log::Dispatch classes so they support the appender() method?
>
> Apologies if there is an obvious solution to this; I wasn't able to
> find one in the docs.

The "Access defined appenders" section explains this issue in detail,
what you need is not the real appender, it's the Log4perl wrapper:

  http://log4perl.sourceforge.net/d/Log/Log4perl.html#356d5

Here's some sample code:

  use Log::Log4perl;
  use Log::Log4perl::Layout;
  use Log::Log4perl::Level;

     # Define a category logger
  my $log = Log::Log4perl->get_logger("Foo::Bar");

     # Define a layout
  my $layout = Log::Log4perl::Layout::PatternLayout->new("%F-%L %m%n");

     # Define a file appender
  my $stdout_appender = Log::Log4perl::Appender->new(
                          "Log::Dispatch::Screen",
                      );

  $stdout_appender->layout($layout);

  $log->add_appender($stdout_appender);
  $log->level($INFO);

  $log->info("Waah!");

> PS:  The reason I'm using Perl (instead of the configuration file little
> language) is that I have some behavior I want to configure conditionally...
> I want to add a ScreenAppender iff a MY_DEBUG environment variable is set,
> but with the same layout as my existing Log::Dispatch::Syslog appender.

Alternatively, you could use sub { } hooks on the right hand side of
the Log4perl configuration file. Or use two different config files and
switch between them according to the env variable setting. Or read the
configuration file, perform manipulations on its content according to
the env var setting and then feed it to init() (which also takes a
string ref).

Hope that helps!

-- Mike

Mike Schilli
[EMAIL PROTECTED]

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
log4perl-devel mailing list
log4perl-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/log4perl-devel

Reply via email to