steno 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,80 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", __FILE__)
4
+
5
+ require "bundler"
6
+ Bundler.setup
7
+
8
+ require "optparse"
9
+ require "yajl"
10
+
11
+ require "steno/json_prettifier"
12
+
13
+ def prettify_io(io, prettifier)
14
+ lineno = 0
15
+ io.each_line do |line|
16
+ begin
17
+ prettified = prettifier.prettify_line(line)
18
+ print line
19
+ rescue Yajl::ParseError => e
20
+ STDERR.puts "steno-prettify: Malformed json at line #{lineno}, '#{line.strip}'"
21
+ end
22
+
23
+ lineno += 1
24
+ end
25
+ end
26
+
27
+ def prettify_file(path, prettifier)
28
+ begin
29
+ f = File.open(path, "r")
30
+ rescue => e
31
+ STDERR.puts "steno-prettify: Failed opening '#{path}', #{e}"
32
+ return
33
+ end
34
+
35
+ prettify_io(f, prettifier)
36
+
37
+ ensure
38
+ f.close if f
39
+ end
40
+
41
+ option_parser = OptionParser.new do |op|
42
+ op.banner =<<EOT
43
+ Usage: steno-prettify [OPTS] [FILE...]
44
+
45
+ Parses json formatted log lines from FILE(s), or stdin,
46
+ and displays a more human friendly version of each line to stdout.
47
+
48
+ Examples (shamelessly stolen from `man cat`):
49
+
50
+ steno-prettify f - g
51
+ Prettify f's contents, then standard input, then g's contents.
52
+
53
+ steno-prettify
54
+ Prettify contents of stdin.
55
+
56
+ Options:
57
+ EOT
58
+
59
+ op.on("-h", "--help", "Display help") do
60
+ puts op
61
+ exit
62
+ end
63
+ end
64
+
65
+ option_parser.parse!(ARGV)
66
+
67
+ STDOUT.sync = true
68
+ prettifier = Steno::JsonPrettifier.new
69
+
70
+ inputs = ARGV.dup
71
+ inputs << "-" if inputs.empty?
72
+
73
+ inputs.each do |path|
74
+ if path == "-"
75
+ prettify_io(STDIN, prettifier)
76
+ else
77
+ prettify_file(path, prettifier)
78
+ end
79
+ end
80
+
@@ -0,0 +1,66 @@
1
+ require "digest/md5"
2
+ require "yajl"
3
+
4
+ module Steno
5
+ end
6
+
7
+ # Transforms JSON log lines into a more human readable format
8
+ class Steno::JsonPrettifier
9
+
10
+ attr_reader :time_format
11
+
12
+ def initialize
13
+ @time_format = "%Y-%m-%d %H:%M:%S.%6N"
14
+ end
15
+
16
+ def prettify_line(line)
17
+ json_record = Yajl::Parser.parse(line)
18
+
19
+ format_record(json_record)
20
+ end
21
+
22
+ private
23
+
24
+ def format_record(record)
25
+ timestamp = nil
26
+ if record.has_key?("timestamp")
27
+ timestamp = Time.at(record["timestamp"]).strftime(@time_format)
28
+ else
29
+ timestamp = "-"
30
+ end
31
+
32
+ log_level = nil
33
+ if record.has_key?("log_level")
34
+ log_level = record["log_level"].upcase
35
+ else
36
+ log_level = "-"
37
+ end
38
+
39
+ fields = [timestamp,
40
+ record["source"] || "-",
41
+ "pid=%s" % [record["process_id"] || "-"],
42
+ "tid=%s" % [shortid(record["thread_id"])],
43
+ "fid=%s" % [shortid(record["fiber_id"])],
44
+ "%s/%s:%s" % [record["file"] || "-",
45
+ record["method"] || "-",
46
+ record["lineno"] || "-"],
47
+ format_data(record["data"]),
48
+ "%7s" % [log_level],
49
+ "--",
50
+ record["message"] || "-"]
51
+
52
+ fields.join(" ") + "\n"
53
+ end
54
+
55
+ def format_data(data = {})
56
+ return "-" if data.empty?
57
+
58
+ data.map { |k, v| "#{k}=#{v}" }.join(",")
59
+ end
60
+
61
+ def shortid(data)
62
+ return "-" if data.nil?
63
+ digest = Digest::MD5.hexdigest(data.to_s)
64
+ digest[0, 4]
65
+ end
66
+ end
data/lib/steno/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Steno
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -0,0 +1,31 @@
1
+ require "spec_helper"
2
+
3
+ require "steno/json_prettifier"
4
+
5
+ describe Steno::JsonPrettifier do
6
+ let(:prettifier) { Steno::JsonPrettifier.new }
7
+ let(:codec) { Steno::Codec::Json.new }
8
+
9
+ describe "#prettify_line" do
10
+ it "should return a properly formatted string" do
11
+ record = Steno::Record.new("test", :info, "message",
12
+ ["filename", "line", "method"], "test" => "data")
13
+ encoded = codec.encode_record(record)
14
+ prettified = prettifier.prettify_line(encoded)
15
+
16
+ exp_regex = ['\d{4}-\d{2}-\d{2}', # YYYY-MM-DD
17
+ '\d{2}:\d{2}:\d{2}\.\d{6}', # HH:MM:SS.uS
18
+ 'test', # Source
19
+ 'pid=\d+', # Process id
20
+ 'tid=\w{4}', # Thread shortid
21
+ 'fid=\w{4}', # Fiber shortid
22
+ 'filename\/method:line', # Location
23
+ 'test=data', # User supplied data
24
+ '\s+INFO', # Level
25
+ '--',
26
+ 'message', # Log message
27
+ ].join(" ") + "\n"
28
+ prettified.should match(exp_regex)
29
+ end
30
+ end
31
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: steno
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-07-24 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: grape
16
- requirement: &15618460 !ruby/object:Gem::Requirement
16
+ requirement: &23171120 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *15618460
24
+ version_requirements: *23171120
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: yajl-ruby
27
- requirement: &15617860 !ruby/object:Gem::Requirement
27
+ requirement: &23168040 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *15617860
35
+ version_requirements: *23168040
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: ci_reporter
38
- requirement: &15617180 !ruby/object:Gem::Requirement
38
+ requirement: &23162620 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *15617180
46
+ version_requirements: *23162620
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rack-test
49
- requirement: &15616200 !ruby/object:Gem::Requirement
49
+ requirement: &23160780 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *15616200
57
+ version_requirements: *23160780
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rake
60
- requirement: &15615780 !ruby/object:Gem::Requirement
60
+ requirement: &23154300 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *15615780
68
+ version_requirements: *23154300
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rspec
71
- requirement: &15615360 !ruby/object:Gem::Requirement
71
+ requirement: &23151740 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,11 +76,12 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *15615360
79
+ version_requirements: *23151740
80
80
  description: A thread-safe logging library designed to support multiple log destinations.
