Apache::Log -- Perl API for Apache Logging Methods
#in startup.pl #------------- use Apache::Log; my $s = Apache->server; $s->log_error("server: log_error"); $s->log_serror(Apache::LOG_MARK, Apache::LOG_ERR, 0, "log_serror logging at err level"); $s->log_serror(Apache::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; 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_MARK, Apache::LOG_DEBUG, APR::ENOTIME, "in debug"); $s->log_serror(Apache::LOG_MARK, Apache::LOG_INFO, 0, "server info"); $s->log_serror(Apache::LOG_MARK, Apache::LOG_ERR, APR::ENOTIME, "fatal error"); $s->warn('routine server warning'); }
Apache::Log provides the Perl API for Apache logging methods.
Apache::Log
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
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:
LogLevel
$r->log_rerror(Apache::LOG_MARK, Apache::LOG_WARNING, APR::ENOTIME, "warning!");
will log the message, but this one won't:
$r->log_rerror(Apache::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. (either in the configuration file or using the $s->loglevel() method.)
$s->loglevel()
Other 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:
ErrorDocument
log_rerror()
$r
$r->log_rerror(Apache::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 set only during the startup.
$s->log_error()
$s->log_error(@message);
just logs the supplied message. For example:
$s->log_error("running low on memory");
$s->log_serror()
log_serror($file, $line, $level, $status, @message);
where:
* $file The file in which this function is called * $line The line number on which this function is called * $level The level of this error message * $status The status code from the previous command * @message The log message
This function provides a fine control of when the message is logged, gives an access to built-in status codes.
For example:
$s->log_serror(Apache::LOG_MARK, Apache::LOG_ERR, 0, "log_serror logging at err level"); $s->log_serror(Apache::LOG_MARK, Apache::LOG_DEBUG, APR::ENOTIME, "debug print");
$s->log()
my $slog = $s->log;
returns a handle which can be used to log messages of different level. See the next entry.
$s->log->emerg(@message);
after getting the log handle with $s->log, use these methods to control when messages should be logged.
$s->log
my $slog = $s->log; $slog->debug("just ", "some debug info"); $slog->warn(@warnings); $slog->crit("dying");
$r->log_error()
$r->log_error(@message);
logs the supplied message (similar to $s->log_error). For example:
$s->log_error
$r->log_error("the request is about to end");
the same as $s->log_error.
$r->log_rerror()
log_rerror($file, $line, $level, $status, @message);
same as $s->log_rerror. For example:
$s->log_rerror
$s->log_rerror(Apache::LOG_MARK, Apache::LOG_ERR, 0, "log_rerror logging at err level"); $s->log_rerror(Apache::LOG_MARK, Apache::LOG_DEBUG, APR::ENOTIME, "debug print");
$r->log()
my $rlog = $r->log;
Similar to $s->log()
Similar to the server's log functions with the same names.
$rlog->debug("just ", "some debug info"); $rlog->warn(@req_warnings); $rlog->crit("dying");
Apache::LOG_MARK()
my($file, $line) = Apache::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.
(__FILE__, __LINE__)
$s->warn()
$s->warn(@warnings);
is the same as:
$s->log_error(Apache::LOG_MARK, Apache::LOG_WARNING, 0, @warnings)
$s->warn('routine server warning');
Apache->warn()
Apache::warn()
Apache->warn(@warnings);
To install mod_perl, copy and paste the appropriate command in to your terminal.
cpanm
cpanm mod_perl
CPAN shell
perl -MCPAN -e shell install mod_perl
For more information on module installation, please visit the detailed CPAN module installation guide.