CGI file based logging

H

howa

Hello,

I have a CGI which is targeted to log some messages to a file, since
Apache are running at multi-process, are there any modules which can
handle file locking issue if I received many requests?

Howard
 
X

xhoster

fishfry said:
Of course in real life you don't want your cgi to die just because it
can't obtain a lock on the logfile. It should wait and retry, or just
return a normal response to the enduser. Losing a log message is less
important than servicing the enduser.

In that case, why bother locking at all?


Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
J

John Bokma

(e-mail address removed) wrote:

[locking a log file]
In that case, why bother locking at all?

Because you don't want to mess up your log file? There is a difference
between missing a log entry and a messed up log file.
 
X

xhoster

John Bokma said:
(e-mail address removed) wrote:

[locking a log file]
In that case, why bother locking at all?

Because you don't want to mess up your log file? There is a difference
between missing a log entry and a messed up log file.

How messed up is the log file going to get? The worst I've ever seen
is one partially truncated line, which is about the same level of
badness as a missing entry in my book.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
J

John Bokma

John Bokma said:
(e-mail address removed) wrote:

[locking a log file]
In that case, why bother locking at all?

Because you don't want to mess up your log file? There is a difference
between missing a log entry and a messed up log file.

How messed up is the log file going to get? The worst I've ever seen
is one partially truncated line, which is about the same level of
badness as a missing entry in my book.

I guess we have to agree on having different books ;-)
 
H

howa

A quick search of CPAN finds Perl interfaces to Apache's logging
utility. There's the sparsely documented Apache::Log and the very
encouraging Log::Dispatch::Apache::Log which allows you to use the
sooper-dooper excellent Log Dispatch module.

The Apache::Log only allow you to write log to the apache log, but
sometimes we would want to create a different file.

Btw, I can't find Log::Dispatch::Apache::Log in CPAN...

So far I think the module: Log::Dispatch::FileShared is what I need, I
am testing it now.

Thanks.

Howard
 
P

Peter J. Holzer

Why use a module? It's simple to implement with Perl's
file locking interface. See "perldoc -f flock".

use strict;
use warnings;

use Fcntl qw/:flock/;

open my $fh,'>>','file.log' or die $!; # open file
flock $fh,LOCK_EX or die $!; # lock file
print $fh "message\n" or die $!; # write file
close $fh or die $!; # close file

At least on unixish systems locking the file isn't necessary if you can
ensure that you always write a log entry with a single write(2) system
call. PerlIO seems to do this if you open a file in raw mode:

#!/usr/bin/perl
use strict;
use warnings;
use IO::Handle;

open my $fh, ">:raw", "foo.out";
$fh->autoflush(1);

my $s = "a" x 100 . "\n";
print $fh $s;

$s = "a" x 10_000 . "\n";
print $fh $s;

$s = "a" x 1000;
print $fh $s, $s, $s, $s, "\n";
__END__

strace shows:

write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 101) = 101
write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 10001) = 10001
write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 1000) = 1000
write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 1000) = 1000
write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 1000) = 1000
write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 1000) = 1000
write(3, "\n", 1) = 1

The first and second writes correlate to the first and second print statement.
The next 5 writes correlate to the 5 arguments of the third print statement.

Remove ":raw" from th open call and the strace output looks like this:

write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 101) = 101
write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 4096) = 4096
write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 4096) = 4096
write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 1809) = 1809
write(3, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 4001) = 4001

The first print statement is again turned into single write call. But the
second string (being longer than the buffer length) is split into three
write calls, and the 5 arguments of the third print statement (being
shorter than the buffer length) are gathered into a single write call.

hp
 
B

Ben Morrow

Quoth "Peter J. Holzer said:
At least on unixish systems locking the file isn't necessary if you can
ensure that you always write a log entry with a single write(2) system
call. PerlIO seems to do this if you open a file in raw mode:

A safer way to ensure this is to use the :unix mode, which makes print
(et al) call write(2) directly.

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top