Apache LogFormat

From Noah.org
Revision as of 19:21, 3 January 2013 by Root (Talk | contribs)

Jump to: navigation, search

This is how I configure my Apache LogFormat.

This includes the process ID of the child that serviced a request and the time taken. This is useful in debugging PHP scripts.

This configuration eliminates log messages from favicon.ico requests; server status checks (libwww-perl); and internal connection checks (internal dummy connection).

This includes a lot of extra info that a high-traffic site might not want to log, but I found Apache's logging facility to be very efficient. The extra log info has low overhead.

# %{%s}t The epoch timestamp (number of seconds since 1970-01-01 00:00:00 +0000 (UTC)).
# %V The server name according to the UseCanonicalName setting. Useful for virtual hosting servers.
# %h Remote host
# %l Remote logname (from identd, if supplied). This will return a dash unless mod_ident is present and IdentityCheck is set On.
# %u Remote user (from auth; may be bogus if return status (%s) is 401)
# %t Time the request was received (standard English format)
# %r First line of request
# %>s status. For requests that got internally redirected, this is the status of the *original* request --- %>s  for the last.
# %b Size of response in bytes, excluding HTTP headers. In CLF format, i.e.  a '-' rather than a 0 when no bytes are sent.
# %{Referer}i The contents of Foobar: header line(s) in the request sent to the server.
# %{User-Agent}i 
# %{Foorbar}C The contents of cookie Foobar in the request sent to the server.
# %P The process ID of the child that serviced the request.
# %D The microseconds of time taken to serve the request.
    LogFormat "%{%s}t %V %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %P %D" vcombinedwithPD

    SetEnvIf Request_URI "^/favicon\.ico$" dontlog
    SetEnvIf User-Agent "libwww-perl.*" dontlog
    SetEnvIf User-Agent ".*internal dummy connection.*" dontlog
    CustomLog /var/log/apache2/access_log vcombinedwithPD env=!dontlog

Apache 2.4 supports subsecond resolution in time. The following adds milliseconds to the epoch timestamp.

    LogFormat "%{%s}t.%{msec_frac}t %V %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %P %D" vcombinedwithPD

= mod_rewrite

It's often handy to log mod_rewrite activity. Unfortunately, you can't change the format of this log (no epoch timestamps, for example). Set RewriteLogLevel to 0 to disable logging; set to 1 for production logging; set up to level 9 for debugging.

RewriteLog /var/log/apache2/rewrite.log
RewriteLogLevel 1