semantic_logger 4.3.1 → 4.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/semantic_logger.rb +7 -1
- data/lib/semantic_logger/appender.rb +3 -0
- data/lib/semantic_logger/appender/async.rb +29 -10
- data/lib/semantic_logger/appender/rabbitmq.rb +120 -0
- data/lib/semantic_logger/appenders.rb +89 -0
- data/lib/semantic_logger/base.rb +3 -3
- data/lib/semantic_logger/concerns/compatibility.rb +2 -2
- data/lib/semantic_logger/formatters.rb +1 -0
- data/lib/semantic_logger/formatters/base.rb +28 -6
- data/lib/semantic_logger/formatters/color.rb +4 -3
- data/lib/semantic_logger/formatters/fluentd.rb +37 -0
- data/lib/semantic_logger/formatters/json.rb +4 -2
- data/lib/semantic_logger/formatters/raw.rb +2 -2
- data/lib/semantic_logger/formatters/signalfx.rb +4 -3
- data/lib/semantic_logger/levels.rb +38 -0
- data/lib/semantic_logger/log.rb +11 -6
- data/lib/semantic_logger/loggable.rb +1 -1
- data/lib/semantic_logger/logger.rb +43 -1
- data/lib/semantic_logger/processor.rb +10 -130
- data/lib/semantic_logger/reporters/minitest.rb +49 -0
- data/lib/semantic_logger/semantic_logger.rb +40 -75
- data/lib/semantic_logger/version.rb +1 -1
- metadata +9 -81
- data/test/appender/async_batch_test.rb +0 -60
- data/test/appender/async_test.rb +0 -44
- data/test/appender/bugsnag_test.rb +0 -81
- data/test/appender/elasticsearch_http_test.rb +0 -74
- data/test/appender/elasticsearch_test.rb +0 -248
- data/test/appender/file_test.rb +0 -120
- data/test/appender/graylog_test.rb +0 -82
- data/test/appender/honeybadger_test.rb +0 -45
- data/test/appender/http_test.rb +0 -63
- data/test/appender/kafka_test.rb +0 -35
- data/test/appender/mongodb_test.rb +0 -104
- data/test/appender/new_relic_test.rb +0 -80
- data/test/appender/newrelic_rpm.rb +0 -14
- data/test/appender/sentry_test.rb +0 -47
- data/test/appender/splunk_http_test.rb +0 -79
- data/test/appender/splunk_test.rb +0 -83
- data/test/appender/syslog_test.rb +0 -61
- data/test/appender/tcp_test.rb +0 -66
- data/test/appender/udp_test.rb +0 -59
- data/test/appender/wrapper_test.rb +0 -95
- data/test/concerns/compatibility_test.rb +0 -117
- data/test/debug_as_trace_logger_test.rb +0 -81
- data/test/formatters/color_test.rb +0 -153
- data/test/formatters/default_test.rb +0 -175
- data/test/formatters/one_line_test.rb +0 -60
- data/test/formatters/signalfx_test.rb +0 -197
- data/test/formatters_test.rb +0 -36
- data/test/in_memory_appender.rb +0 -8
- data/test/in_memory_appender_helper.rb +0 -43
- data/test/in_memory_batch_appender.rb +0 -8
- data/test/in_memory_metrics_appender.rb +0 -13
- data/test/loggable_test.rb +0 -103
- data/test/logger_test.rb +0 -334
- data/test/measure_test.rb +0 -346
- data/test/metric/new_relic_test.rb +0 -35
- data/test/metric/signalfx_test.rb +0 -77
- data/test/semantic_logger_test.rb +0 -303
- data/test/test_helper.rb +0 -31
data/test/logger_test.rb
DELETED
@@ -1,334 +0,0 @@
|
|
1
|
-
require_relative 'test_helper'
|
2
|
-
|
3
|
-
# Unit Test for SemanticLogger::Logger
|
4
|
-
class LoggerTest < Minitest::Test
|
5
|
-
describe SemanticLogger::Logger do
|
6
|
-
include InMemoryAppenderHelper
|
7
|
-
|
8
|
-
let :dimensions do
|
9
|
-
{action: 'hit', user: 'jbloggs', state: 'FL'}
|
10
|
-
end
|
11
|
-
|
12
|
-
# Complex filters
|
13
|
-
module ComplexFilter
|
14
|
-
def self.call(log)
|
15
|
-
(/\AExclude/ =~ log.message).nil?
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
# Ensure that any log level can be logged
|
20
|
-
SemanticLogger::LEVELS.each do |level|
|
21
|
-
describe "##{level}" do
|
22
|
-
describe 'positional parameter' do
|
23
|
-
it 'logs message' do
|
24
|
-
logger.send(level, 'hello world')
|
25
|
-
|
26
|
-
assert log = log_message
|
27
|
-
assert_equal 'hello world', log.message
|
28
|
-
end
|
29
|
-
|
30
|
-
it 'adds message from block' do
|
31
|
-
logger.send(level, 'hello world') { 'Calculations' }
|
32
|
-
|
33
|
-
assert log = log_message
|
34
|
-
assert_equal 'hello world -- Calculations', log.message
|
35
|
-
end
|
36
|
-
|
37
|
-
it 'logs message and payload' do
|
38
|
-
logger.send(level, 'hello world', payload)
|
39
|
-
|
40
|
-
assert log = log_message
|
41
|
-
assert_equal 'hello world', log.message
|
42
|
-
assert_equal payload, log.payload
|
43
|
-
end
|
44
|
-
|
45
|
-
it 'logs with backtrace' do
|
46
|
-
SemanticLogger.stub(:backtrace_level_index, 0) do
|
47
|
-
logger.send(level, 'hello world', payload) { 'Calculations' }
|
48
|
-
|
49
|
-
assert log = log_message
|
50
|
-
assert_equal 'hello world -- Calculations', log.message
|
51
|
-
assert_equal payload, log.payload
|
52
|
-
assert log.backtrace
|
53
|
-
assert log.backtrace.size.positive?, log.backtrace
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
it 'logs with backtrace and exception' do
|
58
|
-
SemanticLogger.stub(:backtrace_level_index, 0) do
|
59
|
-
exc = RuntimeError.new('Test')
|
60
|
-
logger.send(level, 'hello world', exc)
|
61
|
-
|
62
|
-
assert log = log_message
|
63
|
-
assert_equal 'hello world', log.message
|
64
|
-
assert log.backtrace
|
65
|
-
assert log.backtrace.size.positive?, log.backtrace
|
66
|
-
|
67
|
-
assert log.exception
|
68
|
-
refute log.exception.backtrace
|
69
|
-
assert_equal 'RuntimeError', log.exception.class.name
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
describe 'keyword arguments' do
|
75
|
-
it 'logs message' do
|
76
|
-
logger.send(level, message: 'hello world')
|
77
|
-
|
78
|
-
assert log = log_message
|
79
|
-
assert_equal 'hello world', log.message
|
80
|
-
end
|
81
|
-
|
82
|
-
it 'logs payload and message' do
|
83
|
-
logger.send(level, message: 'hello world', payload: payload)
|
84
|
-
|
85
|
-
assert log = log_message
|
86
|
-
assert_equal 'hello world', log.message
|
87
|
-
assert_equal payload, log.payload
|
88
|
-
end
|
89
|
-
|
90
|
-
it 'logs payload and message from block' do
|
91
|
-
logger.send(level) { {message: 'hello world', payload: payload} }
|
92
|
-
|
93
|
-
assert log = log_message
|
94
|
-
assert_equal 'hello world', log.message
|
95
|
-
assert_equal payload, log.payload
|
96
|
-
end
|
97
|
-
|
98
|
-
it 'logs payload from block' do
|
99
|
-
logger.send(level) { {'test_key1' => 'hello world', 'test_key2' => payload} }
|
100
|
-
|
101
|
-
assert log = log_message
|
102
|
-
assert_equal log.payload, 'test_key1' => 'hello world', 'test_key2' => payload
|
103
|
-
end
|
104
|
-
|
105
|
-
it 'logs payload only' do
|
106
|
-
logger.send(level, payload: payload)
|
107
|
-
|
108
|
-
assert log = log_message
|
109
|
-
refute log.message
|
110
|
-
assert_equal payload, log.payload
|
111
|
-
end
|
112
|
-
|
113
|
-
it 'logs duration' do
|
114
|
-
logger.send(level, duration: 123.44, message: 'hello world', payload: payload)
|
115
|
-
|
116
|
-
assert log = log_message
|
117
|
-
assert_equal 'hello world', log.message
|
118
|
-
assert_equal payload, log.payload
|
119
|
-
assert_equal 123.44, log.duration
|
120
|
-
end
|
121
|
-
|
122
|
-
it 'does not log when below min_duration' do
|
123
|
-
logger.send(level, min_duration: 200, duration: 123.45, message: 'hello world', payload: {tracking_number: '123456', even: 2, more: 'data'})
|
124
|
-
|
125
|
-
refute log_message
|
126
|
-
end
|
127
|
-
|
128
|
-
it 'logs metric' do
|
129
|
-
metric_name = '/my/custom/metric'
|
130
|
-
logger.send(level, metric: metric_name, duration: 123.44, message: 'hello world', payload: payload)
|
131
|
-
|
132
|
-
assert log = log_message
|
133
|
-
assert_equal 'hello world', log.message
|
134
|
-
assert_equal payload, log.payload
|
135
|
-
assert_equal 123.44, log.duration
|
136
|
-
assert_equal metric_name, log.metric
|
137
|
-
end
|
138
|
-
|
139
|
-
describe 'metrics appender' do
|
140
|
-
let :appender do
|
141
|
-
InMemoryMetricsAppender.new
|
142
|
-
end
|
143
|
-
|
144
|
-
it 'logs metric only events' do
|
145
|
-
metric_name = '/my/custom/metric'
|
146
|
-
logger.send(level, metric: metric_name, dimensions: dimensions)
|
147
|
-
|
148
|
-
assert log = log_message
|
149
|
-
assert_equal metric_name, log.metric
|
150
|
-
assert_equal dimensions, log.dimensions
|
151
|
-
refute log.message
|
152
|
-
end
|
153
|
-
end
|
154
|
-
|
155
|
-
it 'for compatibility handles random payload logged as keyword arguments' do
|
156
|
-
logger.send(level, payload)
|
157
|
-
|
158
|
-
assert log = log_message
|
159
|
-
refute log.message
|
160
|
-
refute log.exception
|
161
|
-
refute log.metric
|
162
|
-
assert_equal payload, log.payload
|
163
|
-
end
|
164
|
-
end
|
165
|
-
|
166
|
-
describe '#filter' do
|
167
|
-
describe 'at the appender level' do
|
168
|
-
it 'Proc' do
|
169
|
-
appender.filter = ->(log) { (/\AExclude/ =~ log.message).nil? }
|
170
|
-
logger.send(level, 'Exclude this log message', @hash) { 'Calculations' }
|
171
|
-
|
172
|
-
refute log_message
|
173
|
-
end
|
174
|
-
|
175
|
-
it 'Module' do
|
176
|
-
appender.filter = ComplexFilter
|
177
|
-
logger.send(level, 'Exclude this log message', @hash) { 'Calculations' }
|
178
|
-
|
179
|
-
refute log_message
|
180
|
-
end
|
181
|
-
|
182
|
-
it 'RegExp' do
|
183
|
-
filter = /\ALogger/
|
184
|
-
appender.filter = ->(log) { (/\AExclude/ =~ log.message).nil? }
|
185
|
-
logger.send(level, 'Exclude this log message', @hash) { 'Calculations' }
|
186
|
-
|
187
|
-
refute log_message
|
188
|
-
end
|
189
|
-
end
|
190
|
-
|
191
|
-
describe 'at the logger level' do
|
192
|
-
it 'Proc' do
|
193
|
-
logger.filter = ->(log) { (/\AExclude/ =~ log.message).nil? }
|
194
|
-
logger.send(level, 'Exclude this log message', @hash) { 'Calculations' }
|
195
|
-
|
196
|
-
refute log_message
|
197
|
-
end
|
198
|
-
|
199
|
-
it 'Module' do
|
200
|
-
logger.filter = ComplexFilter
|
201
|
-
logger.send(level, 'Exclude this log message', @hash) { 'Calculations' }
|
202
|
-
|
203
|
-
refute log_message
|
204
|
-
end
|
205
|
-
|
206
|
-
it 'RegExp' do
|
207
|
-
filter = /\ALogger/
|
208
|
-
logger.filter = ->(log) { (/\AExclude/ =~ log.message).nil? }
|
209
|
-
logger.send(level, 'Exclude this log message', @hash) { 'Calculations' }
|
210
|
-
|
211
|
-
refute log_message
|
212
|
-
end
|
213
|
-
end
|
214
|
-
end
|
215
|
-
end
|
216
|
-
end
|
217
|
-
|
218
|
-
describe 'when level is too high' do
|
219
|
-
it 'does not log' do
|
220
|
-
SemanticLogger.default_level = :error
|
221
|
-
logger.info('Exclude this log message')
|
222
|
-
|
223
|
-
refute log_message
|
224
|
-
end
|
225
|
-
end
|
226
|
-
|
227
|
-
describe 'Compatibility' do
|
228
|
-
# Ensure that any log level can be logged
|
229
|
-
Logger::Severity.constants.each do |level|
|
230
|
-
it "log Ruby logger #{level} info" do
|
231
|
-
logger.level = Logger::Severity.const_get(level)
|
232
|
-
if level.to_s == 'UNKNOWN'
|
233
|
-
assert_equal Logger::Severity.const_get('ERROR') + 1, logger.send(:level_index)
|
234
|
-
else
|
235
|
-
assert_equal Logger::Severity.const_get(level) + 1, logger.send(:level_index)
|
236
|
-
end
|
237
|
-
end
|
238
|
-
end
|
239
|
-
end
|
240
|
-
|
241
|
-
describe '#level?' do
|
242
|
-
it 'return true for debug? with :trace level' do
|
243
|
-
SemanticLogger.default_level = :trace
|
244
|
-
assert_equal :trace, logger.level
|
245
|
-
assert_equal true, logger.debug?
|
246
|
-
assert_equal true, logger.trace?
|
247
|
-
end
|
248
|
-
|
249
|
-
it 'return false for debug? with global :debug level' do
|
250
|
-
SemanticLogger.default_level = :debug
|
251
|
-
assert_equal :debug, logger.level, logger.inspect
|
252
|
-
assert_equal true, logger.debug?, logger.inspect
|
253
|
-
assert_equal false, logger.trace?, logger.inspect
|
254
|
-
end
|
255
|
-
|
256
|
-
it 'return true for debug? with global :info level' do
|
257
|
-
SemanticLogger.default_level = :info
|
258
|
-
assert_equal :info, logger.level, logger.inspect
|
259
|
-
assert_equal false, logger.debug?, logger.inspect
|
260
|
-
assert_equal false, logger.trace?, logger.inspect
|
261
|
-
end
|
262
|
-
|
263
|
-
it 'return false for debug? with instance :debug level' do
|
264
|
-
logger.level = :debug
|
265
|
-
assert_equal :debug, logger.level, logger.inspect
|
266
|
-
assert_equal true, logger.debug?, logger.inspect
|
267
|
-
assert_equal false, logger.trace?, logger.inspect
|
268
|
-
end
|
269
|
-
|
270
|
-
it 'return true for debug? with instance :info level' do
|
271
|
-
logger.level = :info
|
272
|
-
assert_equal :info, logger.level, logger.inspect
|
273
|
-
assert_equal false, logger.debug?, logger.inspect
|
274
|
-
assert_equal false, logger.trace?, logger.inspect
|
275
|
-
end
|
276
|
-
end
|
277
|
-
|
278
|
-
describe '.tagged' do
|
279
|
-
it 'sets global defaults' do
|
280
|
-
assert_equal [], SemanticLogger.tags
|
281
|
-
assert_equal 0, SemanticLogger.backtrace_level_index
|
282
|
-
end
|
283
|
-
|
284
|
-
it 'add tags to log entries' do
|
285
|
-
logger.tagged('12345', 'DJHSFK') do
|
286
|
-
logger.info('hello world')
|
287
|
-
|
288
|
-
assert log = log_message
|
289
|
-
assert_equal 'hello world', log.message
|
290
|
-
assert_equal %w[12345 DJHSFK], log.tags
|
291
|
-
end
|
292
|
-
end
|
293
|
-
|
294
|
-
it 'add embedded tags to log entries' do
|
295
|
-
logger.tagged('First Level', 'tags') do
|
296
|
-
logger.tagged('Second Level') do
|
297
|
-
assert_equal ['First Level', 'tags', 'Second Level'], logger.tags
|
298
|
-
logger.info('hello world')
|
299
|
-
|
300
|
-
assert log = log_message
|
301
|
-
assert_equal 'hello world', log.message
|
302
|
-
assert_equal ['First Level', 'tags', 'Second Level'], log.tags
|
303
|
-
end
|
304
|
-
assert_equal ['First Level', 'tags'], logger.tags
|
305
|
-
end
|
306
|
-
end
|
307
|
-
|
308
|
-
it 'also supports named tagging' do
|
309
|
-
logger.tagged(level1: 1) do
|
310
|
-
assert_equal({level1: 1}, SemanticLogger.named_tags)
|
311
|
-
assert_equal({level1: 1}, logger.named_tags)
|
312
|
-
logger.tagged(level2: 2, more: 'data') do
|
313
|
-
assert_equal({level1: 1, level2: 2, more: 'data'}, SemanticLogger.named_tags)
|
314
|
-
assert_equal({level1: 1, level2: 2, more: 'data'}, logger.named_tags)
|
315
|
-
logger.tagged(level3: 3) do
|
316
|
-
assert_equal({level1: 1, level2: 2, more: 'data', level3: 3}, SemanticLogger.named_tags)
|
317
|
-
assert_equal({level1: 1, level2: 2, more: 'data', level3: 3}, logger.named_tags)
|
318
|
-
end
|
319
|
-
end
|
320
|
-
end
|
321
|
-
end
|
322
|
-
|
323
|
-
it 'is compatible with rails logging that uses arrays and nils' do
|
324
|
-
logger.tagged('', %w[12345 DJHSFK], nil) do
|
325
|
-
logger.info('hello world')
|
326
|
-
|
327
|
-
assert log = log_message
|
328
|
-
assert_equal 'hello world', log.message
|
329
|
-
assert_equal %w[12345 DJHSFK], log.tags
|
330
|
-
end
|
331
|
-
end
|
332
|
-
end
|
333
|
-
end
|
334
|
-
end
|
data/test/measure_test.rb
DELETED
@@ -1,346 +0,0 @@
|
|
1
|
-
require_relative 'test_helper'
|
2
|
-
|
3
|
-
class MeasureTest < Minitest::Test
|
4
|
-
describe 'Measure' do
|
5
|
-
include InMemoryAppenderHelper
|
6
|
-
|
7
|
-
# Ensure that any log level can be measured and logged
|
8
|
-
SemanticLogger::LEVELS.each do |level|
|
9
|
-
measure_level = "measure_#{level}".to_sym
|
10
|
-
|
11
|
-
describe "##{measure_level}" do
|
12
|
-
it ':message' do
|
13
|
-
assert_equal 'result', logger.send(measure_level, 'hello world') { 'result' }
|
14
|
-
|
15
|
-
assert log = log_message
|
16
|
-
assert_equal 'hello world', log.message
|
17
|
-
end
|
18
|
-
|
19
|
-
it ':level' do
|
20
|
-
assert_equal 'result', logger.send(measure_level, 'hello world') { 'result' }
|
21
|
-
|
22
|
-
assert log = log_message
|
23
|
-
assert_equal level, log.level
|
24
|
-
end
|
25
|
-
|
26
|
-
it ':payload' do
|
27
|
-
assert_equal 'result', logger.send(measure_level, 'hello world', payload: payload) { 'result' }
|
28
|
-
|
29
|
-
assert log = log_message
|
30
|
-
assert_equal payload, log.payload
|
31
|
-
end
|
32
|
-
|
33
|
-
describe ':min_duration' do
|
34
|
-
it 'not log when faster' do
|
35
|
-
assert_equal 'result', logger.send(measure_level, 'hello world', min_duration: 2000) { 'result' }
|
36
|
-
refute log_message
|
37
|
-
end
|
38
|
-
|
39
|
-
it 'log when slower' do
|
40
|
-
assert_equal 'result', logger.send(measure_level, 'hello world', min_duration: 200, payload: payload) { sleep 0.5; 'result' }
|
41
|
-
|
42
|
-
assert log = log_message
|
43
|
-
assert_equal 'hello world', log.message
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
it ':exception' do
|
48
|
-
assert_raises RuntimeError do
|
49
|
-
logger.send(measure_level, 'hello world', payload: payload) { raise 'Test' }
|
50
|
-
end
|
51
|
-
|
52
|
-
assert log = log_message
|
53
|
-
refute log.exception
|
54
|
-
assert_equal 'hello world -- Exception: RuntimeError: Test', log.message
|
55
|
-
assert_equal level, log.level
|
56
|
-
end
|
57
|
-
|
58
|
-
it ':on_exception_level' do
|
59
|
-
assert_raises RuntimeError do
|
60
|
-
logger.measure(level, 'hello world', payload: payload, on_exception_level: :fatal) { raise 'Test' }
|
61
|
-
end
|
62
|
-
|
63
|
-
assert log = log_message
|
64
|
-
refute log.exception
|
65
|
-
assert_equal 'hello world -- Exception: RuntimeError: Test', log.message
|
66
|
-
assert_equal :fatal, log.level
|
67
|
-
end
|
68
|
-
|
69
|
-
describe 'log_exception' do
|
70
|
-
it 'default' do
|
71
|
-
assert_raises RuntimeError do
|
72
|
-
logger.send(measure_level, 'hello world') { raise 'Test' }
|
73
|
-
end
|
74
|
-
|
75
|
-
assert log = log_message
|
76
|
-
refute log.exception
|
77
|
-
assert_equal 'hello world -- Exception: RuntimeError: Test', log.message
|
78
|
-
end
|
79
|
-
|
80
|
-
it ':full' do
|
81
|
-
assert_raises RuntimeError do
|
82
|
-
logger.send(measure_level, 'hello world', log_exception: :full) { raise 'Test' }
|
83
|
-
end
|
84
|
-
|
85
|
-
assert log = log_message
|
86
|
-
assert log.exception.is_a?(RuntimeError)
|
87
|
-
assert log.exception.backtrace
|
88
|
-
assert_equal level, log.level
|
89
|
-
assert_equal 'hello world', log.message
|
90
|
-
end
|
91
|
-
|
92
|
-
it ':partial' do
|
93
|
-
assert_raises RuntimeError do
|
94
|
-
logger.send(measure_level, 'hello world', log_exception: :partial) { raise 'Test' }
|
95
|
-
end
|
96
|
-
|
97
|
-
assert log = log_message
|
98
|
-
refute log.exception
|
99
|
-
assert_equal 'hello world -- Exception: RuntimeError: Test', log.message
|
100
|
-
end
|
101
|
-
|
102
|
-
it ':none' do
|
103
|
-
assert_raises RuntimeError do
|
104
|
-
logger.send(measure_level, 'hello world', log_exception: :none) { raise 'Test' }
|
105
|
-
end
|
106
|
-
|
107
|
-
assert log = log_message
|
108
|
-
refute log.exception
|
109
|
-
assert_equal 'hello world', log.message
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
it ':metric' do
|
114
|
-
metric_name = '/my/custom/metric'
|
115
|
-
assert_equal 'result', logger.send(measure_level, 'hello world', metric: metric_name) { 'result' }
|
116
|
-
|
117
|
-
assert log = log_message
|
118
|
-
assert_equal metric_name, log.metric
|
119
|
-
end
|
120
|
-
|
121
|
-
it ':backtrace_level' do
|
122
|
-
SemanticLogger.stub(:backtrace_level_index, 0) do
|
123
|
-
assert_equal 'result', logger.send(measure_level, 'hello world') { 'result' }
|
124
|
-
|
125
|
-
assert log = log_message
|
126
|
-
assert log.backtrace
|
127
|
-
assert log.backtrace.size.positive?
|
128
|
-
|
129
|
-
# Extract file name and line number from backtrace
|
130
|
-
h = log.to_h
|
131
|
-
assert_match /measure_test.rb/, h[:file], h
|
132
|
-
assert h[:line].is_a?(Integer)
|
133
|
-
end
|
134
|
-
end
|
135
|
-
end
|
136
|
-
|
137
|
-
describe "#measure(#{level})" do
|
138
|
-
it ':message' do
|
139
|
-
assert_equal 'result', logger.measure(level, 'hello world') { 'result' }
|
140
|
-
|
141
|
-
assert log = log_message
|
142
|
-
assert_equal 'hello world', log.message
|
143
|
-
end
|
144
|
-
|
145
|
-
it ':level' do
|
146
|
-
assert_equal 'result', logger.measure(level, 'hello world') { 'result' }
|
147
|
-
|
148
|
-
assert log = log_message
|
149
|
-
assert_equal level, log.level
|
150
|
-
end
|
151
|
-
|
152
|
-
it ':payload' do
|
153
|
-
assert_equal 'result', logger.measure(level, 'hello world', payload: payload) { 'result' }
|
154
|
-
|
155
|
-
assert log = log_message
|
156
|
-
assert_equal payload, log.payload
|
157
|
-
end
|
158
|
-
|
159
|
-
describe ':min_duration' do
|
160
|
-
it 'not log when faster' do
|
161
|
-
assert_equal 'result', logger.measure(level, 'hello world', min_duration: 2000) { 'result' }
|
162
|
-
refute log_message
|
163
|
-
end
|
164
|
-
|
165
|
-
it 'log when slower' do
|
166
|
-
assert_equal 'result', logger.measure(level, 'hello world', min_duration: 200, payload: payload) { sleep 0.5; 'result' }
|
167
|
-
assert log = log_message
|
168
|
-
assert_equal 'hello world', log.message
|
169
|
-
end
|
170
|
-
end
|
171
|
-
|
172
|
-
it ':exception' do
|
173
|
-
assert_raises RuntimeError do
|
174
|
-
logger.measure(level, 'hello world', payload: payload) { raise 'Test' }
|
175
|
-
end
|
176
|
-
|
177
|
-
assert log = log_message
|
178
|
-
refute log.exception
|
179
|
-
assert_equal 'hello world -- Exception: RuntimeError: Test', log.message
|
180
|
-
assert_equal level, log.level
|
181
|
-
end
|
182
|
-
|
183
|
-
it ':on_exception_level' do
|
184
|
-
assert_raises RuntimeError do
|
185
|
-
logger.measure(level, 'hello world', payload: payload, on_exception_level: :fatal) { raise 'Test' }
|
186
|
-
end
|
187
|
-
|
188
|
-
assert log = log_message
|
189
|
-
refute log.exception
|
190
|
-
assert_equal 'hello world -- Exception: RuntimeError: Test', log.message
|
191
|
-
assert_equal :fatal, log.level
|
192
|
-
end
|
193
|
-
|
194
|
-
it ':metric' do
|
195
|
-
metric_name = '/my/custom/metric'
|
196
|
-
assert_equal 'result', logger.measure(level, 'hello world', metric: metric_name) { 'result' }
|
197
|
-
|
198
|
-
assert log = log_message
|
199
|
-
assert_equal metric_name, log.metric
|
200
|
-
end
|
201
|
-
|
202
|
-
it ':backtrace_level' do
|
203
|
-
SemanticLogger.stub(:backtrace_level_index, 0) do
|
204
|
-
assert_equal 'result', logger.measure(level, 'hello world') { 'result' }
|
205
|
-
|
206
|
-
assert log = log_message
|
207
|
-
assert log.backtrace
|
208
|
-
assert log.backtrace.size.positive?
|
209
|
-
|
210
|
-
# Extract file name and line number from backtrace
|
211
|
-
h = log.to_h
|
212
|
-
assert_match /measure_test.rb/, h[:file], h
|
213
|
-
assert h[:line].is_a?(Integer)
|
214
|
-
end
|
215
|
-
end
|
216
|
-
end
|
217
|
-
|
218
|
-
describe "##{measure_level} keyword arguments" do
|
219
|
-
it ':message' do
|
220
|
-
assert_equal 'result', logger.send(measure_level, message: 'hello world') { 'result' }
|
221
|
-
|
222
|
-
assert log = log_message
|
223
|
-
assert_equal 'hello world', log.message
|
224
|
-
end
|
225
|
-
|
226
|
-
it ':level' do
|
227
|
-
assert_equal 'result', logger.send(measure_level, message: 'hello world') { 'result' }
|
228
|
-
|
229
|
-
assert log = log_message
|
230
|
-
assert_equal level, log.level
|
231
|
-
end
|
232
|
-
|
233
|
-
it ':payload' do
|
234
|
-
assert_equal 'result', logger.send(measure_level, message: 'hello world', payload: payload) { 'result' }
|
235
|
-
|
236
|
-
assert log = log_message
|
237
|
-
assert_equal payload, log.payload
|
238
|
-
end
|
239
|
-
|
240
|
-
describe ':min_duration' do
|
241
|
-
it 'not log when faster' do
|
242
|
-
assert_equal 'result', logger.send(measure_level, message: 'hello world', min_duration: 2000) { 'result' }
|
243
|
-
refute log_message
|
244
|
-
end
|
245
|
-
|
246
|
-
it 'log when slower' do
|
247
|
-
assert_equal 'result', logger.send(measure_level, message: 'hello world', min_duration: 200, payload: payload) { sleep 0.5; 'result' }
|
248
|
-
|
249
|
-
assert log = log_message
|
250
|
-
assert_equal 'hello world', log.message
|
251
|
-
end
|
252
|
-
end
|
253
|
-
|
254
|
-
it ':exception' do
|
255
|
-
assert_raises RuntimeError do
|
256
|
-
logger.send(measure_level, message: 'hello world', payload: payload) { raise 'Test' }
|
257
|
-
end
|
258
|
-
|
259
|
-
assert log = log_message
|
260
|
-
refute log.exception
|
261
|
-
assert_equal 'hello world -- Exception: RuntimeError: Test', log.message
|
262
|
-
assert_equal level, log.level
|
263
|
-
end
|
264
|
-
|
265
|
-
it ':on_exception_level' do
|
266
|
-
assert_raises RuntimeError do
|
267
|
-
logger.send(measure_level, message: 'hello world', payload: payload, on_exception_level: :fatal) { raise 'Test' }
|
268
|
-
end
|
269
|
-
|
270
|
-
assert log = log_message
|
271
|
-
refute log.exception
|
272
|
-
assert_equal 'hello world -- Exception: RuntimeError: Test', log.message
|
273
|
-
assert_equal :fatal, log.level
|
274
|
-
end
|
275
|
-
|
276
|
-
it ':metric' do
|
277
|
-
metric_name = '/my/custom/metric'
|
278
|
-
assert_equal 'result', logger.send(measure_level, message: 'hello world', metric: metric_name) { 'result' }
|
279
|
-
|
280
|
-
assert log = log_message
|
281
|
-
assert_equal metric_name, log.metric
|
282
|
-
end
|
283
|
-
|
284
|
-
it ':backtrace_level' do
|
285
|
-
SemanticLogger.stub(:backtrace_level_index, 0) do
|
286
|
-
assert_equal 'result', logger.send(measure_level, message: 'hello world') { 'result' }
|
287
|
-
|
288
|
-
assert log = log_message
|
289
|
-
assert log.backtrace
|
290
|
-
assert log.backtrace.size.positive?
|
291
|
-
|
292
|
-
# Extract file name and line number from backtrace
|
293
|
-
h = log.to_h
|
294
|
-
assert_match /measure_test.rb/, h[:file], h
|
295
|
-
assert h[:line].is_a?(Integer)
|
296
|
-
end
|
297
|
-
end
|
298
|
-
end
|
299
|
-
end
|
300
|
-
|
301
|
-
describe 'return' do
|
302
|
-
it 'log when the block performs a return' do
|
303
|
-
assert_equal 'Good', function_with_return(logger)
|
304
|
-
|
305
|
-
assert log = log_message
|
306
|
-
assert_equal 'hello world', log.message
|
307
|
-
end
|
308
|
-
end
|
309
|
-
|
310
|
-
describe ':silence' do
|
311
|
-
it 'silences messages' do
|
312
|
-
SemanticLogger.default_level = :info
|
313
|
-
logger.measure_info('hello world', silence: :error) do
|
314
|
-
logger.warn "don't log me"
|
315
|
-
end
|
316
|
-
|
317
|
-
assert log = log_message
|
318
|
-
assert_equal 'hello world', log.message
|
319
|
-
end
|
320
|
-
|
321
|
-
it 'does not silence higher level messages' do
|
322
|
-
SemanticLogger.default_level = :info
|
323
|
-
first = nil
|
324
|
-
logger.measure_info('hello world', silence: :trace) do
|
325
|
-
logger.debug('hello world', payload) { 'Calculations' }
|
326
|
-
first = log_message
|
327
|
-
end
|
328
|
-
assert_equal 'hello world -- Calculations', first.message
|
329
|
-
assert_equal payload, first.payload
|
330
|
-
|
331
|
-
SemanticLogger.flush
|
332
|
-
assert log = appender.message
|
333
|
-
assert_equal 'hello world', log.message
|
334
|
-
end
|
335
|
-
end
|
336
|
-
|
337
|
-
# Make sure that measure still logs when a block uses return to return from
|
338
|
-
# a function
|
339
|
-
def function_with_return(logger)
|
340
|
-
logger.measure_info('hello world', payload: payload) do
|
341
|
-
return 'Good'
|
342
|
-
end
|
343
|
-
'Bad'
|
344
|
-
end
|
345
|
-
end
|
346
|
-
end
|