hatchet 0.0.16 → 0.0.17

Sign up to get free protection for your applications and to get access to all the features.
@@ -67,16 +67,34 @@ module Hatchet
67
67
  # Public: Logs a message at the given level.
68
68
  #
69
69
  # message - An already evaluated message, usually a String (default: nil).
70
+ # error - An error which is associated with the message (default: nil).
70
71
  # block - An optional block which will provide a message when invoked.
71
72
  #
72
73
  # One of message or block must be provided. If both are provided then the
73
74
  # block is preferred as it is assumed to provide more detail.
74
75
  #
76
+ # In general, you should use the block style for any message not related
77
+ # to an error. This is because any unneccessary String interpolation is
78
+ # avoided making unwritten debug calls, for example, less expensive.
79
+ #
80
+ # When logging errors it is advised that you include some details of the
81
+ # error within the regular message, perhaps the error's message, but leave
82
+ # the inclusion of the stack trace up to your appenders and their
83
+ # formatters.
84
+ #
85
+ # Examples
86
+ #
87
+ # debug { "A fine grained message" }
88
+ # info { "An interesting message" }
89
+ # warn { "A message worth highlighting" }
90
+ # error "A message relating to an exception", e
91
+ # fatal "A message causing application failure", e
92
+ #
75
93
  # Returns nothing.
76
94
  #
77
- define_method level do |message = nil, &block|
95
+ define_method level do |message = nil, error = nil, &block|
78
96
  return unless message or block
79
- add level, Message.new(message, &block)
97
+ add level, Message.new(message, error, &block)
80
98
  end
81
99
 
82
100
  # Public: Returns true if any of the appenders will log messages for the
@@ -150,11 +168,12 @@ module Hatchet
150
168
  begin
151
169
  appender.add(level, @context, message)
152
170
  rescue => e
153
- puts "Failed to log message for #{context} with appender #{appender} - #{level} - #{message}\n"
171
+ puts "Failed to log message for #{@context} with appender #{appender} - #{level} - #{message}\n"
154
172
  puts "#{e}\n"
155
173
  end
156
174
  end
157
175
  end
176
+ nil
158
177
  end
159
178
 
160
179
  # Private: Determines the contextual name of the host object.
@@ -2,16 +2,24 @@
2
2
 
3
3
  module Hatchet
4
4
 
5
- # Internal: Class for wrapping message strings and blocks in a way that means
5
+ # Public: Class for wrapping message strings and blocks in a way that means
6
6
  # they can be treated identically.
7
7
  #
8
+ # If an error is associated with the message this will be available via the
9
+ # #error attribute.
10
+ #
8
11
  # Blocks will be lazily evaluated once for all appenders when required.
9
12
  #
10
13
  class Message
11
14
 
15
+ # Public: Gets the error associated with this message, if given.
16
+ #
17
+ attr_reader :error
18
+
12
19
  # Internal: Creates a new message.
13
20
  #
14
21
  # message - An already evaluated message, usually a String (default: nil).
22
+ # error - An error that is associated with the message (default: nil).
15
23
  # block - An optional block which will provide a message when invoked.
16
24
  #
17
25
  # One of message or block must be provided. If both are provided then the
@@ -22,8 +30,9 @@ module Hatchet
22
30
  # Message.new "Evaluated message"
23
31
  # Message.new { "Lazily evaluated message" }
24
32
  #
25
- def initialize(message = nil, &block)
33
+ def initialize(message = nil, error = nil, &block)
26
34
  @block = block
35
+ @error = error
27
36
  @message = message unless @block
28
37
  end
29
38
 
@@ -4,6 +4,6 @@ module Hatchet
4
4
 
5
5
  # Public: The version of Hatchet.
6
6
  #
7
- VERSION = '0.0.16'
7
+ VERSION = '0.0.17'
8
8
 
9
9
  end
@@ -0,0 +1,14 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ class FailingAppender
4
+ include LevelManager
5
+
6
+ def initialize
7
+ @levels = { nil => :info }
8
+ end
9
+
10
+ def add(level, context, message)
11
+ raise StandardError, 'Failing appender'
12
+ end
13
+ end
14
+
data/spec/logger_spec.rb CHANGED
@@ -40,6 +40,23 @@ describe HatchetLogger do
40
40
 
41
41
  refute disabled_appender.add_called
42
42
  end
43
+
44
+ it 'should return nil from the call' do
45
+ returned = subject.send(level, message)
46
+
47
+ assert returned.nil?, 'logging calls should return nil'
48
+ end
49
+
50
+ describe 'with an error' do
51
+ let(:error) { StandardError.new }
52
+
53
+ it 'should pass the error through to the appender' do
54
+ subject.send level, message, error
55
+ received = appender.messages.last
56
+
57
+ assert error == received.message.error
58
+ end
59
+ end
43
60
  end
44
61
  end
45
62
 
@@ -81,6 +98,16 @@ describe HatchetLogger do
81
98
  end
82
99
  end
83
100
 
101
+ describe 'failing appender' do
102
+ before do
103
+ configuration.appenders << FailingAppender.new
104
+ end
105
+
106
+ it 'does not fail' do
107
+ subject.info 'Will fail for one appender'
108
+ end
109
+ end
110
+
84
111
  describe 'naming context' do
85
112
  let(:context_name) do
86
113
  subject.fatal 'Message'
data/spec/spec_helper.rb CHANGED
@@ -10,6 +10,7 @@ include Hatchet
10
10
  require_relative 'helpers/logger_double'
11
11
  require_relative 'helpers/test_formatter'
12
12
  require_relative 'helpers/disabled_appender'
13
+ require_relative 'helpers/failing_appender'
13
14
  require_relative 'helpers/storing_appender'
14
15
 
15
16
  INITIAL_EXECUTION_CONTEXT = self
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.16
4
+ version: 0.0.17
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-08-22 00:00:00.000000000 Z
12
+ date: 2012-11-04 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
@@ -33,6 +33,7 @@ files:
33
33
  - lib/hatchet.rb
34
34
  - spec/configuration_spec.rb
35
35
  - spec/helpers/disabled_appender.rb
36
+ - spec/helpers/failing_appender.rb
36
37
  - spec/helpers/logger_double.rb
37
38
  - spec/helpers/storing_appender.rb
38
39
  - spec/helpers/test_formatter.rb
@@ -60,7 +61,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
60
61
  version: '0'
61
62
  segments:
62
63
  - 0
63
- hash: 569280193996851270
64
+ hash: -2041677748427944104
64
65
  required_rubygems_version: !ruby/object:Gem::Requirement
65
66
  none: false
66
67
  requirements:
@@ -69,7 +70,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
69
70
  version: '0'
70
71
  segments:
71
72
  - 0
72
- hash: 569280193996851270
73
+ hash: -2041677748427944104
73
74
  requirements: []
74
75
  rubyforge_project:
75
76
  rubygems_version: 1.8.17
@@ -79,6 +80,7 @@ summary: Logging library that provides the ability to add class/module specific
79
80
  test_files:
80
81
  - spec/configuration_spec.rb
81
82
  - spec/helpers/disabled_appender.rb
83
+ - spec/helpers/failing_appender.rb
82
84
  - spec/helpers/logger_double.rb
83
85
  - spec/helpers/storing_appender.rb
84
86
  - spec/helpers/test_formatter.rb