AccessLog provides logging to various files in various formats.
Multiple logs may be written to at the same time:
access_log = [ [$stderr, WEBrick::AccessLog::COMMON_LOG_FORMAT], [$stderr, WEBrick::AccessLog::REFERER_LOG_FORMAT], ] server = WEBrick::HTTPServer.new :AccessLog => access_log
Custom log formats may be defined. WEBrick::AccessLog provides a subset of the formatting from Apache’s mod_log_config httpd.apache.org/docs/mod/mod_log_config.html#formats. See AccessLog::setup_params for a list of supported options
User-Agent Log Format
Short alias for Common Log Format
The Common Log Format’s time format
Combined Log Format
Common Log Format
Referer Log Format
# File webrick/accesslog.rb, line 143
def escape(data)
if data.tainted?
data.gsub(/[[:cntrl:]\]+/) {$&.dump[1...-1]}.untaint
else
data
end
end
# File webrick/accesslog.rb, line 118
def format(format_string, params)
format_string.gsub(/\%(?:\{(.*?)\})?>?([a-zA-Z%])/){
param, spec = $1, $2
case spec[0]
when ee, ii, nn, oo
raise AccessLogError,
"parameter is required for \"#{spec}\"" unless param
(param = params[spec][param]) ? escape(param) : "-"
when tt
params[spec].strftime(param || CLF_TIME_FORMAT)
when pp
case param
when 'remote'
escape(params["i"].peeraddr[1].to_s)
else
escape(params["p"].to_s)
end
when %%
"%"
else
escape(params[spec].to_s)
end
}
end
This format specification is a subset of mod_log_config of Apache:
Remote IP address
Total response size
Given variable in ENV
Response filename
Remote host name
Given request header
Remote logname, always “-”
Request method
Given request attribute from req.attributes
Given response header
Server’s request port
The canonical port of the server serving the request or the actual port or the client’s actual port. Valid formats are canonical, local or remote.
Request query string
First line of the request
Request status
Time the request was recieved
Time taken to process the request
Remote user from auth
Unparsed URI
Literal %
# File webrick/accesslog.rb, line 94
def setup_params(config, req, res)
params = Hash.new("")
params["a"] = req.peeraddr[3]
params["b"] = res.sent_size
params["e"] = ENV
params["f"] = res.filename || ""
params["h"] = req.peeraddr[2]
params["i"] = req
params["l"] = "-"
params["m"] = req.request_method
params["n"] = req.attributes
params["o"] = res
params["p"] = req.port
params["q"] = req.query_string
params["r"] = req.request_line.sub(/\x0d?\x0a\z/, '')
params["s"] = res.status # won't support "%>s"
params["t"] = req.request_time
params["T"] = Time.now - req.request_time
params["u"] = req.user || "-"
params["U"] = req.unparsed_uri
params["v"] = config[:ServerName]
params
end
Commenting is here to help enhance the documentation. For example, sample code, or clarification of the documentation.
If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.
If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.