stas 2002/08/30 11:08:12 Modified: src/docs/2.0/user/handlers handlers.pod Log: a PerlLogHandler example Revision Changes Path 1.18 +84 -1 modperl-docs/src/docs/2.0/user/handlers/handlers.pod Index: handlers.pod =================================================================== RCS file: /home/cvs/modperl-docs/src/docs/2.0/user/handlers/handlers.pod,v retrieving revision 1.17 retrieving revision 1.18 diff -u -r1.17 -r1.18 --- handlers.pod 30 Aug 2002 17:24:49 -0000 1.17 +++ handlers.pod 30 Aug 2002 18:08:12 -0000 1.18 @@ -894,6 +894,9 @@ -------------------------- package MyApache::TimeReset; + use strict; + use warnings; + use Apache::RequestRec (); use Apache::Const -compile => 'OK'; @@ -1051,6 +1054,9 @@ -------------------------- package MyApache::SendEmail; + use strict; + use warnings; + use Apache::RequestRec (); use Apache::RequestIO (); use Apache::RequestUtil (); @@ -1278,6 +1284,9 @@ -------------------------- package MyApache::BlockByIP; + use strict; + use warnings; + use Apache::RequestRec (); use Apache::Connection (); @@ -1586,6 +1595,9 @@ -------------------------------- package MyApache::FileExtDispatch; + use strict; + use warnings; + use Apache::Const -compile => 'OK'; use constant HANDLER => 0; @@ -1714,6 +1726,9 @@ ------------------------ package MyApache::Deparse; + use strict; + use warnings; + use Apache::RequestRec (); use Apache::RequestIO (); use B::Deparse (); @@ -1771,7 +1786,75 @@ The handler's configuration scope is C<L<DIR|docs::2.0::user::config::config/item_DIR>>. -Example: +Imagine a situation where you have to log requests into individual +files, one per user. Assuming that all requests start with +I</users/username/>, so it's easy to categorize requests by the second +URI path component. Here is the log handler that does that: + + file:MyApache/LogPerUser.pm + --------------------------- + package MyApache::LogPerUser; + + use strict; + use warnings; + + use Apache::RequestRec (); + use Apache::Connection (); + use Fcntl qw(:flock); + + use Apache::Const -compile => qw(OK DECLINED); + + sub handler { + my $r = shift; + + my($username) = $r->uri =~ m|^/users/([^/]+)|; + return Apache::DECLINED unless defined $username; + + my $entry = sprintf qq(%s [%s] "%s" %d %d\n), + $r->connection->remote_ip, scalar(localtime), + $r->uri, $r->status, $r->bytes_sent; + + my $log_path = Apache::server_root_relative($r->pool, + "logs/$username.log"); + open my $fh, ">>$log_path" or die "can't open $log_path: $!"; + flock $fh, LOCK_EX; + print $fh $entry; + close $fh; + + return Apache::OK; + } + 1; + +First the handler tries to figure out what username the request is +issued for, if it fails to match the URI, it simply returns +C<Apache::DECLINED>, letting other log handlers to do the +logging. Though it could return C<Apache::OK> since all other log +handlers will be run anyway. + +Next it builds the log entry, similar to the default I<access_log> +entry. It's comprised of remote IP, the current time, the uri, the +return status and how many bytes were sent to the client as a response +body. + +Finally the handler appends this entry to the log file for the user +the request was issued for. Usually it's safe to append short strings +to the file without being afraid of messing up the file, when two +files attempt to write at the same time, but just to be on the safe +side the handler exclusively locks the file before performing the +writing. + +To configure the handler simply enable the module with the +C<PerlLogHandler> directive, inside the wanted section, which was +I</users/> in our example: + + <Location /users/> + SetHandler perl-script + PerlResponseHandler ModPerl::Registry + PerlLogHandler MyApache::LogPerUser + Options +ExecCGI + </Location> + + =head2 PerlCleanupHandler
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]