rack-traffic-logger 0.0.1 → 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: 2ca028e1a99d48ccc15d12bbe7173a46b1b10c8c
4
- data.tar.gz: 59f69d7fd8ea9ba939d58f9620f68fb7fade8747
3
+ metadata.gz: 2eb0252e73380f08bce1a1fd885b7d1274efb5ea
4
+ data.tar.gz: 52c68544ff792ba5a11b98d6eef256f5dedf2bf6
5
5
  SHA512:
6
- metadata.gz: b404dc7d0092b9c5e3945ac38b992e14eb3cbfa2a204cfb6c60c495ae57235be3cca7631a0174914af25a2fd05ab037612523f59b5ff40aba16469d6e9125364
7
- data.tar.gz: 3838f643eae3525c4ca23e3a9602634b8f41958f268f2673627c673de07e844ba558faa9e4e8c9c98b9744c47c9d58e5ebe2fc2964643df418e8a23896fa51b3
6
+ metadata.gz: 3d4540743bb8c876d8dda6196b32d548044bfac62f7736704f2aa7cef441749debaced16d1fb62a6988b89e272bb67e542b8f43755dd35d77a13babd925deb31
7
+ data.tar.gz: e4486d2c17aff30b74771273df3a5095dce62c1621101dad5549aca5cce7ac8d116a0d47c11395e87c617c874074fe82e73638207a1bd835bcc6986044017d09
@@ -51,18 +51,21 @@ module Rack
51
51
  @default_options ||= PUBLIC_ATTRIBUTES.map { |k, v| [k, v[:default]] }.to_h
52
52
  end
53
53
 
54
+ def render(template, data)
55
+ template.gsub(/:(\w+)/) { data[$1.to_sym] }
56
+ end
57
+
54
58
  REQUEST_TEMPLATES = {
55
- true => "%s %s%s HTTP/%s",
56
- false => "%s %s%s HTTP/%s"
59
+ true => "\e[35m:verb \e[36m:path:qs\e[0m :http",
60
+ false => ':verb :path:qs :http'
57
61
  }
58
62
 
59
63
  def log_request!(env)
60
- debug REQUEST_TEMPLATES[colors] % [
61
- env['REQUEST_METHOD'],
62
- env['PATH_INFO'],
63
- (q = env['QUERY_STRING']).empty? ? '' : "?#{q}",
64
- env['HTTP_VERSION'] || '1.1'
65
- ]
64
+ debug render REQUEST_TEMPLATES[colors],
65
+ verb: env['REQUEST_METHOD'],
66
+ path: env['PATH_INFO'],
67
+ qs: (q = env['QUERY_STRING']).empty? ? '' : "?#{q}",
68
+ http: env['HTTP_VERSION'] || 'HTTP/1.1'
66
69
  log_headers! env_request_headers(env) if request_headers
67
70
  input = env['rack.input']
68
71
  if request_bodies && input
@@ -72,16 +75,24 @@ module Rack
72
75
  end
73
76
 
74
77
  RESPONSE_TEMPLATES = {
75
- true => "HTTP/%s %s %s",
76
- false => "HTTP/%s %s %s"
78
+ true => ":http \e[:color:code \e[36m:status\e[0m",
79
+ false => ':http :code :status'
77
80
  }
78
81
 
82
+ def status_color(status)
83
+ case (status / 100).to_i
84
+ when 2 then '32m'
85
+ when 4, 5 then '31m'
86
+ else '33m'
87
+ end
88
+ end
89
+
79
90
  def log_response!(env, response)
80
- debug RESPONSE_TEMPLATES[colors] % [
81
- env['HTTP_VERSION'] || '1.1',
82
- code = response[0],
83
- Rack::Utils::HTTP_STATUS_CODES[code]
84
- ]
91
+ debug render RESPONSE_TEMPLATES[colors],
92
+ http: env['HTTP_VERSION'] || 'HTTP/1.1',
93
+ code: code = response[0],
94
+ status: Rack::Utils::HTTP_STATUS_CODES[code],
95
+ color: status_color(code)
85
96
  log_headers! response[1] if response_headers
86
97
  log_body! response[2].join if response_bodies
87
98
  end
@@ -91,17 +102,17 @@ module Rack
91
102
  end
92
103
 
93
104
  HEADER_TEMPLATES = {
94
- true => "%s: %s\n",
95
- false => "%s: %s\n"
105
+ true => "\e[4m:key\e[0m: :val\n",
106
+ false => ":key: :val\n"
96
107
  }
97
108
 
98
109
  def log_headers!(headers)
99
- info headers.map { |k, v| HEADER_TEMPLATES[colors] % [k, v] }.join
110
+ info headers.map { |k, v| render HEADER_TEMPLATES[colors], key: k, val: v }.join
100
111
  end
101
112
 
102
113
  def env_request_headers(env)
103
114
  env.select do |k, _|
104
- k =~ /^(CONTENT|HTTP)_/
115
+ k =~ /^(CONTENT|HTTP)_(?!VERSION)/
105
116
  end.map do |(k, v)|
106
117
  [
107
118
  k.sub(/^HTTP_/, '').split(/[_ -]/).map do |word|
@@ -5,7 +5,15 @@ module Rack
5
5
  class TrafficLogger
6
6
  class Echo
7
7
  def call(env)
8
- JSON.parse env['rack.input'].tap(&:rewind).read
8
+ begin
9
+ JSON.parse env['rack.input'].tap(&:rewind).read
10
+ rescue JSON::ParserError => error
11
+ [
12
+ 500,
13
+ {'Content-Type' => 'text/plain;charset=UTF-8'},
14
+ [error.message]
15
+ ]
16
+ end
9
17
  end
10
18
  end
11
19
  end
@@ -4,6 +4,22 @@ module Rack
4
4
  class TrafficLogger
5
5
  class Logger < ::Logger
6
6
 
7
+ def initialize(*args)
8
+ super *args
9
+ @default_formatter = Formatter.new
10
+ end
11
+
12
+ class Formatter
13
+
14
+ def call(severity, time, progname, msg)
15
+ if severity == 'INFO'
16
+ "#{msg}\n"
17
+ else
18
+ "@ #{time.strftime '%a %d %b \'%y %T'}.#{'%d' % (time.usec / 1e4)}\n#{msg}\n"
19
+ end
20
+ end
21
+
22
+ end
7
23
  end
8
24
  end
9
25
  end
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class TrafficLogger
3
- VERSION = '0.0.1'
3
+ VERSION = '0.1.0'
4
4
  end
5
5
  end
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.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Neil E. Pearson