semantic_logger 0.11.1 → 0.11.2

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.
@@ -2,6 +2,7 @@
2
2
  require 'thread'
3
3
  require 'thread_safe'
4
4
  require 'sync_attr'
5
+ require 'semantic_logger/version'
5
6
 
6
7
  module SemanticLogger
7
8
  autoload :Base, 'semantic_logger/base'
@@ -31,16 +31,10 @@
31
31
  #
32
32
  module SemanticLogger
33
33
  class Logger < Base
34
- include SyncAttr
35
-
36
- # Add or remove logging appenders to the appenders Array
34
+ # Add or remove logging appenders to the thread-safe appenders Array
37
35
  # Appenders will be written to in the order that they appear in this list
38
- sync_cattr_reader :appenders do
39
- # The logging thread is only started when an appender is added
40
- @@appender_thread = Thread.new { appender_thread }
41
-
42
- # Thread safe appenders array
43
- ThreadSafe::Array.new
36
+ def self.appenders
37
+ @@appenders
44
38
  end
45
39
 
46
40
  # Returns a Logger instance
@@ -84,9 +78,9 @@ module SemanticLogger
84
78
  # Flush all queued log entries disk, database, etc.
85
79
  # All queued log messages are written and then each appender is flushed in turn
86
80
  def self.flush
87
- return false unless started? && @@appender_thread && @@appender_thread.alive?
81
+ return false unless appender_thread_active?
88
82
 
89
- logger.debug "SemanticLogger::Logger Flushing appenders with #{queue_size} log messages on the queue"
83
+ logger.debug "Flushing appenders with #{queue_size} log messages on the queue"
90
84
  reply_queue = Queue.new
91
85
  queue << { :command => :flush, :reply_queue => reply_queue }
92
86
  reply_queue.pop
@@ -117,16 +111,21 @@ module SemanticLogger
117
111
  @@lag_threshold_s = time_threshold_s
118
112
  end
119
113
 
120
- # Returns whether the logging thread has been started
121
- def self.started?
122
- defined? :@@appenders
114
+ # Allow the internal logger to be overridden from its default to STDERR
115
+ # Can be replaced with another Ruby logger or Rails logger, but never to
116
+ # SemanticLogger::Logger itself since it is for reporting problems
117
+ # while trying to log to the various appenders
118
+ def self.logger=(logger)
119
+ @@logger = logger
123
120
  end
124
121
 
122
+
125
123
  ############################################################################
126
124
  protected
127
125
 
126
+ @@appenders = ThreadSafe::Array.new
128
127
  @@appender_thread = nil
129
- @@queue = Queue.new
128
+ @@queue = Queue.new
130
129
 
131
130
  # Queue to hold messages that need to be logged to the various appenders
132
131
  def self.queue
@@ -136,19 +135,31 @@ module SemanticLogger
136
135
  # Place log request on the queue for the Appender thread to write to each
137
136
  # appender in the order that they were registered
138
137
  def log(log)
139
- self.class.queue << log if self.class.started?
138
+ self.class.queue << log if @@appender_thread
140
139
  end
141
140
 
142
141
  # Internal logger for SemanticLogger
143
142
  # For example when an appender is not working etc..
144
143
  # By default logs to STDERR
145
- # Can be replaced with another Ruby logger or Rails logger, but never to
146
- # SemanticLogger::Logger itself
147
- #
148
- # Warning: Do not use this logger directly it is intended for internal logging
149
- # within Semantic Logger itself
150
- sync_cattr_accessor :logger do
151
- SemanticLogger::Appender::File.new(STDERR, :warn)
144
+ def self.logger
145
+ @@logger ||= begin
146
+ l = SemanticLogger::Appender::File.new(STDERR, :warn)
147
+ l.name = self.class.name
148
+ l
149
+ end
150
+ end
151
+
152
+ # Start the appender thread
153
+ def self.start_appender_thread
154
+ return false if appender_thread_active?
155
+ @@appender_thread = Thread.new { appender_thread }
156
+ raise "Failed to start Appender Thread" unless @@appender_thread
157
+ true
158
+ end
159
+
160
+ # Returns true if the appender_thread is active
161
+ def self.appender_thread_active?
162
+ @@appender_thread && @@appender_thread.alive?
152
163
  end
153
164
 
154
165
  # Separate appender thread responsible for reading log messages and
@@ -159,7 +170,7 @@ module SemanticLogger
159
170
  #
160
171
  # Should any appender fail to log or flush, the exception is logged and
161
172
  # other appenders will still be called
162
- logger.info "SemanticLogger::Logger V#{VERSION} Appender thread active"
173
+ logger.debug "V#{VERSION} Appender thread active"
163
174
  begin
164
175
  count = 0
165
176
  while message = queue.pop