81
81
  email:
82
82
  - mpage@rbcon.com
83
- executables: []
83
+ executables:
84
+ - steno-prettify
84
85
  extensions: []
85
86
  extra_rdoc_files: []
86
87
  files:
@@ -89,6 +90,7 @@ files:
89
90
  - LICENSE
90
91
  - README.md
91
92
  - Rakefile
93
+ - bin/steno-prettify
92
94
  - lib/steno.rb
93
95
  - lib/steno/codec.rb
94
96
  - lib/steno/codec/base.rb
@@ -98,6 +100,7 @@ files:
98
100
  - lib/steno/core_ext.rb
99
101
  - lib/steno/errors.rb
100
102
  - lib/steno/http_handler.rb
103
+ - lib/steno/json_prettifier.rb
101
104
  - lib/steno/log_level.rb
102
105
  - lib/steno/logger.rb
103
106
  - lib/steno/record.rb
@@ -117,6 +120,7 @@ files:
117
120
  - spec/unit/http_handler_spec.rb
118
121
  - spec/unit/io_sink_spec.rb
119
122
  - spec/unit/json_codec_spec.rb
123
+ - spec/unit/json_prettifier_spec.rb
120
124
  - spec/unit/log_level_spec.rb
121
125
  - spec/unit/logger_spec.rb
122
126
  - spec/unit/record_spec.rb
@@ -159,6 +163,7 @@ test_files:
159
163
  - spec/unit/http_handler_spec.rb
160
164
  - spec/unit/io_sink_spec.rb
161
165
  - spec/unit/json_codec_spec.rb
166
+ - spec/unit/json_prettifier_spec.rb
162
167
  - spec/unit/log_level_spec.rb
163
168
  - spec/unit/logger_spec.rb
164
169
  - spec/unit/record_spec.rb