Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

log4perl

Status
Not open for further replies.

Jboy123

MIS
Jun 8, 2004
24
0
0
CH
Hi All

I recently started investigating using log4perl - which is great! I, rather disappointingly, also discovered that the majority of output that I generate from my scripts comes from either system calls or running of external code (e.g. java).

Is there any way I can get the output from these calls back into log4perl for handling?

Any help would be greatly appreciated.

Thanks
Jules
 
You can get the output back as an array using
Code:
my @files = qx[ls -al];
Once it's in the array, you can do whatever you want with it, including logging.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Hi Steve!

Many thanks for the tip. It's not 100% what I was looking for for but definitely a step in the right direction and will certainly work for me - so thanks! I was hoping to find a realtime solution, I guess with this one you have to wait until the external call completes before logging. (But not the end of the world).

One question though. I wanted to be able to try to determine the log level by evaluating the return code. Any ideas?

What I did so far was something like this
Code:
sub extCmd {
	# some code here
	# some more here
	my @output = qx{some command here};
        return @output;
}
and then in the caller script I just put the output straight back into log4perl (via $logger) as follows:
Code:
my @return      = extCmd(some args here);
foreach my $line(@return){
	chomp($line);
	$logger->info("$line");
}
As I say I'd like to be able to determine the return code of the command in sub extCmd and then set a log level accordingly. What do think might be best way?

Your help is much appreciated! Thanks

Jules
 
Hi Kevin

Many thanks for the response. I have read the links you posted (in fact, the second was my intro to log4perl :). I think I'm fairly comfortable with what log4perl can do in terms of log levels, appenders, logger initialisation, re-reading etc.

Perhaps I misunderstood something or missed something in the docs but I couldn't see anything to cover the situation I tried to cover above - where your code is not purely perl but relies on externals calls (be they shell/java/perl).

Maybe the question I asked in my last post is more a general perl question than log4perl specifically? Thanks to Steve I am able to get the ouput of my external calls back into log4perl via my $logger but I wanted to verify the return code from the external script so as to determine the correct log4perl category.

Any pointers would be very welcome. And, of course if there is a more direct way to achieve this in log4perl then I'd be very happy if you could pass a link.

Many thanks in advance for any assistance!
Jules
 
Hi all

I managed to get the return code back to the call script and can now verify the return code and then set logging accordingly. See below:

Code:
sub extCmd {
	# some code here
	# some more here
	my @output = qx{some command here};	
	my $exval = $?;
        
	push (@output, $exval);
	return @output;
}

In caller script:
Code:
$logger->info("calling sub extCmd");
my @return	= extCmd(some args here);
my $rc		= pop(@return);
if ($rc != 0){
	$logger->error("script called in sub extCmd returned an error");
} else {
	$logger->info("script called in sub extCmd completed OK");
}

foreach my $line(@return){
	chomp($line);
	$logger->debug("$line");
}

I'm sure this could be improved but currently works, which is the main thing.

I have one small problem. Where if something happens, like the command to be executed in sub extCmd, is not available. This automatically gets sent to stderr and then output to screen, I'd like to be able to handle that with log4perl and prevent the e.g. shell message appearing on screen
e.g sh: /my/path/to/file: No such file or directory

Any ideas? Many thanks as always.

Jules
 
Hi All

The issue above is actually causing me more hassle than I first thought. It looks like when I do the following:

Code:
$logger->info("calling sub extCmd");
my @return    = extCmd(some args here);
my $rc        = pop(@return);
if ($rc != 0){
    $logger->error("script called in sub extCmd returned an error");
} else {
    $logger->info("script called in sub extCmd completed OK");
}

foreach my $line(@return){
    chomp($line);
    $logger->debug("$line");
}

And extCmd consists of e.g. qx{cp $fileA $fileA.bak}

If $fileA cannot be found instead of sending any output to my @return I get nothing other than an ugly message printed to screen. So looks like STDERR just goes straight to screen.

Does anyone know how to supress this and just have it written to @return array?? Steve?

As always thanks in advance.
Jules
 
Code:
qx{cp $fileA $fileA.bak 2>&1};
might do it...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Many thanks for the response, Steve.

I had actually already tried that one and sadly it doesn't work either. The error message still gets displayed and worse the error code returned is 0.

So - many thanks anyway but still stumped so far :)

 
Steve

I'm an idiot - your suggestion above does of course work.....my testing was flawed.... :)

I stupidly put the following when testing. Doh!
Code:
qx{cp $fileA $fileA.bak > 2>&1};
and not of course:
Code:
qx{cp $fileA $fileA.bak 2>&1};

Thanks!

Works perfectly well now!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top