hatchet 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -19,11 +19,75 @@ module Hatchet
19
19
  reset!
20
20
  end
21
21
 
22
+ # Public: Returns the default formatter given to the appenders that have not
23
+ # had their formatter explicitly set.
24
+ #
25
+ # If not otherwise set, will be a StandardFormatter.
26
+ #
27
+ def formatter
28
+ @formatter.formatter
29
+ end
30
+
31
+ # Public: Sets the default formatter given to the appenders that have not
32
+ # had their formatter explicitly set.
33
+ #
34
+ def formatter=(formatter)
35
+ @formatter.formatter = formatter
36
+ end
37
+
22
38
  # Public: Resets the configuration's internal state to the defaults.
23
39
  #
24
40
  def reset!
25
41
  @levels = { nil => :info }
26
42
  @appenders = []
43
+
44
+ # If a DelegatingFormatter has already been set up replace its
45
+ # formatter, otherwise create a new one.
46
+ #
47
+ if @formatter
48
+ @formatter.formatter = StandardFormatter.new
49
+ else
50
+ @formatter = DelegatingFormatter.new(StandardFormatter.new)
51
+ end
52
+ end
53
+
54
+ # Public: Yields the configuration object to the given block to make it
55
+ # tidier when setting multiple values against a referenced configuration.
56
+ #
57
+ # block - Mandatory block which receives a Configuration object that can be
58
+ # used to setup Hatchet.
59
+ #
60
+ # Once the block returns each of the configured appenders has its formatter
61
+ # set to the default formatter if one is not already set, and its levels
62
+ # Hash is set to the shared levels Hash if an explicit one has not been
63
+ # provided.
64
+ #
65
+ # Example
66
+ #
67
+ # configuration.configure do |config|
68
+ # # Set the level to use unless overridden (defaults to :info)
69
+ # config.level :info
70
+ # # Set the level for a specific class/module and its children
71
+ # config.level :debug, 'Namespace::Something::Nested'
72
+ #
73
+ # # Add as many appenders as you like
74
+ # config.appenders << Hatchet::LoggerAppender.new do |appender|
75
+ # # Set the logger that this is wrapping (required)
76
+ # appender.logger = Logger.new('log/test.log')
77
+ # end
78
+ # end
79
+ #
80
+ # Returns nothing.
81
+ #
82
+ def configure
83
+ yield self
84
+
85
+ # Ensure every appender has a formatter and a level configuration.
86
+ #
87
+ appenders.each do |appender|
88
+ appender.formatter ||= @formatter
89
+ appender.levels = @levels if appender.levels.empty?
90
+ end
27
91
  end
28
92
 
29
93
  end
@@ -0,0 +1,38 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ module Hatchet
4
+
5
+ # Internal: Formatter class that delegates to another formatter
6
+ # implementation. Used within the configuration to make it possible to switch
7
+ # the default formatter at any time.
8
+ #
9
+ class DelegatingFormatter
10
+
11
+ # Internal: Gets or sets the formatter that is delegated to.
12
+ #
13
+ attr_accessor :formatter
14
+
15
+ # Internal: Creates a new instance.
16
+ #
17
+ # formatter - The formatter to delegate to initially.
18
+ #
19
+ def initialize(formatter)
20
+ @formatter = formatter
21
+ end
22
+
23
+ # Public: Returns the formatted message.
24
+ #
25
+ # level - The severity of the log message.
26
+ # context - The context of the log message.
27
+ # message - The message provided by the log caller.
28
+ #
29
+ # Returns messages as formatted by the formatter being delegated to.
30
+ #
31
+ def format(level, context, message)
32
+ @formatter.format(level, context, message)
33
+ end
34
+
35
+ end
36
+
37
+ end
38
+
@@ -13,9 +13,7 @@ module Hatchet
13
13
  # Expose Hatchet's configuration object to consumers through the Rails
14
14
  # config object.
15
15
  #