@@ -168,14 +179,14 @@ module SemanticLogger
168
179
  begin
169
180
  appender.log(message)
170
181
  rescue Exception => exc
171
- logger.error "SemanticLogger::Logger Appender thread: Failed to log to appender: #{appender.inspect}", exc
182
+ logger.error "Appender thread: Failed to log to appender: #{appender.inspect}", exc
172
183
  end
173
184
  end
174
185
  count += 1
175
186
  # Check every few log messages whether this appender thread is falling behind
176
187
  if count > lag_check_interval
177
188
  if (diff = Time.now - message.time) > lag_threshold_s
178
- logger.warn "SemanticLogger::Logger Appender thread has fallen behind by #{diff} seconds with #{queue_size} messages queued up. Consider reducing the log level or changing the appenders"
189
+ logger.warn "Appender thread has fallen behind by #{diff} seconds with #{queue_size} messages queued up. Consider reducing the log level or changing the appenders"
179
190
  end
180
191
  count = 0
181
192
  end
@@ -184,32 +195,37 @@ module SemanticLogger
184
195
  when :flush
185
196
  appenders.each do |appender|
186
197
  begin
187
- logger.info "SemanticLogger::Logger Appender thread: Flushing appender: #{appender.name}"
198
+ logger.info "Appender thread: Flushing appender: #{appender.name}"
188
199
  appender.flush
189
200
  rescue Exception => exc
190
- logger.error "SemanticLogger::Logger Appender thread: Failed to flush appender: #{appender.inspect}", exc
201
+ logger.error "Appender thread: Failed to flush appender: #{appender.inspect}", exc
191
202
  end
192
203
  end
193
204
 
194
205
  message[:reply_queue] << true if message[:reply_queue]
195
- logger.info "SemanticLogger::Logger Appender thread: All appenders flushed"
206
+ logger.info "Appender thread: All appenders flushed"
196
207
  else
197
- logger.warn "SemanticLogger::Logger Appender thread: Ignoring unknown command: #{message[:command]}"
208
+ logger.warn "Appender thread: Ignoring unknown command: #{message[:command]}"
198
209
  end
199
210
  end
200
211
  end
201
212
  rescue Exception => exception
202
- logger.error "SemanticLogger::Logger Appender thread restarting due to exception", exception
213
+ logger.error "Appender thread restarting due to exception", exception
203
214
  retry
204
215
  ensure
205
- logger.debug "SemanticLogger::Logger Appender thread has stopped"
216
+ logger.debug "Appender thread has stopped"
217
+ @@appender_thread = nil
206
218
  end
207
219
  end
208
220
 
209
- end
210
- end
221
+ # Flush all appenders at exit, waiting for outstanding messages on the queue
222
+ # to be written first
223
+ at_exit do
224
+ flush
225
+ end
211
226
 
212
- at_exit do
213
- SemanticLogger::Logger.flush
227
+ # Start appender thread on load to workaround intermittent startup issues
228
+ # with JRuby 1.8.6 under Trinidad in 1.9 mode
229
+ start_appender_thread
230
+ end
214
231
  end
215
-
@@ -1,3 +1,4 @@
1
+ require 'logger'
1
2
  module SemanticLogger #:nodoc:
2
3
  class Railtie < Rails::Railtie #:nodoc:
3
4
  # Make the SemanticLogger config available in the Rails application config
@@ -40,11 +41,12 @@ module SemanticLogger #:nodoc:
40
41
 
41
42
  # Set internal logger to log to file only, in case another appender
42
43
  # experiences logging problems
43
- SemanticLogger::Logger.logger = SemanticLogger::Appender::File.new(path)
44
+ appender = SemanticLogger::Appender::File.new(path)
45
+ appender.name = "SemanticLogger::Logger"
46
+ SemanticLogger::Logger.logger = appender
44
47
 
45
48
  # Add the log file to the list of appenders
46
49
  SemanticLogger::Logger.appenders << SemanticLogger::Appender::File.new(path)
47
-
48
50
  SemanticLogger::Logger.new(Rails)
49
51
  rescue StandardError
50
52
  # If not able to log to file, log to standard error with warning level only
@@ -1,3 +1,3 @@
1
1
  module SemanticLogger #:nodoc
2
- VERSION = "0.11.1"
2
+ VERSION = "0.11.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: semantic_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.1
4
+ version: 0.11.2
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-10-30 00:00:00.000000000 Z
12
+ date: 2012-10-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sync_attr
@@ -108,7 +108,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
108
108
  version: '0'
109
109
  segments:
110
110
  - 0
111
- hash: -3498403633310940698
111
+ hash: 225054469971419847
112
112
  required_rubygems_version: !ruby/object:Gem::Requirement
113
113
  none: false
114
114
  requirements: