hatchet 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -130,12 +130,11 @@ module Hatchet
130
130
  #
131
131
  # Returns nothing.
132
132
  def self.configure
133
- @@config = Configuration.new
134
- yield @@config
133
+ yield configuration
135
134
  default_formatter = StandardFormatter.new
136
- @@config.appenders.each do |appender|
135
+ configuration.appenders.each do |appender|
137
136
  appender.formatter ||= default_formatter
138
- appender.levels = @@config.levels if appender.levels.empty?
137
+ appender.levels = configuration.levels if appender.levels.empty?
139
138
  end
140
139
  end
141
140
 
@@ -153,12 +152,20 @@ module Hatchet
153
152
  # Internal: Returns the Array of configured appenders.
154
153
  #
155
154
  def self.appenders
156
- if @@config and @@config.appenders
157
- @@config.appenders
158
- else
159
- []
160
- end
155
+ configuration.appenders
156
+ end
157
+
158
+ private
159
+
160
+ # Private: Returns the configuration object, initializing it when necessary.
161
+ #
162
+ def self.configuration
163
+ @config ||= Configuration.new
161
164
  end
162
165
 
163
166
  end
164
167
 
168
+ # If we are running in a Rails environment include the Hatchet::Railtie class.
169
+ #
170
+ require_relative 'hatchet/railtie' if defined?(Rails)
171
+
@@ -16,6 +16,12 @@ module Hatchet
16
16
  # Creates the levels Hash with a default logging level of info.
17
17
  #
18
18
  def initialize
19
+ reset!
20
+ end
21
+
22
+ # Public: Resets the configuration's internal state to the defaults.
23
+ #
24
+ def reset!
19
25
  @levels = { nil => :info }
20
26
  @appenders = []
21
27
  end
@@ -0,0 +1,87 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ module Hatchet
4
+
5
+ # Public: Railtie to hook Hatchet into Rails.
6
+ #
7
+ # Wraps the default Rails.logger, Rails.application.assets.logger, and all log
8
+ # subscribers found in ActiveSupport::LogSubscriber.log_subscribers
9
+ # collection.
10
+ #
11
+ class Railtie < Rails::Railtie
12
+
13
+ # Expose Hatchet's configuration object to consumers through the Rails
14
+ # config object.
15
+ #
16
+ Hatchet.configure do |config|
17
+ self.config.hatchet = config
18
+ end
19
+
20
+ # Wrap the default Rails.logger, Rails.application.assets.logger, and all
21
+ # log subscribers found in ActiveSupport::LogSubscriber.log_subscribers
22
+ # collection on initialization.
23
+ #
24
+ initializer 'hatchet_railtie.replace_logger' do |app|
25
+
26
+ # Keep a handle to the original logger.
27
+ #
28
+ logger = Rails.logger
29
+
30
+ # Add an appender that delegates to the current Rails.logger to Hatchet's
31
+ # configuration.
32
+ #
33
+ Hatchet.configure do |config|
34
+ config.appenders << Hatchet::LoggerAppender.new(logger: logger)
35
+ end
36
+
37
+ # Extend the application with Hatchet.
38
+ #
39
+ app.extend Hatchet
40
+
41
+ begin
42
+ # Replace the Rails.logger with the application's Hatchet logger.
43
+ #
44
+ logger.debug 'Replacing Rails logger with Hatchet'
45
+ Rails.logger = app.logger
46
+
47
+ # Replace the logger of every subscriber in the
48
+ # ActiveSupport::LogSubscriber.log_subscribers collection by extending
49
+ #
50
+ ActiveSupport::LogSubscriber.log_subscribers.each do |subscriber|
51
+ logger.debug "Replacing #{subscriber.class} logger with Hatchet"
52
+ subscriber.extend Hatchet
53
+ end
54
+
55
+ # Replace the Rails.application.assets.logger with a logger that lives
56
+ # in a module beneath the application. This allows you to target the
57
+ # asset logger messages directly when managing levels.
58
+ #
59
+ # As you can guess by the description this is probably the riskiest so
60
+ # we do it last.
61
+ #
62
+ logger.debug 'Replacing Rails asset logger with Hatchet'
63
+
64
+ # Initially replace it with the application logger as it's better for
65
+ # this to be done if the next part fails.
66
+ #
67
+ Rails.application.assets.logger = app.logger
68
+
69
+ # Create the <Application>::Assets module and extend it with Hatchet so
70
+ # that it can replace the assets logger.
71
+ #
72
+ assets = Module.new
73
+ app.class.const_set 'Assets', assets
74
+ assets.extend Hatchet
75
+ Rails.application.assets.logger = assets.logger
76
+
77
+ rescue
78
+ # If anything goes wrong along the way log it and let the application
79
+ # continue.
80
+ #
81
+ logger.error 'Failed to replace logger with Hatchet'
82
+ logger.error $!
83
+ end
84
+ end
85
+ end
86
+
87
+ end
@@ -41,7 +41,7 @@ module Hatchet
41
41
  end
42
42
 
43
43
  @millis = millis
44
- @last = @date + "00#{millis}"[1..3]
44
+ @last = @date + "00#{millis}"[-3..-1]
45
45
  end
46
46
 
47
47
  # Private: Returns the name of the current thread from the processes pid and
@@ -4,6 +4,6 @@ module Hatchet
4
4
 
5
5
  # Public: The version of Hatchet.
6
6
  #
7
- VERSION = '0.0.6'
7
+ VERSION = '0.0.7'
8
8
 
9
9
  end
@@ -3,6 +3,12 @@
3
3
  require_relative 'spec_helper'
4
4
 
5
5
  describe 'configuration' do
6
+ before do
7
+ Hatchet.configure do |config|
8
+ config.reset!
9
+ end
10
+ end
11
+
6
12
  describe 'appender defaults' do
7
13
  let(:set_levels) { { unique: :fake_level } }
8
14
  let(:appender) { StoringAppender.new }
@@ -51,7 +57,7 @@ describe 'configuration' do
51
57
  end
52
58
 
53
59
  describe 'global default level' do
54
- let(:appender) { StoringAppender.new }
60
+ let(:appender) { StoringAppender.new }
55
61
 
56
62
  before do
57
63
  Hatchet.configure do |config|
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.6
4
+ version: 0.0.7
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-10 00:00:00.000000000 Z
12
+ date: 2012-07-15 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
@@ -24,6 +24,7 @@ files:
24
24
  - lib/hatchet/level_manager.rb
25
25
  - lib/hatchet/logger_appender.rb
26
26
  - lib/hatchet/message.rb
27
+ - lib/hatchet/railtie.rb
27
28
  - lib/hatchet/standard_formatter.rb
28
29
  - lib/hatchet/version.rb
29
30
  - lib/hatchet.rb