rack-ltsv_logger 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/README.md +74 -38
- data/example/sinatra/config.ru +10 -1
- data/lib/rack/ltsv_logger.rb +25 -26
- data/rack-ltsv_logger.gemspec +1 -1
- data/spec/ltsv_logger_spec.rb +27 -3
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: abb79ce504f7948a99b0358ecc7d7a4ea464abc5
|
4
|
+
data.tar.gz: bf72dd81bfc4395262ad82f6b4ff4b3f48d20a4c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0be7eaad187f30bac8aa20aed3f5a8365ec4e392c682358636c6a70d33cf8e13a921fd443127ae369f7ed2fa29261e3965f2ada514627d23b92ec26bb6b283b6
|
7
|
+
data.tar.gz: 75760dc1f2cf6ae4f35c7251492ced1891f5dc14ca06bba9aee4f73f30397dad5591229600406f80385f2be94411d1d6bd6c325584fc07b51f0e12550565c818
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -73,61 +73,97 @@ reqtime:0.000000
|
|
73
73
|
|
74
74
|
### Default Fields
|
75
75
|
|
76
|
-
|
76
|
+
| key | value |
|
77
|
+
|--------------|--------------------------------------------------------------------------------|
|
78
|
+
| time | The datetime in ISO-8601 format |
|
79
|
+
| pid | Procedd ID |
|
80
|
+
| host | ENV['REMOTE_ADDR'] |
|
81
|
+
| forwardedfor | ENV['X_FORWARDED_FOR'] |
|
82
|
+
| user | ENV['REMOTE_USER'] |
|
83
|
+
| method | ENV['REQUEST_METHOD'] |
|
84
|
+
| uri | ENV['PATH_INFO'] |
|
85
|
+
| query | ENV['QUERY_STRING'] |
|
86
|
+
| protocol | ENV['HTTP_VERSION'] |
|
87
|
+
| status | Response Status Code |
|
88
|
+
| size | Response Content-Length |
|
89
|
+
| reqtime | The request time in secods. milli seconds are written after the decimal point. |
|
77
90
|
|
78
|
-
* The datetime in ISO-8601 format
|
79
91
|
|
80
|
-
|
92
|
+
## Arguments
|
81
93
|
|
82
|
-
|
94
|
+
Arguments:
|
83
95
|
|
84
|
-
*
|
96
|
+
* io
|
97
|
+
* An IO object, or something which has `#write` method
|
98
|
+
* ~~appends (deprecated)~~
|
99
|
+
* ~~A Hash object to append custom fields. See [#custom-fields](#custom-fields)~~
|
85
100
|
|
86
|
-
|
101
|
+
Keyword Arguments:
|
87
102
|
|
88
|
-
*
|
103
|
+
* params\_proc
|
104
|
+
* A Proc object to generate fields. See [#custom-fields](#custom-fields)
|
89
105
|
|
90
|
-
|
106
|
+
### Log Rotation
|
91
107
|
|
92
|
-
|
108
|
+
If you utilize log rotation functionality of Ruby's standard logger, you may write as
|
93
109
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
* uri
|
101
|
-
|
102
|
-
* ENV['PATH_INFO']
|
103
|
-
|
104
|
-
* query
|
105
|
-
|
106
|
-
* ENV['QUERY_STRING']
|
107
|
-
|
108
|
-
* protocol
|
109
|
-
|
110
|
-
* ENV['HTTP_VERSION']
|
111
|
-
|
112
|
-
* status
|
113
|
-
|
114
|
-
* Response Status Code
|
110
|
+
```ruby
|
111
|
+
class MyLogger < ::Logger
|
112
|
+
def write(msg)
|
113
|
+
@logdev.write msg
|
114
|
+
end
|
115
|
+
end
|
115
116
|
|
116
|
-
|
117
|
+
logger = MyLogger.new('/path/to/access.log', 3, 1048576)
|
118
|
+
config.middleware.insert_after(0, Rack::LtsvLogger, logger)
|
119
|
+
```
|
117
120
|
|
118
|
-
|
121
|
+
### Custom Fields
|
119
122
|
|
120
|
-
|
123
|
+
Pass a proc object to customize fields as:
|
121
124
|
|
122
|
-
|
125
|
+
```ruby
|
126
|
+
params_proc = Proc.new do |env, status, headers, body, began_at|
|
127
|
+
params = Rack::LtsvLogger::DEFAULT_PARAMS_PROC.call(env, status, headers, body, began_at)
|
128
|
+
params.delete(:protocol)
|
129
|
+
params.merge!(
|
130
|
+
vhost: env['HTTP_HOST'] || "-",
|
131
|
+
ua: env['HTTP_USER_AGENT'] || "-",
|
132
|
+
referer: env['HTTP_REFERER'] || "-",
|
133
|
+
)
|
134
|
+
end
|
135
|
+
config.middleware.insert_after(0, Rack::LtsvLogger, $stdout, params_proc: params_proc)
|
136
|
+
```
|
123
137
|
|
124
|
-
|
138
|
+
or
|
125
139
|
|
126
|
-
|
140
|
+
```ruby
|
141
|
+
params_proc = Proc.new do |env, status, headers, body, began_at|
|
142
|
+
now = Time.now
|
143
|
+
reqtime = now.instance_eval { to_i + (usec/1000000.0) } - began_at
|
144
|
+
{
|
145
|
+
time: now.iso8601,
|
146
|
+
pid: Process.pid,
|
147
|
+
host: env["REMOTE_ADDR"] || "-",
|
148
|
+
forwardedfor: env['HTTP_X_FORWARDED_FOR'] || "-",
|
149
|
+
user: env["REMOTE_USER"] || "-",
|
150
|
+
method: env["REQUEST_METHOD"],
|
151
|
+
uri: env["PATH_INFO"],
|
152
|
+
query: env["QUERY_STRING"].empty? ? "" : "?"+env["QUERY_STRING"],
|
153
|
+
status: ::Rack::LtsvLogger.extract_status(status),
|
154
|
+
size: ::Rack::LtsvLogger.extract_content_length(headers),
|
155
|
+
reqtime: "%0.6f" % reqtime,
|
156
|
+
vhost: env['HTTP_HOST'] || "-",
|
157
|
+
ua: env['HTTP_USER_AGENT'] || "-",
|
158
|
+
referer: env['HTTP_REFERER'] || "-",
|
159
|
+
}
|
160
|
+
end
|
161
|
+
config.middleware.insert_after(0, Rack::LtsvLogger, $stdout, params_proc: params_proc)
|
162
|
+
```
|
127
163
|
|
128
|
-
|
164
|
+
I recommend to follow http://ltsv.org/ to add new key names.
|
129
165
|
|
130
|
-
|
166
|
+
**Deprecated**
|
131
167
|
|
132
168
|
```ruby
|
133
169
|
appends = {
|
data/example/sinatra/config.ru
CHANGED
@@ -2,5 +2,14 @@ require 'logger'
|
|
2
2
|
require 'rack/ltsv_logger'
|
3
3
|
require_relative 'app'
|
4
4
|
|
5
|
-
|
5
|
+
params_proc = Proc.new do |env, status, headers, body, began_at|
|
6
|
+
params = Rack::LtsvLogger::DEFAULT_PARAMS_PROC.call(env, status, headers, body, began_at)
|
7
|
+
params.delete(:protocol)
|
8
|
+
params.merge!({
|
9
|
+
vhost: env['HTTP_HOST'] || "-",
|
10
|
+
ua: env['HTTP_USER_AGENT'] || "-",
|
11
|
+
referer: env['HTTP_REFERER'] || "-",
|
12
|
+
})
|
13
|
+
end
|
14
|
+
use Rack::LtsvLogger, $stdout, params_proc: params_proc
|
6
15
|
run App
|
data/lib/rack/ltsv_logger.rb
CHANGED
@@ -2,21 +2,10 @@ require 'time'
|
|
2
2
|
|
3
3
|
module Rack
|
4
4
|
class LtsvLogger
|
5
|
-
|
6
|
-
@app = app
|
7
|
-
@logger = logger || $stdout
|
8
|
-
@appends = appends
|
9
|
-
end
|
10
|
-
|
11
|
-
def call(env)
|
12
|
-
began_at = Time.now.instance_eval { to_i + (usec/1000000.0) }
|
13
|
-
|
14
|
-
status, headers, body = @app.call(env)
|
15
|
-
ensure
|
5
|
+
DEFAULT_PARAMS_PROC = Proc.new do |env, status, headers, body, began_at|
|
16
6
|
now = Time.now
|
17
7
|
reqtime = now.instance_eval { to_i + (usec/1000000.0) } - began_at
|
18
|
-
|
19
|
-
params = {
|
8
|
+
{
|
20
9
|
time: now.iso8601,
|
21
10
|
pid: Process.pid,
|
22
11
|
host: env["REMOTE_ADDR"] || "-",
|
@@ -26,31 +15,41 @@ module Rack
|
|
26
15
|
uri: env["PATH_INFO"],
|
27
16
|
query: env["QUERY_STRING"].empty? ? "" : "?"+env["QUERY_STRING"],
|
28
17
|
protocol: env["HTTP_VERSION"],
|
29
|
-
status: extract_status(status),
|
30
|
-
size: extract_content_length(headers),
|
18
|
+
status: ::Rack::LtsvLogger.extract_status(status),
|
19
|
+
size: ::Rack::LtsvLogger.extract_content_length(headers),
|
31
20
|
reqtime: "%0.6f" % reqtime,
|
32
21
|
}
|
33
|
-
@appends.each do |key, proc|
|
34
|
-
params[key] = proc.call(env)
|
35
|
-
end
|
36
|
-
@logger.write ltsv(params)
|
37
|
-
|
38
|
-
[status, headers, body]
|
39
22
|
end
|
40
23
|
|
41
|
-
|
24
|
+
def initialize(app, io = nil, **kwargs)
|
25
|
+
@app = app
|
26
|
+
@io = io || $stdout
|
27
|
+
@params_proc = kwargs[:params_proc] || DEFAULT_PARAMS_PROC
|
28
|
+
@appends = kwargs.tap {|h| h.delete(:params_proc) } # old version compatibility
|
29
|
+
end
|
42
30
|
|
43
|
-
def
|
44
|
-
|
31
|
+
def call(env)
|
32
|
+
began_at = Time.now.instance_eval { to_i + (usec/1000000.0) }
|
33
|
+
status, headers, body = @app.call(env)
|
34
|
+
ensure
|
35
|
+
params = @params_proc.call(env, status, headers, body, began_at)
|
36
|
+
@appends.each {|key, proc| params[key] = proc.call(env) } # old version compatibility
|
37
|
+
@io.write ltsv(params)
|
45
38
|
end
|
46
39
|
|
47
|
-
def extract_content_length(headers)
|
40
|
+
def self.extract_content_length(headers)
|
48
41
|
value = headers && headers['Content-Length'] or return '-'
|
49
42
|
value.to_s == '0' ? '-' : value
|
50
43
|
end
|
51
44
|
|
52
|
-
def extract_status(status)
|
45
|
+
def self.extract_status(status)
|
53
46
|
status.nil? ? "500" : status.to_s[0..3]
|
54
47
|
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def ltsv(hash)
|
52
|
+
hash.map {|k, v| "#{k}:#{v}" }.join("\t") + "\n"
|
53
|
+
end
|
55
54
|
end
|
56
55
|
end
|
data/rack-ltsv_logger.gemspec
CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = "rack-ltsv_logger"
|
7
|
-
spec.version = "0.0
|
7
|
+
spec.version = "0.1.0"
|
8
8
|
spec.authors = ["Naotoshi Seo"]
|
9
9
|
spec.email = ["sonots@gmail.com"]
|
10
10
|
spec.description = %q{A rack middleware to output access log in ltsv format}
|
data/spec/ltsv_logger_spec.rb
CHANGED
@@ -42,7 +42,7 @@ describe Rack::LtsvLogger do
|
|
42
42
|
Timecop.return
|
43
43
|
end
|
44
44
|
|
45
|
-
context 'ltsv' do
|
45
|
+
context 'default ltsv' do
|
46
46
|
subject do
|
47
47
|
@output = StringIO.new
|
48
48
|
Rack::Lint.new( Rack::LtsvLogger.new(app, @output) )
|
@@ -60,7 +60,31 @@ describe Rack::LtsvLogger do
|
|
60
60
|
end
|
61
61
|
end
|
62
62
|
|
63
|
-
context '
|
63
|
+
context 'custom fields' do
|
64
|
+
let(:params_proc) do
|
65
|
+
Proc.new do |env, status, headers, body, began_at|
|
66
|
+
params = Rack::LtsvLogger::DEFAULT_PARAMS_PROC.call(env, status, headers, body, began_at)
|
67
|
+
params.delete(:protocol)
|
68
|
+
params.merge!(
|
69
|
+
new_protocol: env['HTTP_VERSION'],
|
70
|
+
)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
subject do
|
75
|
+
@output = StringIO.new
|
76
|
+
Rack::Lint.new( Rack::LtsvLogger.new(app, @output, params_proc: params_proc) )
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'GET /get' do
|
80
|
+
Rack::MockRequest.new(subject).get('/get', env)
|
81
|
+
params = parse_ltsv(@output.string)
|
82
|
+
expect(params['protocol']).to be_nil
|
83
|
+
expect(params['new_protocol']).to eq(protocol)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
context 'appends (deprecated)' do
|
64
88
|
let(:appends) do
|
65
89
|
{ x_runtime: Proc.new {|env| '1.234' } }
|
66
90
|
end
|
@@ -75,7 +99,7 @@ describe Rack::LtsvLogger do
|
|
75
99
|
params = parse_ltsv(@output.string)
|
76
100
|
expect(params['x_runtime']).to eq('1.234')
|
77
101
|
end
|
78
|
-
end
|
102
|
+
end
|
79
103
|
|
80
104
|
context 'when app error occured' do
|
81
105
|
let(:appends) do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-ltsv_logger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Naotoshi Seo
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-11-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -151,7 +151,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
151
151
|
version: '0'
|
152
152
|
requirements: []
|
153
153
|
rubyforge_project:
|
154
|
-
rubygems_version: 2.
|
154
|
+
rubygems_version: 2.6.13
|
155
155
|
signing_key:
|
156
156
|
specification_version: 4
|
157
157
|
summary: A rack middleware to output access log in ltsv format.
|