On Fri, 18 Nov 2005, Rob Redmon wrote:
> I'm using log4perl in a process safe way using
> Log::Log4perl::Appender::Synchronized for many perl programs to all write to
> the same log file. Our project has a lot of bash code that has been written.
> I would like all of these bash scripts to send their log messages and their
> stdout and stderr to the same log file mentioned above. I need to do this so
> that log messages do not collide with one another and I cannot afford to pay
> the startup time that the following perl code takes (about 2 sec on my
> machine) for each logged message:
>
> use Log::Log4perl qw(get_logger :levels);
> Log::Log4perl->init_and_watch("log.conf", 60);
Hi Rob,
a speedier way to redirect a shell script's messages to a Log4perl appender
is using a pipe to a simple program 'logger.pl' like this:
#!/usr/bin/perl -w
# logger.pl
use strict;
use Log::Log4perl qw(:easy);
Log::Log4perl->easy_init({level => $DEBUG, file => ">file.log"});
while(<>) {
DEBUG $_;
}
and call your shell script via
shellscript.sh 2>&1 | logger.pl
Add different appenders (like Log::Log4perl::Appender::Synchronized) as
needed. Also, if you need different log levels, you could potentially
included them in the shell script's messages and have logger.pl interpret
them before sending a message to Log4perl.
Alternatively, you could have a standalone C program which takes messages
on the command line and sends them over a socket (connect, send) to a
permanently running perl daemon (using Socket::INET). From the
shell script, you would call it like
# ... bash code
clogger "This is my message"
and the permanently running logger daemon would look something like
this:
use strict;
use IO::Socket;
use Log::Log4perl qw(:easy);
Log::Log4perl->easy_init($DEBUG);
my $PORT = 9000;
my $server = IO::Socket::INET->new(
Proto => 'tcp',
LocalPort => $PORT,
Listen => SOMAXCONN,
Reuse => 1);
die "Can't start server" unless $server;
print "Server $0 accepting clients\n";
while(defined( my $client = $server->accept())) {
$client->autoflush(1);
print $client "Welcome to $0, send your message\n";
my $line = <$client>;
DEBUG "$line" if defined $line;
close $client;
}
Hope this helps.
-- Mike
Mike Schilli
m...@pe...
|