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

Updated:   |  Perl (Scripting) articles

This assumes you have installed the Log::Log4perl module on your system.

The following script is a example of how to use the logger in easy mode. In this example, Hello World will be printed to the console.

#!/usr/bin/perl
use strict;
use warning;
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 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 Buy Me A Coffee



Comments


Add a Comment


Please enter c3df18 in the box below so that we can be sure you are a human.