rack-json-logs 1.0 → 1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 15003ab04d1540237ac9c0ad90b88bfc6fac75d3
4
+ data.tar.gz: 3065e8f738bd77fe534fbca3db4eac7c62d8d77b
5
+ SHA512:
6
+ metadata.gz: ba81cc889443d56fcc2ba7edc2e8ffd6d78f31ce4a746f3e70031e28ae375d3e31dff7cd6bd79df9a6b29a7ce857deaeb560c683f9daf48551aadb211ca6f4cb
7
+ data.tar.gz: 43aa158a1cf918fa32fb603a7e9f478d1d7a8736cc71bcb060d372ee95196c9817277c3d3000014c5c42d2834ff9aa4ca8f499c16bdbf91dd7ec2518e21a7510
data/README.md CHANGED
@@ -121,6 +121,11 @@ Using the command line tool is also easy. Output can be configured, see:
121
121
 
122
122
  ## Changelog
123
123
 
124
+ #### 1.1
125
+
126
+ - New feature: can pretty print from middleware, for development usage.
127
+ - Refactoring of binary pretty print code into library.
128
+
124
129
  #### 1.0
125
130
 
126
131
  Production release. Deployed and used in a large-scale system.
data/bin/json-log-pp CHANGED
@@ -1,9 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'json'
4
- require 'colorize'
5
- require 'stringio'
6
4
  require 'trollop'
5
+ require File.expand_path('../../lib/rack-json-logs/pretty-printer.rb', __FILE__)
7
6
 
8
7
  opts = Trollop.options do
9
8
  opt :stdout, 'Print stdout.', default: false, short: 'o'
@@ -27,64 +26,12 @@ while line = STDIN.gets
27
26
 
28
27
  json = json['data'] if json && json['data']
29
28
 
29
+ # only pretty print valid objects
30
30
  if json && json.is_a?(Hash) && json['request']
31
-
32
- out = StringIO.new
33
-
34
- status_color = case json['status']
35
- when 200...300; :green
36
- when 300...600; :red
37
- else :cyan; end
38
-
39
-
40
- resp = 'Response: '
41
- resp << "#{json['status']} " if json['status']
42
- resp << "(#{(json['duration']*1000).round}ms) " if opts[:duration] && json['duration']
43
-
44
- out.puts ' * * *'
45
- out.puts
46
- out.puts "Request: #{json['request']}".cyan
47
- out.puts resp.send(status_color)
48
- out.puts "From: #{json['from']}".cyan if opts[:from] && json['from']
49
- out.puts "At: #{Time.at(json['time']).strftime('%b %-e %Y, %-l:%M%P')}"
50
- out.puts
51
-
52
- %w{stdout stderr}.each do |b|
53
- next unless opts[b.to_sym]
54
- color = b == 'stdout' ? :green : :yellow
55
- log = json[b] || ''
56
- if log == '' || log == "\n"
57
- out.puts "No #{b}.".send(color)
58
- else
59
- out.puts "#{b}:".cyan
60
- out.puts log.send(color)
61
- end
62
- # Typically log statements start end with a \n, so skiping puts here.
63
- end
64
-
65
- if opts[:events] && json['events'] && !json['events'].empty?
66
- out.puts 'Events:'.cyan
67
- out.puts
68
- json['events'].each do |e|
69
- out.puts "Event: #{e['type']}"
70
- out.puts "At: #{(e['time']*1000).round}ms"
71
- out.puts "Value: #{e['value'].to_json}"
72
- out.puts
73
- end
74
- end
75
-
76
- if json['exception']
77
- out.puts "Exception: #{json['exception']['message']}".red
78
- out.puts json['exception']['backtrace'].map {|e| " #{e}"}.join("\n").blue if opts[:trace]
79
- out.puts
80
- end
81
-
82
- STDOUT.print(out.string)
83
-
31
+ Rack::JsonLogs.pretty_print(json, STDOUT, opts)
84
32
  else
85
33
  puts line
86
34
  end
87
-
88
35
  end
89
36
 
90
37
  puts "\nEOF.".red
@@ -1,4 +1,5 @@
1
1
  require 'rack-json-logs/version'
2
+ require 'rack-json-logs/pretty-printer.rb'
2
3
  require 'json'
3
4
  require 'stringio'
4
5
  require 'socket'
@@ -21,12 +22,24 @@ module Rack
21
22
  # for example, you want to log which server the request is from. Defaults
22
23
  # to the machine's hostname.
23
24
  #
25
+ # :pretty_print
26
+ #
27
+ # When set to true, this will pretty-print the logs, instead of printing
28
+ # the json. This is useful in development.
29
+ #
30
+ # :print_options
31
+ #
32
+ # When :pretty_print is set to true, these options will be passed to the
33
+ # pretty-printer. Run `json-logs-pp -h` to see what the options are.
34
+ #
24
35
  class JsonLogs
25
36
 
26
37
  def initialize(app, options={})
27
38
  @app = app
28
39
  @options = {
29
- reraise_exceptions: false
40
+ reraise_exceptions: false,
41
+ pretty_print: false,
42
+ print_options: {trace: true},
30
43
  }.merge(options)
31
44
  @options[:from] ||= Socket.gethostname
32
45
  end
@@ -64,7 +77,13 @@ module Rack
64
77
  backtrace: exception.backtrace
65
78
  }
66
79
  end
67
- STDOUT.puts(log.to_json)
80
+
81
+ if @options[:pretty_print]
82
+ JsonLogs.pretty_print(JSON.parse(log.to_json),
83
+ STDOUT, @options[:print_options])
84
+ else
85
+ STDOUT.puts(log.to_json)
86
+ end
68
87
 
69
88
  raise exception if exception && @options[:reraise_exceptions]
