Question

I'm trying to add and remove a simple file appender to a Log4perl logger, but warnings are generated and nothing is printed. Here is my code:

use strict;
use warnings;
use Log::Log4perl qw(:easy);
Log::Log4perl->easy_init($DEBUG);
use Log::Log4perl::Appender::File;

my $app = Log::Log4perl::Appender::File->new(
    filename  => "C:/users/my name/Desktop/someFile.txt",
    mode      => 'clobber',
    utf8      => 1,
);
# print "appender name is " . $app->name();
my $logger = get_logger();
$logger->add_appender($app);
INFO('info');
$logger->remove_appender($app->name);

The file "someFile.txt" is created but is empty. The warnings generated are:

Odd number of elements in hash assignment at C:/strawberry/perl/site/lib/Log/Log4perl/Appender/File.pm line 223.
Use of uninitialized value in print at C:/strawberry/perl/site/lib/Log/Log4perl/Appender/File.pm line 245.

"info", with a timestamp, is also printed to the command line. Can anyone see what error I am making?

Was it helpful?

Solution

Are you sure that an appender is what you want? If so then you shouldn't be creating a new Log::Log4perl::Appender::File object directly - you create a Log::Log4perl::Appender object and pass the class for the type of appender you want as the first parameter to the constructor. Like this:

my $app = Log::Log4perl::Appender->new(
    'Log::Log4perl::Appender::File',
    filename  => 'C:/users/my name/Desktop/someFile.txt',
    mode      => 'clobber',
    utf8      => 1,
);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top