hatchet 0.0.18 → 0.0.19

Sign up to get free protection for your applications and to get access to all the features.
data/RELEASE.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Release notes
2
2
 
3
+ ## 0.0.19
4
+
5
+ * Changed core formatters to output an indented backtrace after a message when
6
+ an error is present, can be disabled via the formatter's `backtrace=`
7
+ attribute
8
+
3
9
  ## 0.0.18
4
10
 
5
11
  * Made the presence of a `formatter=` method on appenders optional
@@ -9,6 +9,17 @@ module Hatchet
9
9
  #
10
10
  class PlainFormatter
11
11
 
12
+ # Public: Gets or sets whether backtraces should be output when messages
13
+ # contain an error with one.
14
+ #
15
+ attr_accessor :backtrace
16
+
17
+ # Public: Creates a new instance.
18
+ #
19
+ def initialize
20
+ self.backtrace = true
21
+ end
22
+
12
23
  # Public: Returns the formatted message.
13
24
  #
14
25
  # level - The severity of the log message.
@@ -18,9 +29,17 @@ module Hatchet
18
29
  # Returns messages in the format:
19
30
  #
20
31
  # MESSAGE
32
+ # BACKTRACE
33
+ #
34
+ # The backtrace is only present if the message contains an error.
21
35
  #
22
36
  def format(level, context, message)
23
- message.to_s
37
+ unless self.backtrace && message.error && message.error.respond_to?(:backtrace)
38
+ message.to_s
39
+ else
40
+ indented_backtrace = message.error.backtrace.map { |line| " #{line}" }.to_a
41
+ ([message.to_s] + indented_backtrace).join "\n"
42
+ end
24
43
  end
25
44
 
26
45
  end
@@ -7,6 +7,17 @@ module Hatchet
7
7
  #
8
8
  class SimpleFormatter
9
9
 
10
+ # Public: Gets or sets whether backtraces should be output when messages
11
+ # contain an error with one.
12
+ #
13
+ attr_accessor :backtrace
14
+
15
+ # Public: Creates a new instance.
16
+ #
17
+ def initialize
18
+ self.backtrace = true
19
+ end
20
+
10
21
  # Public: Returns the formatted message.
11
22
  #
12
23
  # level - The severity of the log message.
@@ -16,10 +27,17 @@ module Hatchet
16
27
  # Returns messages in the format:
17
28
  #
18
29
  # LEVEL - CONTEXT - MESSAGE
30
+ # BACKTRACE
31
+ #
32
+ # The backtrace is only present if the message contains an error.
19
33
  #
20
34
  def format(level, context, message)
21
- message = message.to_s.strip
22
- "#{level.to_s.upcase} - #{context} - #{message}"
35
+ msg = message.to_s.strip
36
+ if self.backtrace && message.error && message.error.respond_to?(:backtrace)
37
+ indented_backtrace = message.error.backtrace.map { |line| " #{line}" }.to_a
38
+ msg = ([msg] + indented_backtrace).join("\n")
39
+ end
40
+ "#{level.to_s.upcase} - #{context} - #{msg}"
23
41
  end
24
42
 
25
43
  end
@@ -6,10 +6,16 @@ module Hatchet
6
6
  #
7
7
  class StandardFormatter
8
8
 
9
+ # Public: Gets or sets whether backtraces should be output when messages
10
+ # contain an error with one.
11
+ #
12
+ attr_accessor :backtrace
13
+
9
14
  # Public: Creates a new instance.
10
15
  #
11
16
  def initialize
12
17
  @secs = 0
18
+ self.backtrace = true
13
19
  end
14
20
 
15
21
  # Public: Returns the formatted message.
@@ -21,10 +27,17 @@ module Hatchet
21
27
  # Returns messages in the format:
22
28
  #
23
29
  # %Y-%m-%d %H:%M:%S.%L [THREAD] LEVEL CONTEXT - MESSAGE
30
+ # BACKTRACE
31
+ #
32
+ # The backtrace is only present if the message contains an error.
24
33
  #
25
34
  def format(level, context, message)
26
- message = message.to_s.strip
27
- "#{timestamp} [#{thread_name}] #{format_level level} #{context} - #{message}"
35
+ msg = message.to_s.strip
36
+ if self.backtrace && message.error && message.error.respond_to?(:backtrace)
37
+ indented_backtrace = message.error.backtrace.map { |line| " #{line}" }.to_a
38
+ msg = ([msg] + indented_backtrace).join("\n")
39
+ end
40
+ "#{timestamp} [#{thread_name}] #{format_level level} #{context} - #{msg}"
28
41
  end
29
42
 
30
43
  private
@@ -4,6 +4,6 @@ module Hatchet
4
4
 
5
5
  # Public: The version of Hatchet.
6
6
  #
7
- VERSION = '0.0.18'
7
+ VERSION = '0.0.19'
8
8
 
9
9
  end
@@ -6,13 +6,54 @@ describe PlainFormatter do
6
6
  let(:subject) { PlainFormatter.new }
7
7
 
8
8
  describe 'when formatting a message' do
9
- before do
10
- @message = subject.format(:info, 'Custom::Context', ' Hello, World ')
9
+
10
+ describe 'without an error' do
11
+
12
+ before do
13
+ @message = Message.new(' Hello, World ')
14
+ end
15
+
16
+ it 'outputs the message in the MESSAGE format' do
17
+ message = subject.format(:info, 'Custom::Context', @message)
18
+ assert ' Hello, World ' == message, "got #{message}"
19
+ end
20
+
11
21
  end