70
89
 
@@ -0,0 +1,77 @@
1
+ require 'colorize'
2
+ require 'stringio'
3
+
4
+ module Rack
5
+ class JsonLogs
6
+
7
+ def self.pretty_print(json, print_to = STDOUT, opts = {})
8
+
9
+ opts = {
10
+ stdout: false,
11
+ stderr: false,
12
+ from: true,
13
+ trace: false,
14
+ duration: true,
15
+ events: false,
16
+ }.merge(opts)
17
+
18
+ out = StringIO.new
19
+
20
+ status_color = case json['status']
21
+ when 200...300; :green
22
+ when 300...600; :red
23
+ else :cyan; end
24
+
25
+
26
+ resp = 'Response: '
27
+ resp << "#{json['status']} " if json['status']
28
+ if opts[:duration] && json['duration']
29
+ resp << "(#{(json['duration']*1000).round}ms) "
30
+ end
31
+
32
+ out.puts ' * * *'
33
+ out.puts
34
+ out.puts "Request: #{json['request']}".cyan
35
+ out.puts resp.send(status_color)
36
+ out.puts "From: #{json['from']}".cyan if opts[:from] && json['from']
37
+ out.puts "At: #{Time.at(json['time']).strftime('%b %-e %Y, %-l:%M%P')}"
38
+ out.puts
39
+
40
+ %w{stdout stderr}.each do |b|
41
+ next unless opts[b.to_sym]
42
+ color = b == 'stdout' ? :green : :yellow
43
+ log = json[b] || ''
44
+ if log == '' || log == "\n"
45
+ out.puts "No #{b}.".send(color)
46
+ else
47
+ out.puts "#{b}:".cyan
48
+ out.puts log.send(color)
49
+ end
50
+ # Typically log statements start end with a \n, so skiping puts here.
51
+ end
52
+
53
+ if opts[:events] && json['events'] && !json['events'].empty?
54
+ out.puts 'Events:'.cyan
55
+ out.puts
56
+ json['events'].each do |e|
57
+ out.puts "Event: #{e['type']}"
58
+ out.puts "At: #{(e['time']*1000).round}ms"
59
+ out.puts "Value: #{e['value'].to_json}"
60
+ out.puts
61
+ end
62
+ end
63
+
64
+ if json['exception']
65
+ out.puts "Exception: #{json['exception']['message']}".red
66
+ if opts[:trace]
67
+ out.puts json['exception']['backtrace'].map {|e| " #{e}"}.join("\n").blue
68
+ end
69
+ out.puts
70
+ end
71
+
72
+ print_to.print(out.string)
73
+
74
+
75
+ end
76
+ end
77
+ end
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class JsonLogs
3
- VERSION = '1.0'
3
+ VERSION = '1.1'
4
4
  end
5
5
  end
data/test/config.ru CHANGED
@@ -4,7 +4,7 @@
4
4
  $: << File.expand_path('../lib/', __FILE__)
5
5
  require 'rack-json-logs'
6
6
 
7
- use Rack::JsonLogs
7
+ use Rack::JsonLogs, pretty_print: true
8
8
 
9
9
  run ->(env) do
10
10
  puts "hello world"
metadata CHANGED
@@ -1,62 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-json-logs
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.0'
5
- prerelease:
4
+ version: '1.1'
6
5
  platform: ruby
7
6
  authors:
8
7
  - Kenneth Ballenegger
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-10-07 00:00:00.000000000 Z
11
+ date: 2014-08-14 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: json
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - ">="
28
25
  - !ruby/object:Gem::Version
29
26
  version: '0'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: colorize
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - ">="
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :runtime
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - ">="
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: trollop
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - ">="
52
46
  - !ruby/object:Gem::Version
53
47
  version: '0'
54
48
  type: :runtime
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - ">="
60
53
  - !ruby/object:Gem::Version
61
54
  version: '0'
62
55
  description: Rack::JsonLogs is a gem that helps log sanely in production.
@@ -67,13 +60,14 @@ executables:
67
60
  extensions: []
68
61
  extra_rdoc_files: []
69
62
  files:
70
- - .gitignore
63
+ - ".gitignore"
71
64
  - Gemfile
72
65
  - LICENSE.txt
73
66
  - README.md
74
67
  - Rakefile
75
68
  - bin/json-log-pp
76
69
  - lib/rack-json-logs.rb
70
+ - lib/rack-json-logs/pretty-printer.rb
77
71
  - lib/rack-json-logs/version.rb
78
72
  - rack-json-logs.gemspec
79
73
  - test/.gitignore
@@ -81,27 +75,26 @@ files:
81
75
  - test/log
82
76
  homepage: ''
83
77
  licenses: []
78
+ metadata: {}
84
79
  post_install_message:
85
80
  rdoc_options: []
86
81
  require_paths:
87
82
  - lib
88
83
  required_ruby_version: !ruby/object:Gem::Requirement
89
- none: false
90
84
  requirements:
91
- - - ! '>='
85
+ - - ">="
92
86
  - !ruby/object:Gem::Version
93
87
  version: '0'
94
88
  required_rubygems_version: !ruby/object:Gem::Requirement
95
- none: false
96
89
  requirements:
97
- - - ! '>='
90
+ - - ">="
98
91
  - !ruby/object:Gem::Version
99
92
  version: '0'
100
93
  requirements: []
101
94
  rubyforge_project:
102
- rubygems_version: 1.8.24
95
+ rubygems_version: 2.2.2
103
96
  signing_key:
104
- specification_version: 3
97
+ specification_version: 4
105
98
  summary: Rack::JsonLogs is a gem that helps log sanely in production.
106
99
  test_files:
107
100
  - test/.gitignore