steno 0.0.12 → 0.0.15

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.
data/bin/steno-prettify CHANGED
@@ -12,21 +12,25 @@ require "steno/json_prettifier"
12
12
 
13
13
  Signal.trap("INT") { exit }
14
14
 
15
- def prettify_io(io, prettifier)
15
+ def prettify_io(io, prettifier, ignore_parse_error = false)
16
16
  lineno = 0
17
17
  io.each_line do |line|
18
18
  begin
19
19
  prettified = prettifier.prettify_line(line)
20
20
  print prettified
21
- rescue Yajl::ParseError => e
22
- STDERR.puts "steno-prettify: Malformed json at line #{lineno}, '#{line.strip}'"
21
+ rescue Steno::JsonPrettifier::ParseError => e
22
+ if ignore_parse_error
23
+ print line
24
+ else
25
+ STDERR.puts "steno-prettify: Malformed json at line #{lineno}, '#{line.strip}'"
26
+ end
23
27
  end
24
28
 
25
29
  lineno += 1
26
30
  end
27
31
  end
28
32
 
29
- def prettify_file(path, prettifier)
33
+ def prettify_file(path, prettifier, ignore_parse_error = false)
30
34
  begin
31
35
  f = File.open(path, "r")
32
36
  rescue => e
@@ -34,13 +38,14 @@ def prettify_file(path, prettifier)
34
38
  return
35
39
  end
36
40
 
37
- prettify_io(f, prettifier)
41
+ prettify_io(f, prettifier, ignore_parse_error)
38
42
 
39
43
  ensure
40
44
  f.close if f
41
45
  end
42
46
 
43
47
  excluded_fields = []
48
+ ignore_parse_error = false
44
49
 
45
50
  option_parser = OptionParser.new do |op|
46
51
  op.banner =<<EOT
@@ -68,6 +73,10 @@ EOT
68
73
  op.on("-a", "--align", "Omit location and data in order to provide well-aligned logs") do
69
74
  excluded_fields = %w[location data]
70
75
  end
76
+
77
+ op.on("-s", "--no-messages", "Donot complain about errors in parsing logs") do
78
+ ignore_parse_error = true
79
+ end
71
80
  end
72
81
 
73
82
  option_parser.parse!(ARGV)
@@ -80,9 +89,8 @@ inputs << "-" if inputs.empty?
80
89
 
81
90
  inputs.each do |path|
82
91
  if path == "-"
83
- prettify_io(STDIN, prettifier)
92
+ prettify_io(STDIN, prettifier, ignore_parse_error)
84
93
  else
85
- prettify_file(path, prettifier)
94
+ prettify_file(path, prettifier, ignore_parse_error)
86
95
  end
87
96
  end
88
-
@@ -1,5 +1,11 @@
1
- class Class
1
+ class Module
2
2
  def logger
3
3
  Steno.logger(name)
4
4
  end
5
5
  end
6
+
7
+ class Object
8
+ def logger
9
+ self.class.logger
10
+ end
11
+ end
@@ -10,13 +10,20 @@ class Steno::JsonPrettifier
10
10
  FIELD_ORDER = %w[timestamp source process_id thread_id fiber_id location data
11
11
  log_level message]
12
12
 
13
+ class ParseError < StandardError
14
+ end
15
+
13
16
  def initialize(excluded_fields = [])
14
17
  @time_format = "%Y-%m-%d %H:%M:%S.%6N"
15
18
  @excluded_fields = Set.new(excluded_fields)
16
19
  end
17
20
 
18
21
  def prettify_line(line)
19
- json_record = Yajl::Parser.parse(line)
22
+ begin
23
+ json_record = Yajl::Parser.parse(line)
24
+ rescue Yajl::ParseError => e
25
+ raise ParseError, e.to_s
26
+ end
20
27
 
21
28
  format_record(json_record)
22
29
  end
@@ -24,6 +31,7 @@ class Steno::JsonPrettifier
24
31
  protected
25
32
 
26
33
  def format_record(record)
34
+ record ||= {}
27
35
  fields = []
28
36
 
29
37
  FIELD_ORDER.each do |field_name|
@@ -33,8 +41,11 @@ class Steno::JsonPrettifier
33
41
  pred_meth = "check_#{field_name}".to_sym
