semantic_logger 0.6.0 → 0.6.1
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.
- data/README.md +1 -1
- data/lib/semantic_logger/base.rb +14 -1
- data/lib/semantic_logger/logger.rb +34 -0
- data/lib/semantic_logger/version.rb +1 -1
- data/test/appender_file_test.rb +27 -0
- metadata +2 -2
data/README.md
CHANGED
@@ -285,7 +285,7 @@ For example to replace the Rails or Ruby text log formatter, in the environment
|
|
285
285
|
end
|
286
286
|
end
|
287
287
|
|
288
|
-
str = "#{log.time.strftime("%Y-%m-%d %H:%M:%S")}.#{"%
|
288
|
+
str = "#{log.time.strftime("%Y-%m-%d %H:%M:%S")}.#{"%06d" % log.time.usec} #{"%-05s" % log.level.to_s.upcase} [#{$$}:#{log.thread_name}] #{tags}#{log.name} -- #{message}"
|
289
289
|
str << " (#{'%.1f' % log.duration}ms)" if log.duration
|
290
290
|
str
|
291
291
|
end
|
data/lib/semantic_logger/base.rb
CHANGED
@@ -37,7 +37,7 @@ module SemanticLogger
|
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
40
|
-
str = "#{
|
40
|
+
str = "#{SemanticLogger::Base.formatted_time(log.time)} #{log.level.to_s[0..0].upcase} [#{$$}:#{log.thread_name}] #{tags}#{log.name} -- #{message}"
|
41
41
|
str << " (#{'%.1f' % log.duration}ms)" if log.duration
|
42
42
|
str
|
43
43
|
end
|
@@ -240,13 +240,26 @@ module SemanticLogger
|
|
240
240
|
|
241
241
|
# For JRuby include the Thread name rather than its id
|
242
242
|
if defined? Java
|
243
|
+
# Name of the current Thread
|
243
244
|
def self.thread_name
|
244
245
|
Java::java.lang::Thread.current_thread.name
|
245
246
|
end
|
247
|
+
|
248
|
+
# Return the Time as a formatted string
|
249
|
+
# JRuby only supports time in ms
|
250
|
+
def self.formatted_time(time)
|
251
|
+
"#{time.strftime("%Y-%m-%d %H:%M:%S")}.#{"%03d" % (time.usec/1000)}"
|
252
|
+
end
|
246
253
|
else
|
247
254
|
def self.thread_name
|
248
255
|
Thread.current.object_id
|
249
256
|
end
|
257
|
+
|
258
|
+
# Return the Time as a formatted string
|
259
|
+
# Ruby MRI supports micro seconds
|
260
|
+
def self.formatted_time(time)
|
261
|
+
"#{time.strftime("%Y-%m-%d %H:%M:%S")}.#{"%06d" % (time.usec)}"
|
262
|
+
end
|
250
263
|
end
|
251
264
|
|
252
265
|
# Internal method to return the log level as an internal index
|
@@ -93,6 +93,31 @@ module SemanticLogger
|
|
93
93
|
reply_queue.pop
|
94
94
|
end
|
95
95
|
|
96
|
+
@@lag_check_interval = 1000
|
97
|
+
@@lag_threshold_s = 30
|
98
|
+
|
99
|
+
# Returns the check_interval which is the number of messages between checks
|
100
|
+
# to determine if the appender thread is falling behind
|
101
|
+
def self.lag_check_interval
|
102
|
+
@@lag_check_interval
|
103
|
+
end
|
104
|
+
|
105
|
+
# Set the check_interval which is the number of messages between checks
|
106
|
+
# to determine if the appender thread is falling behind
|
107
|
+
def self.lag_check_interval=(lag_check_interval)
|
108
|
+
@@lag_check_interval = lag_check_interval
|
109
|
+
end
|
110
|
+
|
111
|
+
# Returns the amount of time in seconds
|
112
|
+
# to determine if the appender thread is falling behind
|
113
|
+
def self.lag_threshold_s
|
114
|
+
@@lag_threshold_s
|
115
|
+
end
|
116
|
+
|
117
|
+
def self.time_threshold_s=(time_threshold_s)
|
118
|
+
@@lag_threshold_s = time_threshold_s
|
119
|
+
end
|
120
|
+
|
96
121
|
############################################################################
|
97
122
|
protected
|
98
123
|
|
@@ -128,9 +153,18 @@ module SemanticLogger
|
|
128
153
|
@@appender_thread = Thread.new do
|
129
154
|
begin
|
130
155
|
logger.debug "SemanticLogger::Logger Appender thread started"
|
156
|
+
count = 0
|
131
157
|
while message=queue.pop
|
132
158
|
if message.is_a? Log
|
133
159
|
appenders.each {|appender| appender.log(message) }
|
160
|
+
count += 1
|
161
|
+
# Check every few log messages whether this appender thread is falling behind
|
162
|
+
if count > lag_check_interval
|
163
|
+
if (diff = Time.now - message.time) > lag_threshold_s
|
164
|
+
logger.warn "SemanticLogger::Logger Appender thread has fallen behind by #{diff} seconds with #{cache_count} messages queued up. Consider reducing the log level or changing the appenders"
|
165
|
+
end
|
166
|
+
count = 0
|
167
|
+
end
|
134
168
|
else
|
135
169
|
case message[:command]
|
136
170
|
when :shutdown
|
data/test/appender_file_test.rb
CHANGED
@@ -48,5 +48,32 @@ class AppenderFileTest < Test::Unit::TestCase
|
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
51
|
+
context "custom formatter" do
|
52
|
+
setup do
|
53
|
+
@appender = SemanticLogger::Appender::File.new(@io) do |log|
|
54
|
+
message = log.message.to_s
|
55
|
+
tags = log.tags.collect { |tag| "[#{tag}]" }.join(" ") + " " if log.tags && (log.tags.size > 0)
|
56
|
+
|
57
|
+
if log.payload
|
58
|
+
if log.payload.is_a?(Exception)
|
59
|
+
exception = log.payload
|
60
|
+
message << " -- " << "#{exception.class}: #{exception.message}\n#{(exception.backtrace || []).join("\n")}"
|
61
|
+
else
|
62
|
+
message << " -- " << log.payload.inspect
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
str = "#{log.time.strftime("%Y-%m-%d %H:%M:%S")}.#{"%03d" % (log.time.usec/1000)} #{log.level.to_s.upcase} [#{$$}:#{log.thread_name}] #{tags}#{log.name} -- #{message}"
|
67
|
+
str << " (#{'%.1f' % log.duration}ms)" if log.duration
|
68
|
+
str
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
should "format using formatter" do
|
73
|
+
@appender.debug
|
74
|
+
assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ DEBUG \[\d+:#{@thread_name}\] SemanticLogger::Appender::File -- \n/, @io.string
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
51
78
|
end
|
52
79
|
end
|