=head1 NAME

Apache::Log - Perl API for Apache Logging Methods




=head1 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
  #------------
  use Apache::Log;
  
  use strict;
  use warnings FATAL => 'all';
  
  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");
      $r->warn("whoah!");
  
      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;
  }




=head1 Description

C<Apache::Log> provides the Perl API for Apache logging methods.

Depending on the the current C<LogLevel> setting, only logging with
the same log level or higher will be loaded. For example if the
current C<LogLevel> is set to I<warning>, only messages with log level
of the level I<warning> or higher (I<err>, I<crit>, I<elert> and
I<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 I<info> or
I<debug>. C<LogLevel> is set in the configuration file, but can be
changed using the
C<L<$s-E<gt>loglevel()|docs::2.0::api::Apache::Server/C_loglevel_>>
method.

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





=head1 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);

=head2 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:

=head3 C<Apache::LOG_EMERG>

=head3 C<Apache::LOG_ALERT>

=head3 C<Apache::LOG_CRIT>

=head3 C<Apache::LOG_ERR>

=head3 C<Apache::LOG_WARNING>

=head3 C<Apache::LOG_NOTICE>

=head3 C<Apache::LOG_INFO>

=head3 C<Apache::LOG_DEBUG>

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

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


=head2 Other Constants

=head3 C<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;

=head3 C<Apache::LOG_TOCLIENT>

used to give content handlers the option of including the error text
in the C<ErrorDocument> sent back to the client. When
C<Apache::LOG_TOCLIENT> is passed to C<log_rerror()> the error message
will be saved in the C<$r>'s notes table, keyed to the string
I<"error-notes">, if and only if the severity level of the message is
C<Apache::LOG_WARNING> or greater and there are no other
I<"error-notes"> entry already set in the request record's notes
table. Once the I<"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:

  $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.

=head3 C<Apache::LOG_STARTUP>

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

  $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 C<Apache::LOG_STARTUP> is binary ORed as in:

  $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




=head1 Server Logging Methods



=head2 C<$s-E<gt>log_error>

just logs the supplied message to I<error_log>

  $s->log_error(@message);

=over 4

=item arg1: C<$s> (C<L<Apache::Server|docs::2.0::api::Apache::Server>>)

=item arg2: C<@message> (ARRAY)

what to log

=item ret: (XXX)

=back

For example:

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





=head2 C<$s-E<gt>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);

=over 4

=item arg1: C<$s> (C<L<Apache::Server|docs::2.0::api::Apache::Server>>)

=item arg2: C<$file> (string)

The file in which this function is called

=item arg3: C<$line> (number)

The line number on which this function is called

=item arg4: C<$level> (C<L<Apache::LOG_* constant|/LogLevel_Constants>>)

The level of this error message

=item arg5: C<$status> (integer)

The status code from the last command (similar to $! in perl, usually
APR:: status constant)

=item arg6: C<@message> (ARRAY)

The log message

=item ret: (XXX)

=back

For example:

  $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");




=head2 C<$s-E<gt>log>

returns a log handle which can be used to L<log messages of different
levels|/LogLevel_Methods>.

  my $slog = $s->log;

=over 4

=item arg1: C<$s> (C<L<Apache::Server|docs::2.0::api::Apache::Server>>)

=item ret: C<$slog> (scalar)

=back







=head1 Request Logging Methods




=head2 C<$r-E<gt>log_error>

logs the supplied message (similar to C<L<$s-E<gt>log_error|/C__s_E_gt_log_error_>>).

  $r->log_error(@message);

=over 4

=item arg1: C<$r> (C<L<Apache::RequestRec|docs::2.0::api::Apache::RequestRec>>)

=item arg2: C<@message> (ARRAY)

what to log

=item ret: (XXX)

=back

For example:

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






=head2 C<$r-E<gt>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
C<L<$s-E<gt>log_serror|/C__s_E_gt_log_serror_>>.

For example:

  $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");




=head2 C<$r-E<gt>log>

returns a log handle which can be used to L<log messages of different
levels|/LogLevel_Methods>.

  $rlog = $r->log;

=over 4

=item arg1: C<$r> (C<L<Apache::RequestRec|docs::2.0::api::Apache::RequestRec>>)

=item ret: C<$rlog> (scalar)

=back






=head1 Other Logging Methods

=head2 LogLevel Methods

after getting the log handle with C<L<$s-E<gt>log|/C__s_E_gt_log_>> or
C<L<$r-E<gt>log|/C__s_E_gt_log_>>, use one of the following methods
(corresponding to the C<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);

=over 4

=item arg1: C<$slog> (log handle)

=item arg2: C<@message> (ARRAY)

=back

For example if the C<LogLevel> is C<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
I<warn>, I<debug> and other logging command which are listed right to
I<error> will be disabled.



=head2 C<emerg>

See L<LogLevel Methods|/LogLevel_Methods>.



=head2 C<alert>

See L<LogLevel Methods|/LogLevel_Methods>.



=head2 C<crit>

See L<LogLevel Methods|/LogLevel_Methods>.



=head2 C<error>

See L<LogLevel Methods|/LogLevel_Methods>.



=head2 C<warn>

See L<LogLevel Methods|/LogLevel_Methods>.



=head2 C<notice>

See L<LogLevel Methods|/LogLevel_Methods>.


=head2 C<info>

See L<LogLevel Methods|/LogLevel_Methods>.


=head2 C<debug>

See L<LogLevel Methods|/LogLevel_Methods>.







=head1 General Functions

=head2 C<Apache::Log::LOG_MARK>

  ($file, $line) = Apache::Log::LOG_MARK();

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

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., C<L<$s-E<gt>log_serror|/C__s_E_gt_log_serror_>> and
C<L<$r-E<gt>log_rerror|/C__r_E_gt_log_rerror_>>).


=head2 C<Apache::Log::log_pid>


Log the current pid of the parent process

  Apache::Log::log_pid($pool, $fname);

=over 4

=item arg1: C<$p> (C<L<APR::Pool|docs::2.0::api::APR::Pool>>)

The pool to use for logging

=item arg2: C<$fname> (C<L<APR::Pool|docs::2.0::api::APR::Pool>>)

The name of the file to log to

=item ret: no return value

=back


=head1 Aliases

=head2 C<$s-E<gt>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');

=head2 C<Apache-E<gt>warn>

=head2 C<Apache::warn>

  Apache->warn(@warnings);






=head1 See Also

L<mod_perl 2.0 documentation|docs::2.0::index>.




=head1 Copyright

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




=head1 Authors

L<The mod_perl development team and numerous
contributors|about::contributors::people>.

=cut