12
22
 
13
- it 'outputs the message in the MESSAGE format' do
14
- assert ' Hello, World ' == @message, "got #{@message}"
23
+ describe 'with an error' do
24
+
25
+ before do
26
+ error = OpenStruct.new(message: 'Boom!', backtrace: ['foo.rb:1:a', 'foo.rb:20:b'])
27
+ @message = Message.new(' Hello, World ', error)
28
+ end
29
+
30
+ describe 'with backtraces enabled' do
31
+
32
+ it 'outputs the message in the MESSAGE format' do
33
+ message = subject.format(:info, 'Custom::Context', @message)
34
+ assert_equal %q{ Hello, World
35
+ foo.rb:1:a
36
+ foo.rb:20:b}, message
37
+ end
38
+
39
+ end
40
+
41
+ describe 'with backtraces disabled' do
42
+
43
+ before do
44
+ subject.backtrace = false
45
+ end
46
+
47
+ it 'outputs the message in the MESSAGE format' do
48
+ message = subject.format(:info, 'Custom::Context', @message)
49
+ assert ' Hello, World ' == message, "got #{message}"
50
+ end
51
+
52
+ end
53
+
15
54
  end
55
+
16
56
  end
57
+
17
58
  end
18
59
 
@@ -6,12 +6,51 @@ describe SimpleFormatter do
6
6
  let(:subject) { SimpleFormatter.new }
7
7
 
8
8
  describe 'when formatting a message' do
9
- before do
10
- @message = subject.format(:info, 'Custom::Context', 'Hello, World')
9
+
10
+ describe 'without an error' do
11
+
12
+ before do
13
+ @message = Message.new(' Hello, World ')
14
+ end
15
+
16
+ it 'outputs the message in the LEVEL - CONTEXT - MESSAGE format' do
17
+ message = subject.format(:info, 'Custom::Context', @message)
18
+ assert 'INFO - Custom::Context - Hello, World' == message, "got #{message}"
19
+ end
20
+
11
21
  end
12
22
 
13
- it 'outputs the message in the LEVEL - CONTEXT - MESSAGE format' do
14
- assert 'INFO - Custom::Context - Hello, World' == @message, "got #{@message}"
23
+ describe 'with an error' do
24
+
25
+ before do
26
+ error = OpenStruct.new(message: 'Boom!', backtrace: ['foo.rb:1:a', 'foo.rb:20:b'])
27
+ @message = Message.new(' Hello, World ', error)
28
+ end
29
+
30
+ describe 'with backtraces enabled' do
31
+
32
+ it 'outputs the message in the LEVEL - CONTEXT - MESSAGE format' do
33
+ message = subject.format(:info, 'Custom::Context', @message)
34
+ assert_equal %q{INFO - Custom::Context - Hello, World
35
+ foo.rb:1:a
36
+ foo.rb:20:b}, message
37
+ end
38
+
39
+ end
40
+
41
+ describe 'with backtraces disabled' do
42
+
43
+ before do
44
+ subject.backtrace = false
45
+ end
46
+
47
+ it 'outputs the message in the LEVEL - CONTEXT - MESSAGE format' do
48
+ message = subject.format(:info, 'Custom::Context', @message)
49
+ assert 'INFO - Custom::Context - Hello, World' == message, "got #{message}"
50
+ end
51
+
52
+ end
53
+
15
54
  end
16
55
  end
17
56
  end
@@ -10,7 +10,7 @@ describe StandardFormatter do
10
10
 
11
11
  describe 'when formatting a message' do
12
12
  before do
13
- @message = 'Hello, World'
13
+ @message = Message.new('Hello, World')
14
14
  @context = 'Custom::Context'
15
15
  @level = :info
16
16
  @formatted_message = subject.format(@level, @context, @message)
@@ -54,6 +54,39 @@ describe StandardFormatter do
54
54
  end
55
55
  end
56
56
 
57
+ describe 'with an error' do
58
+
59
+ before do
60
+ error = OpenStruct.new(message: 'Boom!', backtrace: ['foo.rb:1:a', 'foo.rb:20:b'])
61
+ @message = Message.new(' Hello, World ', error)
62
+ end
63
+
64
+ describe 'with backtraces enabled' do
65
+
66
+ it 'outputs the message in the LEVEL - CONTEXT - MESSAGE format' do
67
+ formatted_message = subject.format(@level, @context, @message)
68
+ backtrace = formatted_message.split("\n").drop(1)
69
+ assert_equal [' foo.rb:1:a', ' foo.rb:20:b'], backtrace
70
+ end
71
+
72
+ end
73
+
74
+ describe 'with backtraces disabled' do
75
+
76
+ before do
77
+ subject.backtrace = false
78
+ end
79
+
80
+ it 'outputs the message in the LEVEL - CONTEXT - MESSAGE format' do
81
+ formatted_message = subject.format(@level, @context, @message)
82
+ backtrace = formatted_message.split("\n").drop(1)
83
+ assert_empty backtrace
84
+ end
85
+
86
+ end
87
+
88
+ end
89
+
57
90
  if ENV["BENCH"] then
58
91
 
59
92
  describe 'benchmarks' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hatchet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.18
4
+ version: 0.0.19
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-11-04 00:00:00.000000000 Z
12
+ date: 2012-11-07 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Logging library that provides the ability to add class/module specific
15
15
  filters