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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 987267c5e1469fd1a9ad94c468c9755a4ecb9999
4
- data.tar.gz: d244d4b74d07fc31c073583f84c3b41fbf85c102
3
+ metadata.gz: abb79ce504f7948a99b0358ecc7d7a4ea464abc5
4
+ data.tar.gz: bf72dd81bfc4395262ad82f6b4ff4b3f48d20a4c
5
5
  SHA512:
6
- metadata.gz: cb48db461edf5cc8dfbb59d6f05ebca624b6a0de09158a7dfde52b35e2a1d7a3204f3a1a3551e9eb8cd1e86f689513e4e79fc8f99eefcd5c0843d828ffd1fa1e
7
- data.tar.gz: 8786999d9dd5e5cb77506cacb21d8126e1983e78f3251882954c8266317e3db6a800f67c9ae115855d3deccf755d3e24793b678767a5d7ca951cc93edfbcc99f
6
+ metadata.gz: 0be7eaad187f30bac8aa20aed3f5a8365ec4e392c682358636c6a70d33cf8e13a921fd443127ae369f7ed2fa29261e3965f2ada514627d23b92ec26bb6b283b6
7
+ data.tar.gz: 75760dc1f2cf6ae4f35c7251492ced1891f5dc14ca06bba9aee4f73f30397dad5591229600406f80385f2be94411d1d6bd6c325584fc07b51f0e12550565c818
data/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
1
+ ## 0.1.0 (2017/11/26)
2
+
3
+ Enhancements:
4
+
5
+ * Add params\_proc keyword arguments to support more flexible ways to customize fields.
6
+
7
+ Changes:
8
+
9
+ * `appends` argument is deprecated.
10
+
1
11
  ## 0.0.3 (2016/04/03)
2
12
 
3
13
  Fixes:
data/README.md CHANGED
@@ -73,61 +73,97 @@ reqtime:0.000000
73
73
 
74
74
  ### Default Fields
75
75
 
76
- * time
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
- * pid
92
+ ## Arguments
81
93
 
82
- * Process ID
94
+ Arguments:
83
95
 
84
- * host
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
- * ENV['REMOTE_ADDR']
101
+ Keyword Arguments:
87
102
 
88
- * forwardedfor
103
+ * params\_proc
104
+ * A Proc object to generate fields. See [#custom-fields](#custom-fields)
89
105
 
90
- * ENV['X_FORWARDED_FOR']
106
+ ### Log Rotation
91
107
 
92
- * user
108
+ If you utilize log rotation functionality of Ruby's standard logger, you may write as
93
109
 
94
- * ENV['REMOTE_USER']
95
-
96
- * method
97
-
98
- * ENV['REQUEST_METHOD']
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
- * size
117
+ logger = MyLogger.new('/path/to/access.log', 3, 1048576)
118
+ config.middleware.insert_after(0, Rack::LtsvLogger, logger)
119
+ ```
117
120
 
118
- * Response Content-Length
121
+ ### Custom Fields
119
122
 
120
- * reqtime
123
+ Pass a proc object to customize fields as:
121
124
 
122
- * The request time in secods. milli seconds are written after the decimal point.
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
- * Others
138
+ or
125
139
 
126
- * See http://ltsv.org/
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
- ### Custom Fields
164
+ I recommend to follow http://ltsv.org/ to add new key names.
129
165
 
130
- You may append LTSV fields as:
166
+ **Deprecated**
131
167
 
132
168
  ```ruby
133
169
  appends = {
@@ -2,5 +2,14 @@ require 'logger'
2
2
  require 'rack/ltsv_logger'
3
3
  require_relative 'app'
4
4
 
5
- use Rack::LtsvLogger, STDOUT
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
@@ -2,21 +2,10 @@ require 'time'
2
2
 
3
3
  module Rack
4
4
  class LtsvLogger
5
- def initialize(app, logger=nil, appends = {})
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
- private
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 ltsv(hash)
44
- hash.map {|k, v| "#{k}:#{v}" }.join("\t") + "\n"
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
@@ -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.3"
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}
@@ -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 'appends' do
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.3
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: 2016-04-02 00:00:00.000000000 Z
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.5.1
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.