Click here to get back home

CGI file based logging

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
CGI file based logging howa 03-04-2008
Posted by howa on March 4, 2008, 10:59 pm
Please log in for more thread options
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

Posted by Frank Seitz on March 5, 2008, 1:33 am
Please log in for more thread options
howa wrote:
>
> 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?

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

Frank
--
Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
Anwendungen für Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel

Posted by fishfry on March 5, 2008, 1:58 am
Please log in for more thread options

> howa wrote:
> >
> > 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?
>
> 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
>


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. Writing through a logging service
is the way to solve this issue in real world, high volume applications.

Posted by Frank Seitz on March 5, 2008, 2:25 am
Please log in for more thread options
fishfry wrote:
>>howa wrote:
>>>
>>>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?
>>
>>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
>
> 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.

It is example code. The OP can do whatever he want
in case of an error.

> Losing a log message is less important than servicing the enduser.

It depends on the application.

> Writing through a logging service
> is the way to solve this issue in real world, high volume applications.

Yes, but in this case a simple solution may be more appropiate.

Frank
--
Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
Anwendungen für Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel

Posted by xhoster on March 5, 2008, 10:23 am
Please log in for more thread options
>
> > howa wrote:
> > >
> > > 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?
> >
> > 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
> >
>
> 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.

Similar ThreadsPosted
PERL can't open file for logging (world writable directory Windows XP Home/ Active Perl / Apache) November 8, 2006, 5:50 pm
how to remove or add code based on file test February 16, 2006, 7:11 am
How do i skip n number of lines in a file based on a pattern of the record July 8, 2005, 11:49 am
How Do I Locate a value in a text file and evaluate it and then write out that line based on the value? December 28, 2007, 1:11 pm
Logging UDP Packets November 5, 2004, 3:35 pm
Logging onto a Website August 27, 2007, 12:03 am
Perl LWP, logging into yahoo etc ? October 6, 2005, 12:19 pm
Logging Errors (Instead of Just Dying) November 26, 2005, 4:35 pm
[Net::Jabber] problem with logging May 15, 2006, 9:39 am
Logging into and parsing a website using Perl February 15, 2005, 10:11 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap