2

I have Log4perl installed on my Linux machine and have the following /path/to/log4perl.conf:

log4perl.logger.Fizz = INFO, FizzAppender
log4perl.logger.Buzz = INFO, BuzzAppender

log4perl.appender.FizzAppender = Log::Dispatch::FileRotate
log4perl.appender.FizzAppender.filename = /my/logs/fizz-log.txt
log4perl.appender.FizzAppender.max = 1
log4perl.appender.FizzAppender.DatePattern = yyyy-MM-dd
log4perl.appender.FizzAppender.TZ = EST
log4perl.appender.FizzAppender.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.FizzAppender.layout.ConversionPattern = %d %m %n

log4perl.appender.BuzzAppender = Log::Dispatch::FileRotate
log4perl.appender.BuzzAppender.filename = /my/logs/buzz-log.txt
log4perl.appender.BuzzAppender.max = 1
log4perl.appender.BuzzAppender.DatePattern = yyyy-MM-dd
log4perl.appender.BuzzAppender.TZ = EST
log4perl.appender.BuzzAppender.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.BuzzAppender.layout.ConversionPattern = %d %m %n

Inside each of my perl scripts (each script uses a different appender and a logs to a different log file):

use Log::Log4perl;

my $log_conf = "/path/to/log4perl.conf";
Log::Log4perl::init($log_conf);

# In one script the logger is a "Fizz" logger (like below), and in the other
# script the logger is a "Buzz" logger.
my $logger = Log::Log4perl->get_logger("Fizz");

$logger->info("This should work.");

Even though each script uses a different appender and logs to a different file, they should all have the same behavior:

  • Each log gets rotated once a day, and old (rotated) logs gets deleted

I've had this script running for more than 24 hours now, and I don't see any logs being generated under /my/logs/.

So I ask: is my log4perl.conf incorrect? If not, what can I do to diagnose the issue? Am I initializing the library incorrectly in the perl scripts? Thanks in advance.

IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756
  • I should also note that I *only* installed the main Log4perl module, so if `Log::Dispatch::FileRotate` is not apart of `Log4perl`, then I would say that's obviously it. However I've read documentation online that suggests both ways, so I'm confused as to whether or not I need to install anything else... – IAmYourFaja Dec 20 '12 at 13:20
  • If you add `use strict` and `use warnings` to your sample code and then run only that, what errors do you get on screen? – Dancrumb Dec 20 '12 at 17:52

1 Answers1

1

I think you need to add the parameter mode to your log4perl.conf file:

log4perl.appender.FizzAppender.mode = truncate
log4perl.appender.BuzzAppender.mode = truncate

To speed things along I've changed your DatePattern to the following:

log4perl.appender.FizzAppender.DatePattern = yyyy-MM-dd-HH-MM
log4perl.appender.BuzzAppender.DatePattern = yyyy-MM-dd-HH-MM

Here's a test using your log4perl.conf file. I've created 2 copies of your Perl script:

  • log4perl_Buzz.pl
  • log4perl_Fizz.pl

I've added a loop within the scripts so that every second they log a message to their respective log files:

while (1) {
  $logger->info("This should work.");
  sleep 1;
}

I run them both at the same time and see the following results using a watch ls -l:

Every 2.0s: ls -l                    Thu Dec 20 21:16:51 2012

total 12
-rwxrwxr-x 1 saml saml 358 Dec 20 20:15 log4perl_Buzz.pl
-rw-rw-r-- 1 saml saml 978 Dec 20 21:07 log4perl.conf
-rwxrwxr-x 1 saml saml 358 Dec 20 20:15 log4perl_Fizz.pl

After I run the 2 scripts:

Every 2.0s: ls -l                    Thu Dec 20 21:17:56 2012

total 20
-rw-rw-r-- 1 saml saml  39 Dec 20 21:17 buzz-log.txt
-rw-rw-r-- 1 saml saml  39 Dec 20 21:17 fizz-log.txt
-rwxrwxr-x 1 saml saml 358 Dec 20 20:15 log4perl_Buzz.pl
-rw-rw-r-- 1 saml saml 978 Dec 20 21:07 log4perl.conf
-rwxrwxr-x 1 saml saml 358 Dec 20 20:15 log4perl_Fizz.pl

After ~2 minutes pass:

Every 2.0s: ls -l                    Thu Dec 20 21:19:05 2012

total 28
-rw-rw-r-- 1 saml saml  195 Dec 20 21:19 buzz-log.txt
-rw-rw-r-- 1 saml saml 2340 Dec 20 21:18 buzz-log.txt.1
-rw-rw-r-- 1 saml saml  234 Dec 20 21:19 fizz-log.txt
-rw-rw-r-- 1 saml saml 2301 Dec 20 21:18 fizz-log.txt.1
-rwxrwxr-x 1 saml saml  358 Dec 20 20:15 log4perl_Buzz.pl
-rw-rw-r-- 1 saml saml  978 Dec 20 21:07 log4perl.conf
-rwxrwxr-x 1 saml saml  358 Dec 20 20:15 log4perl_Fizz.pl

Here's my version of log4perl.conf for those that are playing at home 8-).

log4perl.logger.Fizz = INFO, FizzAppender
log4perl.logger.Buzz = INFO, BuzzAppender

log4perl.appender.FizzAppender = Log::Dispatch::FileRotate
log4perl.appender.FizzAppender.filename = fizz-log.txt
log4perl.appender.FizzAppender.max = 1
log4perl.appender.FizzAppender.DatePattern = yyyy-MM-dd-HH-MM
log4perl.appender.FizzAppender.TZ = EST
log4perl.appender.FizzAppender.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.FizzAppender.layout.ConversionPattern = %d %m %n
log4perl.appender.FizzAppender.mode = truncate

log4perl.appender.BuzzAppender = Log::Dispatch::FileRotate
log4perl.appender.BuzzAppender.filename = buzz-log.txt
log4perl.appender.BuzzAppender.max = 1
log4perl.appender.BuzzAppender.DatePattern = yyyy-MM-dd-HH-MM
log4perl.appender.BuzzAppender.TZ = EST
log4perl.appender.BuzzAppender.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.BuzzAppender.layout.ConversionPattern = %d %m %n
log4perl.appender.BuzzAppender.mode = truncate

These were useful resources in figuring this out:

Community
  • 1
  • 1
slm
  • 15,396
  • 12
  • 109
  • 124