Write to my root/logs from Perl script?

software development

#1

Well, since sendmail’d logs can take hours to arrive (see my previous post, topic=“sendmail Delays for Hours”), I was wondering if I could write to my root/logs, the way the perl compiler does when it encounters compile errors. I would like to log run-time variables so I can debug execution errors.

I found this in the list of perl modules supposedly installed on DreamHost:

http://search.cpan.org/~dougm/Apache-LogFile-0.12/LogFile.pm

Is that the tool I should use? If so, what is the correct incantation to log, say, “Hello, World!”?

I tried the various incantations suggested in that doc and none of them worked. From searching this forum it appears that I don’t have access to my httpd.conf. Also,

use Apache::LogFile ;

generated an error that it could not even find that module. What did I do wrong there?

Thanks!

Jerry


#2

You’re going about it the wrong way.

[code]#!/usr/local/bin/perl

use CGI;

my $query = new CGI;

print
$query->header() .
$query->start_html() .
$query->h1(‘Error Test’) .
$query->end_html();

print STDERR “This is an error.”;

exit;[/code]
:cool: [color=#6600CC]Atropos[/color] | openvein.org


#3

Thanks, I got it. “You just write to stderr, dummy”.

Surprisingly, this doesn’t stamp it with the process name or timestamp, so I wrote a little sub that does. It seems like this should be built in somewhere, but, oh, well…

#!/usr/bin/perl

use CGI ; # Scripts work on DreamHost without this

Set global logging level to a number from 0-5.

0 logs everything (debugging)

my $globalLoggingLevel = 2 ;

Print “OK” reply back to client

print “content-type: text/plain\n\n”;
print “OK, it worked!”, “\n” ;

printLog (0, “Emergency Message to system log”) ;
printLog (1, “Critical Message to system log”) ;
printLog (2, “Normal Message to system log”) ;
printLog (3, “Unimportant Message to system log”) ;
printLog (4, “Trivial Message to system log”) ;

exit ;

sub printLog {
my $loggingLevel = shift ;
if ($loggingLevel <= $globalLoggingLevel) {
my $rawMsg = shift ;

create $dateTime

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
= localtime(time) ;
my $dateTime
= sprintf(“ddd.d:d:d”,
$year+1900,$mon+1,$mday,$hour,$min,$sec) ;

my $scriptName = “scriptName” ;

my $scriptName = $ENV{‘SCRIPT_NAME’} ;

my $wrappedMsg = “$dateTime $scriptName: $rawMsg\n” ;
print stderr $wrappedMsg ;
}
}


#4

Rather than reinventing the wheel, why not locally install something like:

http://search.cpan.org/~dconway/Log-StdLog-v0.0.3/