Apache::Log - Perl API for Apache Logging Methods


NAME

Apache::Log - Perl API for Apache Logging Methods


Synopsis

  #in startup.pl
  #-------------
  use Apache::Log;

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

  my $s = Apache->server;

  $s->log_error("server: log_error");
  $s->log_serror(__FILE__, __LINE__, Apache::LOG_ERR,
                 APR::SUCCESS, "log_serror logging at err level");
  $s->log_serror(Apache::Log::LOG_MARK, Apache::LOG_DEBUG,
                 APR::ENOTIME, "debug print");
  Apache::Server->log_error("routine warning");

  Apache->warn("routine warning");
  Apache::warn("routine warning");
  Apache::Server->warn("routine warning");
  #in a handler
  #------------
  package Foo;

  use strict;
  use warnings FATAL => 'all';

  use Apache::Log;

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

  sub handler {
      my $r = shift;
      $r->log_error("request: log_error");

      my $rlog = $r->log;
      for my $level qw(emerg alert crit error warn notice info debug) {
          no strict 'refs';
          $rlog->$level($package, "request: $level log level");
      }

      # can use server methods as well
      my $s = $r->server;
      $s->log_error("server: log_error");

      $r->log_rerror(Apache::Log::LOG_MARK, Apache::LOG_DEBUG,
                     APR::ENOTIME, "in debug");

      $s->log_serror(Apache::Log::LOG_MARK, Apache::LOG_INFO,
                     APR::SUCESS, "server info");

      $s->log_serror(Apache::Log::LOG_MARK, Apache::LOG_ERR,
                     APR::ENOTIME, "fatal error");
      $s->warn('routine server warning');
      return Apache::OK;
  }
  1;


Description

Apache::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:

  $r->log_rerror(Apache::Log::LOG_MARK, Apache::LOG_WARNING,
                 APR::ENOTIME, "warning!");

will log the message, but this one won't:

  $r->log_rerror(Apache::Log::LOG_MARK, Apache::LOG_INFO,
                 APR::ENOTIME, "just an info");

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()|docs::2.0::api::Apache::Server/C_loglevel_ method.

The filename and the line number of the caller are logged only if Apache::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 Apache::Log functions could be slower than Perl's print()/warn().


Constants

Log level constants can be compiled all at once:

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

or individually:

  use Apache::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:

Apache::LOG_EMERG

Apache::LOG_ALERT

Apache::LOG_CRIT

Apache::LOG_ERR

Apache::LOG_WARNING

Apache::LOG_NOTICE

Apache::LOG_INFO

Apache::LOG_DEBUG

Other Constants

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

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

Here is the rest of the logging related constants:

Apache::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;

Apache::LOG_TOCLIENT

used to give content handlers the option of including the error text in the ErrorDocument sent back to the client. When Apache::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 Apache::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:

  use Apache::Const -compile => qw(:log);
  use APR::Const    -compile => qw(ENOTIME);
  $r->log_rerror(Apache::Log::LOG_MARK,
                 Apache::LOG_ERR|Apache::LOG_TOCLIENT,
                 APR::ENOTIME,
                 "request log_rerror");

now the log message can be retrieved via:

  $r->notes->get("error-notes");

Remember that client-generated text streams sent back to the client MUST be escaped to prevent CSS attacks.

Apache::LOG_STARTUP

is useful for startup message where no timestamps, logging level is wanted. For example:

  use Apache::Const -compile => qw(:log);
  use APR::Const    -compile => qw(SUCCESS);
  $s->log_serror(Apache::Log::LOG_MARK,
                 Apache::LOG_INFO,
                 APR::SUCCESS,
                 "This log message comes with a header");

will print:

  [Wed May 14 16:47:09 2003] [info] This log message comes with a header

whereas, when Apache::LOG_STARTUP is binary ORed as in:

  use Apache::Const -compile => qw(:log);
  use APR::Const    -compile => qw(SUCCESS);
  $s->log_serror(Apache::Log::LOG_MARK,
                 Apache::LOG_INFO|Apache::LOG_STARTUP,
                 APR::SUCCESS,
                 "This log message comes with no header");

then the logging will be:

  This log message comes with no header


Server Logging Methods

$s->log_error

just logs the supplied message to error_log

  $s->log_error(@message);
obj: $s ( Apache::ServerRec object|docs::2.0::api::Apache::ServerRec )
arg1: @message ( strings ARRAY )
what to log

ret: no return value
since: 1.99_12

For example:

  $s->log_error("running low on memory");

$s->log_serror

This function provides a fine control of when the message is logged, gives an access to built-in status codes.

  $s->log_serror($file, $line, $level, $status, @message);
obj: $s ( Apache::ServerRec object|docs::2.0::api::Apache::ServerRec )
arg1: $file ( string )
The file in which this function is called

