Appender::Synchronized(3pm) User Contributed Perl Documentation Appender::Synchronized(3pm)
NAME
Log::Log4perl::Appender::Synchronized - Synchronizing other appenders
SYNOPSIS
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.appender.Syncer = Log::Log4perl::Appender::Synchronized
log4perl.appender.Syncer.appender = Logfile
);
Log::Log4perl->init(\$conf);
WARN("This message is guaranteed to be complete.");
DESCRIPTION
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.
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::Appen-
der::Synchronized" as a gateway between your loggers and your appenders. An appender
itself, "Log::Log4perl::Appender::Synchronized" just takes two additional arguments:
"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 defi-
nition of "Log::Log4perl::Appender::Synchronized".
"key"
This optional argument specifies the key for the semaphore that "Log::Log4perl::Appen-
der::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. The maximum key length is four characters, longer keys will be truncated
to 4 characters -- "mylongkey1" and "mylongkey2" are interpreted to be the same:
"mylo" (thanks to David Viner <dviner AT yahoo-inc.com> for pointing this out).
"Log::Log4perl::Appender::Synchronized" uses "IPC::Shareable" internally to perform lock-
ing with semaphores provided by the operating system used.
Performance tips
The "Log::Log4perl::Appender::Synchronized" serializes access to a protected resource
globally, slowing down actions otherwise performed in parallel.
Unless specified otherwise, all instances of "Log::Log4perl::Appender::Synchronized"
objects in the system will use the same global IPC key "_l4p".
To control access to different appender instances, it often makes sense to define differ-
ent keys for different synchronizing appenders. In this way, Log::Log4perl serializes
access to each appender instance separately:
log4perl.category = WARN, Syncer
# File appender 1 (unsynchronized)
log4perl.appender.Logfile1 = Log::Log4perl::Appender::File
log4perl.appender.Logfile1.filename = test1.log
log4perl.appender.Logfile1.layout = SimpleLayout
# File appender 2 (unsynchronized)
log4perl.appender.Logfile2 = Log::Log4perl::Appender::File
log4perl.appender.Logfile2.filename = test2.log
log4perl.appender.Logfile2.layout = SimpleLayout
# Synchronizing appender, using the file appender above
log4perl.appender.Syncer1 = Log::Log4perl::Appender::Synchronized
log4perl.appender.Syncer1.appender = Logfile1
log4perl.appender.Syncer1.key = l4p1
# Synchronizing appender, using the file appender above
log4perl.appender.Syncer2 = Log::Log4perl::Appender::Synchronized
log4perl.appender.Syncer2.appender = Logfile2
log4perl.appender.Syncer2.key = l4p2
Without the ".key = l4p1" and ".key = l4p2" lines, both Synchronized appenders would be
using the default "_l4p" key, causing unnecessary serialization of output written to dif-
ferent files.
DEVELOPMENT NOTES
"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.
LEGALESE
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.
AUTHOR
2003, Mike Schilli <m AT perlmeister.com>
perl v5.8.4 2004-06-22 Appender::Synchronized(3pm)
Generated by $Id: phpMan.php,v 4.49 2006/02/26 13:18:18 chedong Exp $ Author: Che Dong
On Apache/2.0.61 (Unix) PHP/4.4.7 mod_ssl/2.0.61 OpenSSL/0.9.7e mod_fastcgi/2.4.2 DAV/2 SVN/1.4.2
Under GNU General Public License
2009-01-06 17:26 @38.103.63.57 Crawled by CCBot/1.0 (+http://www.commoncrawl.org/bot.html)