mod_perl logo perl icon
no previous pagepage up: What is mod_perl?no next page

Log Handler Example






Writing Apache Modules with Perl and C

Writing Apache Modules with Perl and C

By Lincoln Stein, Doug MacEachern
Embedding Perl in HTML with Mason

Embedding Perl in HTML with Mason

By Dave Rolsky, Ken Williams
mod_perl2 User's Guide

mod_perl2 User's Guide

By Stas Bekman, Jim Brandt
Practical mod_perl

Practical mod_perl

By Stas Bekman, Eric Cholet
The mod_perl Developer's Cookbook

The mod_perl Developer's Cookbook

By Geoffrey Young, Paul Lindner, Randy Kobes
mod_perl Pocket Reference

mod_perl Pocket Reference

By Andrew Ford


Table of Contents

Creating a PerlLogHandler

Every request phase can be controlled using mod_perl. Here's an example of a PerlLogHandler. The PerlLogHandler is one of the last phases of the request cycle.

This example sends mail when a request is made to the /private section of your web space. A more common use of a PerlLogHandler might be to track hits on a specific set of URLs, or to write logging data to a relational database.

  file:My/Notify.pm
  ------------------------
  package My::Notify;
  use strict;
  use Apache::Constants qw(:common);

  use Mail::Send;

  sub handler {
      my $r = shift;

      my $email = $r->server->server_admin || return DECLINED;

      my $mail = Mail::Send->new(
          To      => $email,
          Subject => "mod_perl Notification",
      );
      my $file = $r->filename;
      my $fh = $mail->open;
      $fh->print("File '$file' was accessed");
      $fh->close;

      return DECLINED; # let apache write to the lot
  }
  1; # modules must return true

The httpd.conf setup:

  <Location /private>
      SetHandler perl-script
      PerlLogHandler My::Notify
  </Location>
« back




TOP
no previous pagepage up: What is mod_perl?no next page