Search code examples
perllogginglog4perl

How do I make my Log::Log4perl logger work?


Here's the code I have:

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

Log::Log4perl->init({
 level => $DEBUG
});

my $logger = Log::Log4perl->get_logger("my.logger");
my $appender = Log::Log4perl::Appender->new("Log::Log4perl::Appender::File",filename => "my.file");
$appender->layout(Log::Log4perl::Layout::SimpleLayout->new);
$logger->add_appender($appender);

$logger->info("this is an info");

all I want to do is log a message to a file, and have the level show up. I understood that is what the SimpleLayout is for . I'd like to do this without a configuration file. Running the code above shows the following message:

Log::Log4perl configuration looks suspicious: No loggers defined


Solution

  • From http://log4perl.sourceforge.net/releases/Log-Log4perl/docs/html/Log/Log4perl/FAQ.html#f625e , try this approach:

    use Log::Log4perl qw(:easy);
    # Append to a log file
    Log::Log4perl->easy_init( { level   => $DEBUG,
                                file    => ">>my.file" } );