16
- Hatchet.configure do |config|
17
- self.config.hatchet = config
18
- end
16
+ self.config.hatchet = Hatchet.configuration
19
17
 
20
18
  # Wrap the default Rails.logger, Rails.application.assets.logger, and all
21
19
  # log subscribers found in ActiveSupport::LogSubscriber.log_subscribers
@@ -27,10 +25,23 @@ module Hatchet
27
25
  #
28
26
  logger = Rails.logger
29
27
 
28
+ # Map the level of the logger so Hatchet uses the same.
29
+ #
30
+ current_level =
31
+ case logger.level
32
+ when Logger::DEBUG then :debug
33
+ when Logger::INFO then :info
34
+ when Logger::WARN then :warn
35
+ when Logger::ERROR then :error
36
+ when Logger::FATAL then :fatal
37
+ else nil # If not recognized use Hatchet's default
38
+ end
39
+
30
40
  # Add an appender that delegates to the current Rails.logger to Hatchet's
31
41
  # configuration.
32
42
  #
33
43
  Hatchet.configure do |config|
44
+ config.level(current_level) if current_level
34
45
  config.appenders << Hatchet::LoggerAppender.new(logger: logger)
35
46
  end
36
47
 
@@ -41,14 +52,15 @@ module Hatchet
41
52
  begin
42
53
  # Replace the Rails.logger with the application's Hatchet logger.
43
54
  #
44
- logger.debug 'Replacing Rails logger with Hatchet'
45
55
  Rails.logger = app.logger
56
+ app.logger.debug { 'Replaced Rails logger with Hatchet' }
46
57
 
47
58
  # Replace the logger of every subscriber in the
48
59
  # ActiveSupport::LogSubscriber.log_subscribers collection by extending
60
+ # them with Hatchet.
49
61
  #
50
62
  ActiveSupport::LogSubscriber.log_subscribers.each do |subscriber|
51
- logger.debug "Replacing #{subscriber.class} logger with Hatchet"
63
+ app.logger.debug { "Replacing #{subscriber.class} logger with Hatchet" }
52
64
  subscriber.extend Hatchet
53
65
  end
54
66
 
@@ -59,7 +71,7 @@ module Hatchet
59
71
  # As you can guess by the description this is probably the riskiest so
60
72
  # we do it last.
61
73
  #
62
- logger.debug 'Replacing Rails asset logger with Hatchet'
74
+ app.logger.debug { 'Replacing Rails asset logger with Hatchet' }
63
75
 
64
76
  # Initially replace it with the application logger as it's better for
65
77
  # this to be done if the next part fails.
@@ -78,8 +90,8 @@ module Hatchet
78
90
  # If anything goes wrong along the way log it and let the application
79
91
  # continue.
80
92
  #
81
- logger.error 'Failed to replace logger with Hatchet'
82
- logger.error $!
93
+ logger.error { 'Failed to replace logger with Hatchet' }
94
+ logger.error { $! }
83
95
  end
84
96
  end
85
97
  end
@@ -6,6 +6,8 @@ module Hatchet
6
6
  #
7
7
  class StandardFormatter
8
8
 
9
+ # Public: Creates a new instance.
10
+ #
9
11
  def initialize
10
12
  @secs = 0
11
13
  end
@@ -4,6 +4,6 @@ module Hatchet
4
4
 
5
5
  # Public: The version of Hatchet.
6
6
  #
7
- VERSION = '0.0.10'
7
+ VERSION = '0.0.11'
8
8
 
9
9
  end
data/lib/hatchet.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative 'hatchet/level_manager'
4
4
  require_relative 'hatchet/configuration'
5
+ require_relative 'hatchet/delegating_formatter'
5
6
  require_relative 'hatchet/hatchet_logger'
6
7
  require_relative 'hatchet/logger_appender'
7
8
  require_relative 'hatchet/message'
@@ -117,12 +118,10 @@ module Hatchet
117
118
  # Hatchet.configure do |config|
118
119
  # # Set the level to use unless overridden (defaults to :info)
