Perl (Scripting) - Getting Started with Log::Log4perl Logger

by
Jeremy Canfield |
Updated: June 01 2025
| Perl (Scripting) articles
This assumes you have installed the Log::Log4perl module on your system.
The following script is an example of how to use the logger in easy mode with easy_init. In this example, Hello World will be printed to the console.
#!/usr/bin/perl
use strict;
use warnings;
use Log::Log4perl qw(:easy);
Log::Log4perl->easy_init({
level => $INFO,
layout => '%d{MMM-dd-yyyy HH:mm:ss,SSS} %-5p %m%n'
});
my $logger = Log::Log4perl->get_logger();
$logger->info("Hello World");
Running this script should output something like this.
Sep-20-2022 00:36:21,951 INFO Hello World
And here is an example of how to use to init to load the logger using a configuration file.
#!/usr/local/bin/perl
use strict;
use warnings;
use Log::Log4perl qw(:easy);
my $conf = q(
log4perl.logger = INFO, Logfile, Screen
log4perl.appender.Logfile = Log::Log4perl::Appender::File
log4perl.appender.Logfile.filename = example.log
log4perl.appender.Logfile.mode = append
log4perl.appender.Logfile.size = 1KB
log4perl.appender.Logfile.max = 5
log4perl.appender.Logfile.DatePattern = yyyy-MM-dd
log4perl.appender.Logfile.TZ = CST
log4perl.appender.Logfile.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.Logfile.layout.ConversionPattern = %d{MM-dd-yy HH:mm:ss} %-5p [%c] %m%n
log4perl.appender.Screen = Log::Dispatch::Screen
log4perl.appender.Screen.Threshold = INFO
log4perl.appender.Screen.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.Screen.layout.ConversionPattern = %d{MM-dd-yy HH:mm:ss} %-5p [%c] %m%n
);
Log::Log4perl::init(\$conf);
my $logger = Log::Log4perl->get_logger();
$logger->info("Hello World");
More commonly, you may have the configuration in it's own file, perhaps perl_logger.conf.
log4perl.logger = INFO, Logfile, Screen
log4perl.appender.Logfile = Log::Log4perl::Appender::File
log4perl.appender.Logfile.filename = sub { return get_log_filename(); }
log4perl.appender.Logfile.mode = append
log4perl.appender.Logfile.size = 1KB
log4perl.appender.Logfile.max = 5
log4perl.appender.Logfile.DatePattern = yyyy-MM-dd
log4perl.appender.Logfile.TZ = CST
log4perl.appender.Logfile.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.Logfile.layout.ConversionPattern = %d{MM-dd-yy HH:mm:ss} %-5p [%c] %m%n
log4perl.appender.Screen = Log::Dispatch::Screen
log4perl.appender.Screen.Threshold = INFO
log4perl.appender.Screen.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.Screen.layout.ConversionPattern = %d{MM-dd-yy HH:mm:ss} %-5p [%c] %m%n
And the init the logger using the conf file.
#!/usr/local/bin/perl
use strict;
use warnings;
use Log::Log4perl qw(:easy);
Log::Log4perl::init("/path/to/perl_logger.conf");
my $logger = Log::Log4perl->get_logger();
$logger->info("Hello World");
And here is how you can have the logger append to a log file and to the console.
Log::Log4perl->easy_init(
{
level => $INFO,
layout => "%d{MMM-dd-yyyy HH:mm:ss,SSS} %-5p %m%n",
file => "STDOUT"
},
{
level => $INFO,
layout => "%d{MMM-dd-yyyy HH:mm:ss,SSS} %-5p %m%n",
file => ">>/path/to/example.log"
}
);
my $logger = Log::Log4perl->get_logger();
$logger->info("Hello World");
Did you find this article helpful?
If so, consider buying me a coffee over at