1 | #
|
---|
2 | # accesslog.rb -- Access log handling utilities
|
---|
3 | #
|
---|
4 | # Author: IPR -- Internet Programming with Ruby -- writers
|
---|
5 | # Copyright (c) 2002 keita yamaguchi
|
---|
6 | # Copyright (c) 2002 Internet Programming with Ruby writers
|
---|
7 | #
|
---|
8 | # $IPR: accesslog.rb,v 1.1 2002/10/01 17:16:32 gotoyuzo Exp $
|
---|
9 |
|
---|
10 | module WEBrick
|
---|
11 | module AccessLog
|
---|
12 | class AccessLogError < StandardError; end
|
---|
13 |
|
---|
14 | CLF_TIME_FORMAT = "[%d/%b/%Y:%H:%M:%S %Z]"
|
---|
15 | COMMON_LOG_FORMAT = "%h %l %u %t \"%r\" %s %b"
|
---|
16 | CLF = COMMON_LOG_FORMAT
|
---|
17 | REFERER_LOG_FORMAT = "%{Referer}i -> %U"
|
---|
18 | AGENT_LOG_FORMAT = "%{User-Agent}i"
|
---|
19 | COMBINED_LOG_FORMAT = "#{CLF} \"%{Referer}i\" \"%{User-agent}i\""
|
---|
20 |
|
---|
21 | module_function
|
---|
22 |
|
---|
23 | # This format specification is a subset of mod_log_config of Apache.
|
---|
24 | # http://httpd.apache.org/docs/mod/mod_log_config.html#formats
|
---|
25 | def setup_params(config, req, res)
|
---|
26 | params = Hash.new("")
|
---|
27 | params["a"] = req.peeraddr[3]
|
---|
28 | params["b"] = res.sent_size
|
---|
29 | params["e"] = ENV
|
---|
30 | params["f"] = res.filename || ""
|
---|
31 | params["h"] = req.peeraddr[2]
|
---|
32 | params["i"] = req
|
---|
33 | params["l"] = "-"
|
---|
34 | params["m"] = req.request_method
|
---|
35 | params["n"] = req.attributes
|
---|
36 | params["o"] = res
|
---|
37 | params["p"] = req.port
|
---|
38 | params["q"] = req.query_string
|
---|
39 | params["r"] = req.request_line.sub(/\x0d?\x0a\z/o, '')
|
---|
40 | params["s"] = res.status # won't support "%>s"
|
---|
41 | params["t"] = req.request_time
|
---|
42 | params["T"] = Time.now - req.request_time
|
---|
43 | params["u"] = req.user || "-"
|
---|
44 | params["U"] = req.unparsed_uri
|
---|
45 | params["v"] = config[:ServerName]
|
---|
46 | params
|
---|
47 | end
|
---|
48 |
|
---|
49 | def format(format_string, params)
|
---|
50 | format_string.gsub(/\%(?:\{(.*?)\})?>?([a-zA-Z%])/){
|
---|
51 | param, spec = $1, $2
|
---|
52 | case spec[0]
|
---|
53 | when ?e, ?i, ?n, ?o
|
---|
54 | raise AccessLogError,
|
---|
55 | "parameter is required for \"#{spec}\"" unless param
|
---|
56 | params[spec][param] || "-"
|
---|
57 | when ?t
|
---|
58 | params[spec].strftime(param || CLF_TIME_FORMAT)
|
---|
59 | when ?%
|
---|
60 | "%"
|
---|
61 | else
|
---|
62 | params[spec]
|
---|
63 | end
|
---|
64 | }
|
---|
65 | end
|
---|
66 | end
|
---|
67 | end
|
---|