119
120
  # config.level :info
120
- # # Set the level for a specific class/module and its children (can be a
121
- # # string)
122
- # config.level :debug, Namespace::Something::Nested
121
+ # # Set the level for a specific class/module and its children
122
+ # config.level :debug, 'Namespace::Something::Nested'
123
123
  #
124
- # # Add as many appenders as you like, Hatchet comes with one that formats
125
- # # the standard logger in the TTCC style of log4j.
124
+ # # Add as many appenders as you like
126
125
  # config.appenders << Hatchet::LoggerAppender.new do |appender|
127
126
  # # Set the logger that this is wrapping (required)
128
127
  # appender.logger = Logger.new('log/test.log')
@@ -130,13 +129,9 @@ module Hatchet
130
129
  # end
131
130
  #
132
131
  # Returns nothing.
133
- def self.configure
134
- yield configuration
135
- default_formatter = StandardFormatter.new
136
- configuration.appenders.each do |appender|
137
- appender.formatter ||= default_formatter
138
- appender.levels = configuration.levels if appender.levels.empty?
139
- end
132
+ #
133
+ def self.configure(&block)
134
+ configuration.configure(&block)
140
135
  end
141
136
 
142
137
  # Public: Callback method for when Hatchet is registered as a Sinatra helper.
@@ -146,6 +141,7 @@ module Hatchet
146
141
  # register Hatchet
147
142
  #
148
143
  # Returns nothing.
144
+ #
149
145
  def self.registered(app)
150
146
  app.helpers Hatchet
151
147
  end
@@ -156,9 +152,7 @@ module Hatchet
156
152
  configuration.appenders
157
153
  end
158
154
 
159
- private
160
-
161
- # Private: Returns the configuration object, initializing it when necessary.
155
+ # Internal: Returns the configuration object, initializing it when necessary.
162
156
  #
163
157
  def self.configuration
164
158
  @config ||= Configuration.new
@@ -25,7 +25,30 @@ describe 'configuration' do
25
25
  end
26
26
 
27
27
  it 'formatter set as a StandardFormatter' do
28
- assert appender.formatter.instance_of? StandardFormatter
28
+ assert appender.formatter.instance_of? DelegatingFormatter
29
+ assert appender.formatter.formatter.instance_of? StandardFormatter
30
+ end
31
+
32
+ describe 'with an explicit default formatter' do
33
+ let(:formatter) { SimpleFormatter.new }
34
+ let(:second_appender) { StoringAppender.new }
35
+
36
+ before do
37
+ Hatchet.configure do |config|
38
+ config.formatter = formatter
39
+ config.appenders << second_appender
40
+ end
41
+ end
42
+
43
+ it 'original formatter set as the configured default' do
44
+ assert appender.formatter.instance_of? DelegatingFormatter
45
+ assert appender.formatter.formatter == formatter
46
+ end
47
+
48
+ it 'second formatter set as the configured default' do
49
+ assert appender.formatter.instance_of? DelegatingFormatter
50
+ assert second_appender.formatter.formatter == formatter
51
+ end
29
52
  end
30
53
  end
31
54
 
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.10
4
+ version: 0.0.11
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-23 00:00:00.000000000 Z
12
+ date: 2012-07-29 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
@@ -20,6 +20,7 @@ extensions: []
20
20
  extra_rdoc_files: []
21
21
  files:
22
22
  - lib/hatchet/configuration.rb
23
+ - lib/hatchet/delegating_formatter.rb
23
24
  - lib/hatchet/hatchet_logger.rb
24
25
  - lib/hatchet/level_manager.rb
25
26
  - lib/hatchet/logger_appender.rb
@@ -62,7 +63,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
62
63
  version: '0'
63
64
  requirements: []
64
65
  rubyforge_project:
65
- rubygems_version: 1.8.17
66
+ rubygems_version: 1.8.24
66
67
  signing_key:
67
68
  specification_version: 3
68
69
  summary: Logging library that provides the ability to add class/module specific filters