Description

Apache2::Log provides the Perl API for Apache logging methods.

Depending on the the current LogLevel setting, only logging with the same log level or higher will be loaded. For example if the current LogLevel is set to warning, only messages with log level of the level warning or higher (err, crit, elert and emerg) will be logged. Therefore this:

It will be logged only if the server log level is set to info or debug. LogLevel is set in the configuration file, but can be changed using the $s->loglevel() method.

The filename and the line number of the caller are logged only if Apache2::Const::LOG_DEBUG is used (because that's how Apache 2.0 logging mechanism works).

Note: On Win32 Apache attempts to lock all writes to a file whenever it's opened for append (which is the case with logging functions), as Unix has this behavior built-in, while Win32 does not. Therefore Apache2::Log functions could be slower than Perl's print()/warn().

Constants

Log level constants can be compiled all at once:

use Apache2::Const -compile => qw(:log);

or individually:

use Apache2::Const -compile => qw(LOG_DEBUG LOG_INFO);

LogLevel Constants

The following constants (sorted from the most severe level to the least severe) are used in logging methods to specify the log level at which the message should be logged:

Apache2::Const::LOG_EMERG

Apache2::Const::LOG_ALERT

Apache2::Const::LOG_CRIT

Apache2::Const::LOG_ERR

Apache2::Const::LOG_WARNING

Apache2::Const::LOG_NOTICE

Apache2::Const::LOG_INFO

Apache2::Const::LOG_DEBUG

Other Constants

Make sure to compile the APR status constants before using them. For example to compile APR::Const::SUCCESS and all the APR error status constants do:

use APR::Const -compile => qw(:error SUCCESS);

Here is the rest of the logging related constants:

Apache2::Const::LOG_LEVELMASK

used to mask off the level value, to make sure that the log level's value is within the proper bits range. e.g.:

$loglevel &= LOG_LEVELMASK;

Apache2::Const::LOG_TOCLIENT

used to give content handlers the option of including the error text in the ErrorDocument sent back to the client. When Apache2::Const::LOG_TOCLIENT is passed to log_rerror() the error message will be saved in the $r's notes table, keyed to the string "error-notes", if and only if the severity level of the message is Apache2::Const::LOG_WARNING or greater and there are no other "error-notes" entry already set in the request record's notes table. Once the "error-notes" entry is set, it is up to the error handler to determine whether this text should be sent back to the client. For example:

warn

General Functions

LOG_MARK

Though looking like a constant, this is a function, which returns a list of two items: (__FILE__, __LINE__), i.e. the file and the line where the function was called from.

my ($file, $line) = Apache2::Log::LOG_MARK();

ret1: $file ( string )

ret2: $line ( number )

since: 2.0.00

It's mostly useful to be passed as the first argument to those logging methods, expecting the filename and the line number as the first arguments (e.g., $s->log_serror and $r->log_rerror ).

Virtual Hosts

Code running from within a virtual host needs to be able to log into its ErrorLog file, if different from the main log. Calling any of the logging methods on the $r and $s objects will do the logging correctly.

If the core warn() is called, it'll be always logged to the main log file. Here is how to make it log into the vhost error_log file. Let's say that we start with the following code:

warn "the code is smoking";

First, we need to use mod_perl's logging function, instead of CORE::warn

Outside HTTP handlers at the moment it is not possible, to get hold of the vhost's error_log file. This shouldn't be a problem for the code that runs only under mod_perl, since the always available $s object can invoke a plethora of methods supplied by Apache2::Log. This is only a problem for modules, which are supposed to run outside mod_perl as well.

META: To solve this we think to introduce 'PerlOptions +GlobalServer', a big brother for 'PerlOptions +GlobalRequest', which will be set in modperl_hook_pre_connection.

Unsupported API

Apache2::Log also provides auto-generated Perl interface for a few other methods which aren't tested at the moment and therefore their API is a subject to change. These methods will be finalized later as a need arises. If you want to rely on any of the following methods please contact the the mod_perl development mailing list so we can help each other take the steps necessary to shift the method to an officially supported API.

log_pid

META: what is this method good for? it just calls getpid and logs it. In any case it has nothing to do with the logging API. And it uses static variables, it probably shouldn't be in the Apache public API.

As a valued partner and proud supporter of MetaCPAN, StickerYou is
happy to offer a 10% discount on all Custom Stickers,
Business Labels, Roll Labels,
Vinyl Lettering or Custom Decals. StickerYou.com
is your one-stop shop to make your business stick.
Use code METACPAN10 at checkout to apply your discount.