steno 0.0.8 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
data/bin/steno-prettify CHANGED
@@ -10,6 +10,8 @@ require "yajl"
10
10
 
11
11
  require "steno/json_prettifier"
12
12
 
13
+ Signal.trap("INT") { exit }
14
+
13
15
  def prettify_io(io, prettifier)
14
16
  lineno = 0
15
17
  io.each_line do |line|
@@ -38,6 +40,8 @@ ensure
38
40
  f.close if f
39
41
  end
40
42
 
43
+ excluded_fields = []
44
+
41
45
  option_parser = OptionParser.new do |op|
42
46
  op.banner =<<EOT
43
47
  Usage: steno-prettify [OPTS] [FILE...]
@@ -60,12 +64,16 @@ EOT
60
64
  puts op
61
65
  exit
62
66
  end
67
+
68
+ op.on("-a", "--align", "Omit location and data in order to provide well-aligned logs") do
69
+ excluded_fields = %w[location data]
70
+ end
63
71
  end
64
72
 
65
73
  option_parser.parse!(ARGV)
66
74
 
67
75
  STDOUT.sync = true
68
- prettifier = Steno::JsonPrettifier.new
76
+ prettifier = Steno::JsonPrettifier.new(excluded_fields)
69
77
 
70
78
  inputs = ARGV.dup
71
79
  inputs << "-" if inputs.empty?
@@ -1,4 +1,5 @@
1
1
  require "digest/md5"
2
+ require "set"
2
3
  require "yajl"
3
4
 
4
5
  module Steno
@@ -6,11 +7,12 @@ end
6
7
 
7
8
  # Transforms JSON log lines into a more human readable format
8
9
  class Steno::JsonPrettifier
10
+ FIELD_ORDER = %w[timestamp source process_id thread_id fiber_id location data
11
+ log_level message]
9
12
 
10
- attr_reader :time_format
11
-
12
- def initialize
13
+ def initialize(excluded_fields = [])
13
14
  @time_format = "%Y-%m-%d %H:%M:%S.%6N"
15
+ @excluded_fields = Set.new(excluded_fields)
14
16
  end
15
17
 
16
18
  def prettify_line(line)
@@ -19,55 +21,79 @@ class Steno::JsonPrettifier
19
21
  format_record(json_record)
20
22
  end
21
23
 
22
- private
24
+ protected
23
25
 
24
26
  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 = "-"
27
+ fields = []
28
+
29
+ FIELD_ORDER.each do |field_name|
30
+ next if @excluded_fields.include?(field_name)
31
+
32
+ exists = nil
33
+ pred_meth = "check_#{field_name}".to_sym
34
+ if respond_to?(pred_meth)
35
+ exists = send(pred_meth, record)
36
+ else
37
+ exists = record.has_key?(field_name)
38
+ end
39
+
40
+ if exists
41
+ fields << send("format_#{field_name}".to_sym, record)
42
+ else
43
+ fields << "-"
44
+ end
30
45
  end
31
46
 
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
47
+ fields.join(" ") + "\n"
48
+ end
38
49
 
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" % [trim_filename(record["file"]),
45
- record["method"] || "-",
46
- record["lineno"] || "-"],
47
- format_data(record["data"]),
48
- "%7s" % [log_level],
49
- "--",
50
- record["message"] || "-"]
50
+ def format_timestamp(record)
51
+ Time.at(record["timestamp"]).strftime(@time_format)
52
+ end
51
53
 
52
- fields.join(" ") + "\n"
54
+ def format_source(record)
55
+ "%14s" % record["source"]
53
56
  end
54
57
 
55
- def trim_filename(path)
56
- return "-" if path.nil?
58
+ def format_process_id(record)
59
+ "pid=%-5s" % [record["process_id"]]
60
+ end
57
61
 
58
- parts = path.split("/")
62
+ def format_thread_id(record)
63
+ "tid=%s" % [shortid(record["thread_id"])]
64
+ end
59
65
 
66
+ def format_fiber_id(record)
67
+ "fid=%s" % [shortid(record["fiber_id"])]
68
+ end
69
+
70
+ def check_location(record)
71
+ %w[file lineno method].reduce(true) { |ok, k| ok && record.has_key?(k) }
72
+ end
73
+
74
+ def format_location(record)
75
+ parts = record["file"].split("/")
76
+
77
+ trimmed_filename = nil
60
78
  if parts.size == 1
61
- parts[0]
79
+ trimmed_filename = parts[0]
62
80
  else
63
- parts.slice(-2, 2).join("/")
81
+ trimmed_filename = parts.slice(-2, 2).join("/")
64
82
  end
83
+
84
+ "%s/%s:%s" % [trimmed_filename, record["method"], record["lineno"]]
85
+ end
86
+
87
+ def format_data(record)
88
+ record["data"].map { |k, v| "#{k}=#{v}" }.join(",")
65
89
  end
66
90
 
67
- def format_data(data = {})
68
- return "-" if data.empty?
91
+ def format_log_level(record)
92
+ "%7s" % [record["log_level"].upcase]
93
+ end
69
94
 
70
- data.map { |k, v| "#{k}=#{v}" }.join(",")
95
+ def format_message(record)
96
+ "-- %s" % [record["message"]]
71
97
  end
72
98
 
73
99
  def shortid(data)
data/lib/steno/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Steno
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.10"
3
3
  end
@@ -21,10 +21,10 @@ describe Steno::JsonPrettifier do
21
21
  'fid=\w{4}', # Fiber shortid
22
22
  'filename\/method:line', # Location
23
23
  'test=data', # User supplied data
24
- '\s+INFO', # Level
24
+ 'INFO', # Level
25
25
  '--',
26
26
  'message', # Log message
27
- ].join(" ") + "\n"
27
+ ].join("\s+") + "\n"
28
28
  prettified.should match(exp_regex)
29
29
  end
30
30
  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.8
4
+ version: 0.0.10
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-24 00:00:00.000000000Z
12
+ date: 2012-07-25 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: grape
16
- requirement: &8836080 !ruby/object:Gem::Requirement
16
+ requirement: &18015280 !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: *8836080
24
+ version_requirements: *18015280
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: yajl-ruby
27
- requirement: &8831880 !ruby/object:Gem::Requirement
27
+ requirement: &18014180 !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: *8831880
35
+ version_requirements: *18014180
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: ci_reporter
38
- requirement: &8831240 !ruby/object:Gem::Requirement
38
+ requirement: &18010240 !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: *8831240
46
+ version_requirements: *18010240
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rack-test
49
- requirement: &8830680 !ruby/object:Gem::Requirement
49
+ requirement: &17993800 !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: *8830680
57
+ version_requirements: *17993800
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rake
60
- requirement: &8829840 !ruby/object:Gem::Requirement
60
+ requirement: &17989080 !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: *8829840
68
+ version_requirements: *17989080
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rspec
71
- requirement: &8829060 !ruby/object:Gem::Requirement
71
+ requirement: &17978960 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,7 +76,7 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *8829060
79
+ version_requirements: *17978960
80
80
  description: A thread-safe logging library designed to support multiple log destinations.
81
81
  email:
82
82
  - mpage@rbcon.com