rack-traffic-logger 0.2.0 → 0.2.1

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: f50ccbadf767b049731815d271ce316d986d916a
4
- data.tar.gz: a6a970b4368ff2ed2a88a3ab66384dc32e046220
3
+ metadata.gz: b956c1f090a00f5a1a2a9312a2d75d553110a1a5
4
+ data.tar.gz: 61e0c4abd72b7273951b6b6a3bcca482b0debea1
5
5
  SHA512:
6
- metadata.gz: 29df161ebf86d8f6735f92f8d85314450c53534ef9e6fbcc0c67edc0f306b014bea4cd5920a41e520e9f86698feb85d5b1bf5d948764cb8bd64e4c73d00b3a11
7
- data.tar.gz: 8331c0187a9d47ecbda6972f18724801dc6b3b806f32f5c1a95af3f220320eb71d79db85c9fbfb7047652ee2f1d0345d73ebc3762816e609d483d17bc822de24
6
+ metadata.gz: 1a1f32d6b739d39af2375383f659aa3f9efa37a69d47e0c8f6ddb22b2bca5dc355074f2e879590109fd2f035c2aaa7d0e84f35734535e4346514bbfb1dff7d89
7
+ data.tar.gz: 81552abbaa6f568e6af21cb2e88fb0c98c831b602b54cae47883981d4d6eb2b568977e975ea31dd2af132af1c73a4e84eb9dcd8a1e5bf537e91afd3b4c63110d
data/README.md CHANGED
@@ -133,3 +133,33 @@ If you need to, you can get pretty fancy:
133
133
  use Rack::TrafficLogger, 'file.log', :request_headers, 401 => false, 500...600 => :all, 200...300 => {post: :request_bodies, delete: false}
134
134
  use Rack::TrafficLogger, 'file.log', [:get, :head] => 200..204, post: {only: {201 => :request_bodies}}, [:put, :patch] => :all
135
135
  ```
136
+
137
+ ### Tailing a JSON log
138
+
139
+ Tailing a JSON log can induce migraines. There are a couple of solutions:
140
+
141
+ #### Pipe it through the log parser
142
+
143
+ This gem is bundled with the `parse-rack-traffic-log` executable for this exact purpose.
144
+
145
+ ```bash
146
+ tail -f traffic.log | parse-rack-traffic-log
147
+ ```
148
+
149
+ This will let you tail a JSON log as if it were a regular log. You can add colors and/or JSON pretty printing using environment variables:
150
+
151
+ ```bash
152
+ tail -f traffic.log | PRETTY_PRINT=1 COLOR=1 parse-rack-traffic-log
153
+ ```
154
+
155
+ I haven't tested this with `less` but it should give the same result.
156
+
157
+ #### Use pretty-printing
158
+
159
+ You can make the JSON formatter output pretty:
160
+
161
+ ```ruby
162
+ use Rack::TrafficLogger, 'file.log', Rack::TrafficLogger::Formatter::JSON.new(pretty_print: true)
163
+ ```
164
+
165
+ Note that if you do, log parsers may have a hard time understanding your logs if they expect each event to be on a single line. If you think this could be an issue, use the first method instead.
@@ -0,0 +1,49 @@
1
+ require 'json'
2
+ require 'time'
3
+
4
+ module Rack
5
+ class TrafficLogger
6
+ class Reader
7
+
8
+ def self.start(input, output, **options)
9
+ new input, output, **options
10
+ end
11
+
12
+ def initialize(input, output, **options)
13
+ @input = input
14
+ @output = output
15
+ @formatter = Formatter::Stream.new(**options)
16
+ Signal.trap('INT') { exit 0 }
17
+ loop { readline }
18
+ end
19
+
20
+ # Reads a line from input, formats it, and sends it to output
21
+ def readline
22
+ buffer = ''
23
+ loop do
24
+ begin
25
+ buffer << @input.read_nonblock(1)
26
+ return writeline buffer if buffer[-1] == "\n"
27
+ rescue IO::EAGAINWaitReadable
28
+ sleep 0.1
29
+ rescue EOFError
30
+ exit 0
31
+ end
32
+ end
33
+ end
34
+
35
+ def writeline(line)
36
+ begin
37
+ hash = JSON.parse(line)
38
+ hash[:timestamp] = Time.parse(hash['timestamp'])
39
+ hash[:request_log_id] = hash['request_log_id']
40
+ hash[:event] = hash['event']
41
+ @output << @formatter.format(hash)
42
+ rescue
43
+ @output << line
44
+ end
45
+ end
46
+
47
+ end
48
+ end
49
+ end
@@ -48,7 +48,8 @@ module Rack
48
48
  result = render RESPONSE_TEMPLATES[@color],
49
49
  http: input['http_version'] || 'HTTP/1.1',
50
50
  code: input['status_code'],
51
- status: input['status_name']
51
+ status: input['status_name'],
52
+ color: status_color(input['status_code'])
52
53
  headers = input['headers']
53
54
  result << format_headers(headers) if headers
54
55
  result << format_body(input, headers && headers['Content-Type'])
@@ -82,6 +83,14 @@ module Rack
82
83
  env.select { |k, _| k =~ /^(CONTENT|HTTP)_(?!VERSION)/ }.map { |(k, v)| [k.sub(/^HTTP_/, ''), v] }.to_h
83
84
  end
84
85
 
86
+ def status_color(status)
87
+ case (status.to_i / 100).to_i
88
+ when 2 then '32m'
89
+ when 4, 5 then '31m'
90
+ else '33m'
91
+ end
92
+ end
93
+
85
94
  end
86
95
  end
87
96
  end
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class TrafficLogger
3
- VERSION = '0.2.0'
3
+ VERSION = '0.2.1'
4
4
  end
5
5
  end
@@ -3,6 +3,7 @@ require_relative 'traffic_logger/header_hash'
3
3
  require_relative 'traffic_logger/option_interpreter'
4
4
  require_relative 'traffic_logger/stream_simulator'
5
5
  require_relative 'traffic_logger/formatter'
6
+ require_relative 'traffic_logger/reader'
6
7
 
7
8
  require 'json'
8
9
  require 'securerandom'
@@ -44,7 +45,7 @@ module Rack
44
45
  if @log_path.respond_to? :write
45
46
  @log_path.write data
46
47
  else
47
- File.write @log_path, data, mode: 'a', encoding: data.encoding
48
+ ::File.write @log_path, data, mode: 'a', encoding: data.encoding
48
49
  end
49
50
  end
50
51
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-traffic-logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Neil E. Pearson
@@ -40,6 +40,7 @@ files:
40
40
  - lib/rack/traffic_logger/formatter/stream.rb
41
41
  - lib/rack/traffic_logger/header_hash.rb
42
42
  - lib/rack/traffic_logger/option_interpreter.rb
43
+ - lib/rack/traffic_logger/reader.rb
43
44
  - lib/rack/traffic_logger/stream_simulator.rb
44
45
  - lib/rack/traffic_logger/version.rb
45
46
  homepage: https://github.com/hx/rack-traffic-logger