Log::Log4perl::Appender::Synchronized - Synchronizing other appenders
use Log::Log4perl qw(:easy); my $conf = qq( log4perl.category = WARN, Syncer # File appender (unsynchronized) log4perl.appender.Logfile = Log::Log4perl::Appender::File log4perl.appender.Logfile.autoflush = 1 log4perl.appender.Logfile.filename = test.log log4perl.appender.Logfile.mode = truncate log4perl.appender.Logfile.layout = SimpleLayout # Synchronizing appender, using the file appender above log4perl.category.Bogus = WARN, Logfile log4perl.appender.Syncer = Log::Log4perl::Appender::Synchronized log4perl.appender.Syncer.appender = Logfile log4perl.appender.Syncer.layout = SimpleLayout ); Log::Log4perl->init(\$conf); WARN("This message is guaranteed to be complete.");
If multiple processes are using the same Log::Log4perl appender without synchronization, overwrites might happen. A typical scenario for this would be a process spawning children, each of which inherits the parent's Log::Log4perl configuration.
Log::Log4perl
Usually, you should avoid this scenario and have each child have its own Log::Log4perl configuration, ensuring that each e.g. writes to a different logfile.
In cases where you need additional synchronization, however, use Log::Log4perl::Appender::Synchronized as a gateway between your loggers and your appenders. An appender itself, Log::Log4perl::Appender::Synchronized just takes two additional arguments:
Log::Log4perl::Appender::Synchronized
appender
Specifies the name of the appender it synchronizes access to. The appender specified must be defined somewhere in the configuration file, not necessarily before the definition of Log::Log4perl::Appender::Synchronized.
key
This optional argument specifies the key for the semaphore that Log::Log4perl::Appender::Synchronized uses internally to ensure atomic operations. It defaults to _l4p. If you define more than one Log::Log4perl::Appender::Synchronized appender, it is important to specify different keys for them, as otherwise every new Log::Log4perl::Appender::Synchronized appender will nuke previously defined semaphores.
_l4p
Log::Log4perl::Appender::Synchronized uses IPC::Shareable internally.
IPC::Shareable
Log::Log4perl::Appender::Synchronized is a composite appender. Unlike other appenders, it doesn't log any messages, it just passes them on to its attached sub-appender. For this reason, it doesn't need a layout (contrary to regular appenders). If it defines none, messages are passed on unaltered.
Custom filters are also applied to the composite appender only They are not applied to the sub-appender. Same applies to appender thresholds. This behaviour might change in the future.
Copyright 2003 by Mike Schilli, all rights reserved. This program is free software, you can redistribute it and/or modify it under the same terms as Perl itself.
2003, Mike Schilli <m@perlmeister.com>
To install Log::Log4perl, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Log::Log4perl
CPAN shell
perl -MCPAN -e shell install Log::Log4perl
For more information on module installation, please visit the detailed CPAN module installation guide.