34
42
  if respond_to?(pred_meth)
35
43
  exists = send(pred_meth, record)
36
- else
44
+ elsif record.respond_to?(:has_key?)
37
45
  exists = record.has_key?(field_name)
46
+ else
47
+ msg = "Expected the record to be a hash, but received: #{record.class}."
48
+ raise ParseError, msg
38
49
  end
39
50
 
40
51
  if exists
data/lib/steno/record.rb CHANGED
@@ -28,7 +28,7 @@ class Steno::Record
28
28
  @timestamp = Time.now
29
29
  @source = source
30
30
  @log_level = log_level
31
- @message = message
31
+ @message = message.to_s
32
32
  @data = {}.merge(data)
33
33
  @thread_id = Thread.current.object_id
34
34
  @fiber_id = Fiber.current.object_id
@@ -31,8 +31,11 @@ class Steno::Sink::Syslog < Steno::Sink::Base
31
31
 
32
32
  def add_record(record)
33
33
  msg = @codec.encode_record(record)
34
- pri = LOG_LEVEL_MAP[record.log_level]
34
+ pri = LOG_LEVEL_MAP[record.log_level.name]
35
35
  @syslog_lock.synchronize { @syslog.log(pri, "%s", msg) }
36
36
  end
37
37
 
38
+ def flush
39
+ nil
40
+ end
38
41
  end
data/lib/steno/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Steno
2
- VERSION = "0.0.12"
2
+ VERSION = "0.0.15"
3
3
  end
@@ -1,20 +1,38 @@
1
1
  require "spec_helper"
2
2
 
3
+ require "steno/core_ext"
4
+
3
5
  module Foo
4
6
  class Bar
5
7
  end
6
8
  end
7
9
 
8
- describe Class do
10
+ describe Module do
9
11
  describe "#logger" do
10
- it "should request a logger named after the class" do
11
- expect do
12
- Foo::Bar.logger
13
- end.to raise_error(/undefined/)
12
+ it "should request a logger named after itself" do
13
+ x = Foo.logger
14
+ x.should be_a(Steno::Logger)
15
+ x.name.should include("Foo")
16
+ end
17
+ end
18
+ end
14
19
 
15
- require "steno/core_ext"
20
+ describe Class do
21
+ describe "#logger" do
22
+ it "should request a logger named after itself" do
16
23
  x = Foo::Bar.logger
17
- x.should_not be_nil
24
+ x.should be_a(Steno::Logger)
25
+ x.name.should include("Foo::Bar")
26
+ end
27
+ end
28
+ end
29
+
30
+ describe Object do
31
+ describe "#logger" do
32
+ it "should request a logger named after its class" do
33
+ x = Foo::Bar.new.logger
34
+ x.should be_a(Steno::Logger)
35
+ x.name.should include("Foo::Bar")
18
36
  end
19
37
  end
20
38
  end
@@ -27,5 +27,17 @@ describe Steno::JsonPrettifier do
27
27
  ].join("\s+") + "\n"
28
28
  prettified.should match(exp_regex)
29
29
  end
30
+
31
+ it "should raise a parse error when the json-encoded string is not a hash" do
32
+ expect {
33
+ prettifier.prettify_line("[1,2,3]")
34
+ }.to raise_error(Steno::JsonPrettifier::ParseError)
35
+ end
36
+
37
+ it "should raise a parse error when the json-encoded string is malformed" do
38
+ expect {
39
+ prettifier.prettify_line("blah")
40
+ }.to raise_error(Steno::JsonPrettifier::ParseError)
41
+ end
30
42
  end
31
43
  end
@@ -1,7 +1,8 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Steno::Record do
4
- let(:record) { Steno::Record.new("test", :info, "test message") }
4
+ let(:message) { Array("test message") }
5
+ let(:record) { Steno::Record.new("test", :info, message) }
5
6
 
6
7
  it "should set the process id" do
7
8
  record.process_id.should == Process.pid
@@ -18,4 +19,8 @@ describe Steno::Record do
18
19
  it "should set the source" do
19
20
  record.source.should == "test"
20
21
  end
22
+
23
+ it "should stringify the message" do
24
+ record.message.should be_a(String)
25
+ end
21
26
  end