arg2: $line ( number )
The line number on which this function is called

arg3: $level ( Apache::LOG_* constant|/LogLevel_Constants )
The level of this error message

arg4: $status ( APR::Const status constant|docs::2.0::api::APR::Const )
The status code from the last command (similar to $! in perl), usually APR::Const constant|docs::2.0::api::APR::Const or coming from an exception object.

arg5: @message ( strings ARRAY )
The log message(s)

ret: no return value
since: 1.99_12

For example:

  use Apache::Const -compile => qw(:log);
  use APR::Const    -compile => qw(ENOTIME SUCCESS);
  $s->log_serror(Apache::Log::LOG_MARK, Apache::LOG_ERR,
                 APR::SUCCESS, "log_serror logging at err level");

  $s->log_serror(Apache::Log::LOG_MARK, Apache::LOG_DEBUG,
                 APR::ENOTIME, "debug print");

$s->log

get a log handle which can be used to log messages of different levels.

  my $slog = $s->log;
obj: $s ( Apache::ServerRec object|docs::2.0::api::Apache::ServerRec )
ret: $slog ( Apache::Log::Server object )
Apache::Log::Server object to be used with LogLevel methods.

since: 1.99_12


Request Logging Methods

$r->log_error

just logs the supplied message (similar to $s->log_error|/C__s_E_gt_log_error_ ).

  $r->log_error(@message);
obj: $r ( Apache::RequestRec object|docs::2.0::api::Apache::RequestRec )
arg1: @message ( strings ARRAY )
what to log

ret: no return value
since: 1.99_12

For example:

  $r->log_error("the request is about to end");

$r->log_rerror

This function provides a fine control of when the message is logged, gives an access to built-in status codes.

  $r->log_rerror($file, $line, $level, $status, @message);

arguments are identical to $s->log_serror|/C__s_E_gt_log_serror_.

since: 1.99_12

For example:

  use Apache::Const -compile => qw(:log);
  use APR::Const    -compile => qw(ENOTIME SUCCESS);
  $r->log_rerror(Apache::Log::LOG_MARK, Apache::LOG_ERR,
                 APR::SUCCESS, "log_rerror logging at err level");

  $r->log_rerror(Apache::Log::LOG_MARK, Apache::LOG_DEBUG,
                 APR::ENOTIME, "debug print");

$r->log

get a log handle which can be used to log messages of different levels.

  $rlog = $r->log;
obj: $r ( Apache::RequestRec object|docs::2.0::api::Apache::RequestRec )
ret: $rlog ( Apache::Log::Request object )
Apache::Log::Request object to be used with LogLevel methods.

since: 1.99_12


Other Logging Methods

LogLevel Methods

after getting the log handle with $s->log|/C__s_E_gt_log_ or $r->log|/C__s_E_gt_log_, use one of the following methods (corresponding to the LogLevel levels):

  emerg(), alert(), crit(), error(), warn(), notice(), info(), debug()

to control when messages should be logged:

  $s->log->emerg(@message);
  $r->log->emerg(@message);
obj: $slog ( server or request log handle )
arg1: @message ( strings ARRAY )
ret: no return value
since: 1.99_12

For example if the LogLevel is error and the following code is executed:

  my $slog = $s->log;
  $slog->debug("just ", "some debug info");
  $slog->warn(@warnings);
  $slog->crit("dying");

only the last command's logging will be performed. This is because warn, debug and other logging command which are listed right to error will be disabled.

emerg

See LogLevel Methods.

alert

See LogLevel Methods.

crit

See LogLevel Methods.

error

See LogLevel Methods.

warn

See LogLevel Methods.

notice

See LogLevel Methods.

info

See LogLevel Methods.

debug

See LogLevel Methods.


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) = Apache::Log::LOG_MARK();
ret1: $file ( string )
ret2: $line ( number )
since: 1.99_12

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|/C__s_E_gt_log_serror_ and $r->log_rerror|/C__r_E_gt_log_rerror_ ).


Aliases

$s->warn

  $s->warn(@warnings);

is the same as:

  $s->log_error(Apache::Log::LOG_MARK, Apache::LOG_WARNING,
                APR::SUCCESS, @warnings)

For example:

  $s->warn('routine server warning');

Apache->warn

Apache::warn

  Apache->warn(@warnings);


Unsupported API

Apache::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.

Log the current pid

  Apache::Log::log_pid($pool, $fname);
obj: $p ( APR::Pool object|docs::2.0::api::APR::Pool )
The pool to use for logging

arg1: $fname ( file path )
The name of the file to log to

ret: no return value
since: subject to change


See Also

mod_perl 2.0 documentation.


Copyright

mod_perl 2.0 and its core modules are copyrighted under The Apache Software License, Version 2.0.


Authors

The mod_perl development team and numerous contributors.

 Apache::Log - Perl API for Apache Logging Methods