@@ -1,15 +1,21 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Steno::Sink::IO do
4
- let(:record) { { :data => "test" } }
4
+ let(:level) do
5
+ Steno::Logger.lookup_level(:info)
6
+ end
7
+
8
+ let(:record) do
9
+ Steno::Record.new("source", level, "message")
10
+ end
5
11
 
6
12
  describe "#add_record" do
7
13
  it "should encode the record and write it to the underlying io object" do
8
14
  codec = mock("codec")
9
- codec.should_receive(:encode_record).with(record).and_return(record[:data])
15
+ codec.should_receive(:encode_record).with(record).and_return(record.message)
10
16
 
11
17
  io = mock("io")
12
- io.should_receive(:write).with(record[:data])
18
+ io.should_receive(:write).with(record.message)
13
19
 
14
20
  Steno::Sink::IO.new(io, codec).add_record(record)
15
21
  end
@@ -1,6 +1,14 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Steno::Sink::Syslog do
4
+ let(:level) do
5
+ Steno::Logger.lookup_level(:info)
6
+ end
7
+
8
+ let(:record) do
9
+ Steno::Record.new("source", level, "message")
10
+ end
11
+
4
12
  describe "#add_record" do
5
13
  it "should append an encoded record with the correct priority" do
6
14
  identity = "test"
@@ -13,15 +21,19 @@ describe Steno::Sink::Syslog do
13
21
  sink = Steno::Sink::Syslog.instance
14
22
  sink.open(identity)
15
23
 
16
- record = Steno::Record.new("test", :info, "hello")
17
-
18
24
  codec = mock("codec")
19
- codec.should_receive(:encode_record).with(record).and_return("test")
25
+ codec.should_receive(:encode_record).with(record).and_return(record.message)
20
26
  sink.codec = codec
21
27
 
22
- syslog.should_receive(:log).with(Syslog::LOG_INFO, "%s", "test")
28
+ syslog.should_receive(:log).with(Syslog::LOG_INFO, "%s", record.message)
23
29
 
24
30
  sink.add_record(record)
25
31
  end
26
32
  end
33
+
34
+ describe "#flush" do
35
+ it "should do nothing" do
36
+ Steno::Sink::Syslog.instance.flush
37
+ end
38
+ end
27
39
  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.12
4
+ version: 0.0.15
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-27 00:00:00.000000000 Z
12
+ date: 2012-08-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: grape
@@ -148,14 +148,14 @@ files:
148
148
  - spec/unit/context_spec.rb
149
149
  - spec/unit/core_ext_spec.rb
150
150
  - spec/unit/http_handler_spec.rb
151
- - spec/unit/io_sink_spec.rb
152
151
  - spec/unit/json_codec_spec.rb
153
152
  - spec/unit/json_prettifier_spec.rb
154
153
  - spec/unit/log_level_spec.rb
155
154
  - spec/unit/logger_spec.rb
156
155
  - spec/unit/record_spec.rb
156
+ - spec/unit/sink/io_spec.rb
157
+ - spec/unit/sink/syslog_spec.rb
157
158
  - spec/unit/steno_spec.rb
158
- - spec/unit/syslog_sink_spec.rb
159
159
  - spec/unit/tagged_logger_spec.rb
160
160
  - steno.gemspec
161
161
  homepage: http://www.cloudfoundry.org
@@ -191,12 +191,12 @@ test_files:
191
191
  - spec/unit/context_spec.rb
192
192
  - spec/unit/core_ext_spec.rb
193
193
  - spec/unit/http_handler_spec.rb
194
- - spec/unit/io_sink_spec.rb
195
194
  - spec/unit/json_codec_spec.rb
196
195
  - spec/unit/json_prettifier_spec.rb
197
196
  - spec/unit/log_level_spec.rb
198
197
  - spec/unit/logger_spec.rb
199
198
  - spec/unit/record_spec.rb
199
+ - spec/unit/sink/io_spec.rb
200
+ - spec/unit/sink/syslog_spec.rb
200
201
  - spec/unit/steno_spec.rb
201
- - spec/unit/syslog_sink_spec.rb
202
202
  - spec/unit/tagged_logger_spec.rb