semantic_logger 3.0.1 → 3.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/semantic_logger.rb +29 -13
- data/lib/semantic_logger/ansi_colors.rb +27 -0
- data/lib/semantic_logger/appender/base.rb +54 -128
- data/lib/semantic_logger/appender/bugsnag.rb +29 -19
- data/lib/semantic_logger/appender/elasticsearch.rb +9 -9
- data/lib/semantic_logger/appender/file.rb +40 -18
- data/lib/semantic_logger/appender/graylog.rb +30 -26
- data/lib/semantic_logger/appender/http.rb +14 -19
- data/lib/semantic_logger/appender/mongodb.rb +20 -20
- data/lib/semantic_logger/appender/new_relic.rb +15 -15
- data/lib/semantic_logger/appender/splunk.rb +1 -1
- data/lib/semantic_logger/appender/splunk_http.rb +28 -25
- data/lib/semantic_logger/appender/syslog.rb +41 -42
- data/lib/semantic_logger/appender/wrapper.rb +19 -17
- data/lib/semantic_logger/base.rb +57 -32
- data/lib/semantic_logger/concerns/compatibility.rb +51 -0
- data/lib/semantic_logger/debug_as_trace_logger.rb +6 -2
- data/lib/semantic_logger/formatters/color.rb +66 -0
- data/lib/semantic_logger/formatters/default.rb +39 -0
- data/lib/semantic_logger/formatters/json.rb +16 -0
- data/lib/semantic_logger/jruby/garbage_collection_logger.rb +1 -1
- data/lib/semantic_logger/log.rb +13 -8
- data/lib/semantic_logger/loggable.rb +2 -2
- data/lib/semantic_logger/logger.rb +18 -13
- data/lib/semantic_logger/metrics/new_relic.rb +18 -0
- data/lib/semantic_logger/metrics/statsd.rb +48 -0
- data/lib/semantic_logger/semantic_logger.rb +122 -55
- data/lib/semantic_logger/version.rb +1 -1
- data/test/appender/bugsnag_test.rb +12 -3
- data/test/appender/mongodb_test.rb +6 -5
- data/test/appender/new_relic_test.rb +1 -1
- data/test/appender/splunk_http_test.rb +1 -0
- data/test/concerns/compatibility_test.rb +106 -0
- data/test/debug_as_trace_logger_test.rb +1 -1
- data/test/loggable_test.rb +1 -1
- data/test/logger_test.rb +183 -24
- metadata +12 -3
@@ -9,9 +9,18 @@ module Appender
|
|
9
9
|
@message = 'AppenderBugsnagTest log message'
|
10
10
|
end
|
11
11
|
|
12
|
-
|
12
|
+
SemanticLogger::LEVELS.each do |level|
|
13
13
|
it "sends #{level} message" do
|
14
|
-
|
14
|
+
bugsnag_level =
|
15
|
+
case level
|
16
|
+
when :warn
|
17
|
+
'warning'
|
18
|
+
when :fatal
|
19
|
+
'error'
|
20
|
+
else
|
21
|
+
level.to_s
|
22
|
+
end
|
23
|
+
exception = hash = nil
|
15
24
|
Bugsnag.stub(:notify, -> exc, h { exception = exc; hash = h }) do
|
16
25
|
@appender.send(level, @message)
|
17
26
|
end
|
@@ -21,7 +30,7 @@ module Appender
|
|
21
30
|
else
|
22
31
|
assert_equal 'RuntimeError', exception.class.to_s
|
23
32
|
assert_equal @message, exception.message
|
24
|
-
assert_equal
|
33
|
+
assert_equal bugsnag_level.to_s, hash[:severity]
|
25
34
|
end
|
26
35
|
end
|
27
36
|
|
@@ -23,7 +23,7 @@ module Appender
|
|
23
23
|
end
|
24
24
|
|
25
25
|
describe 'format logs into documents' do
|
26
|
-
it 'handle
|
26
|
+
it 'handle no arguments' do
|
27
27
|
@appender.debug
|
28
28
|
document = @appender.collection.find_one
|
29
29
|
assert_equal :debug, document['level']
|
@@ -36,15 +36,16 @@ module Appender
|
|
36
36
|
assert_equal 'test_application', document['application']
|
37
37
|
end
|
38
38
|
|
39
|
-
it
|
39
|
+
it 'handle hash message' do
|
40
40
|
@appender.debug(@hash)
|
41
41
|
|
42
42
|
document = @appender.collection.find_one
|
43
43
|
assert_equal :debug, document['level']
|
44
|
-
assert_equal
|
44
|
+
assert_equal nil, document['message']
|
45
45
|
assert_equal 'thread', document['thread']
|
46
46
|
assert document['time'].is_a?(Time)
|
47
|
-
|
47
|
+
assert_equal 12345, document['tracking_number']
|
48
|
+
assert_equal 'HSSKLEU@JDK767', document['session_id']
|
48
49
|
assert_equal $$, document['pid']
|
49
50
|
assert_equal 'test', document['host']
|
50
51
|
assert_equal 'test_application', document['application']
|
@@ -80,7 +81,7 @@ module Appender
|
|
80
81
|
end
|
81
82
|
end
|
82
83
|
|
83
|
-
describe
|
84
|
+
describe 'for each log level' do
|
84
85
|
# Ensure that any log level can be logged
|
85
86
|
SemanticLogger::LEVELS.each do |level|
|
86
87
|
it 'log #{level} information' do
|
@@ -46,7 +46,7 @@ module Appender
|
|
46
46
|
NewRelic::Agent.stub(:notice_error, -> exc, h { exception = exc; hash = h }) do
|
47
47
|
@appender.tagged('test') do
|
48
48
|
@appender.with_payload({key1: 1, key2: 'a'}) do
|
49
|
-
@appender.
|
49
|
+
@appender.measure(:error, @message) do
|
50
50
|
sleep 0.001
|
51
51
|
end
|
52
52
|
end
|
@@ -55,6 +55,7 @@ module Appender
|
|
55
55
|
end
|
56
56
|
body = decompress_data(request.body)
|
57
57
|
message = JSON.parse(body)
|
58
|
+
assert message['event'], message.ai
|
58
59
|
assert_equal @message, message['event']['message']
|
59
60
|
assert_equal level.to_s, message['event']['level']
|
60
61
|
refute message['event']['stack_trace']
|
@@ -0,0 +1,106 @@
|
|
1
|
+
require_relative '../test_helper'
|
2
|
+
|
3
|
+
# Unit Test for SemanticLogger::Logger
|
4
|
+
class CompatibilityTest < Minitest::Test
|
5
|
+
describe SemanticLogger::Logger do
|
6
|
+
before do
|
7
|
+
# Use a mock logger that just keeps the last logged entry in an instance
|
8
|
+
# variable
|
9
|
+
SemanticLogger.default_level = :trace
|
10
|
+
SemanticLogger.backtrace_level = nil
|
11
|
+
@mock_logger = MockLogger.new
|
12
|
+
@appender = SemanticLogger.add_appender(logger: @mock_logger)
|
13
|
+
|
14
|
+
# Use this test's class name as the application name in the log output
|
15
|
+
@logger = SemanticLogger[CompatibilityTest]
|
16
|
+
@thread_name = Thread.current.name
|
17
|
+
end
|
18
|
+
|
19
|
+
after do
|
20
|
+
SemanticLogger.remove_appender(@appender)
|
21
|
+
end
|
22
|
+
|
23
|
+
it '#add' do
|
24
|
+
@logger.add(Logger::INFO, 'hello world', 'progname') { 'Data' }
|
25
|
+
SemanticLogger.flush
|
26
|
+
assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ I \[\d+:#{@thread_name}\] CompatibilityTest -- hello world -- Data -- \"progname\"/, @mock_logger.message
|
27
|
+
end
|
28
|
+
|
29
|
+
it '#log' do
|
30
|
+
@logger.log(Logger::FATAL, 'hello world', 'progname') { 'Data' }
|
31
|
+
SemanticLogger.flush
|
32
|
+
assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ F \[\d+:#{@thread_name}\] CompatibilityTest -- hello world -- Data -- \"progname\"/, @mock_logger.message
|
33
|
+
end
|
34
|
+
|
35
|
+
it '#unknown' do
|
36
|
+
@logger.unknown('hello world') { 'Data' }
|
37
|
+
SemanticLogger.flush
|
38
|
+
assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ E \[\d+:#{@thread_name}\] CompatibilityTest -- hello world -- Data/, @mock_logger.message
|
39
|
+
end
|
40
|
+
|
41
|
+
it '#unknown? as error?' do
|
42
|
+
SemanticLogger.default_level = :error
|
43
|
+
assert @logger.unknown?
|
44
|
+
end
|
45
|
+
|
46
|
+
it '#unknown? as error? when false' do
|
47
|
+
SemanticLogger.default_level = :fatal
|
48
|
+
refute @logger.unknown?
|
49
|
+
end
|
50
|
+
|
51
|
+
it '#<< as info' do
|
52
|
+
@logger << 'hello world'
|
53
|
+
SemanticLogger.flush
|
54
|
+
assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ I \[\d+:#{@thread_name}\] CompatibilityTest -- hello world/, @mock_logger.message
|
55
|
+
end
|
56
|
+
|
57
|
+
it '#progname= as #name=' do
|
58
|
+
assert_equal 'CompatibilityTest', @logger.name
|
59
|
+
@logger.progname = 'NewTest'
|
60
|
+
assert_equal 'NewTest', @logger.name
|
61
|
+
end
|
62
|
+
|
63
|
+
it '#progname as #name' do
|
64
|
+
assert_equal 'CompatibilityTest', @logger.name
|
65
|
+
assert_equal 'CompatibilityTest', @logger.progname
|
66
|
+
end
|
67
|
+
|
68
|
+
it '#sev_threshold= as #level=' do
|
69
|
+
assert_equal :trace, @logger.level
|
70
|
+
@logger.sev_threshold = Logger::DEBUG
|
71
|
+
assert_equal :debug, @logger.level
|
72
|
+
end
|
73
|
+
|
74
|
+
it '#sev_threshold as #level' do
|
75
|
+
assert_equal :trace, @logger.level
|
76
|
+
assert_equal :trace, @logger.sev_threshold
|
77
|
+
end
|
78
|
+
|
79
|
+
it '#formatter NOOP' do
|
80
|
+
assert_equal nil, @logger.formatter
|
81
|
+
@logger.formatter = 'blah'
|
82
|
+
assert_equal 'blah', @logger.formatter
|
83
|
+
end
|
84
|
+
|
85
|
+
it '#datetime_format NOOP' do
|
86
|
+
assert_equal nil, @logger.datetime_format
|
87
|
+
@logger.datetime_format = 'blah'
|
88
|
+
assert_equal 'blah', @logger.datetime_format
|
89
|
+
end
|
90
|
+
|
91
|
+
it '#close NOOP' do
|
92
|
+
@logger.close
|
93
|
+
@logger.info('hello world') { 'Data' }
|
94
|
+
SemanticLogger.flush
|
95
|
+
assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ I \[\d+:#{@thread_name}\] CompatibilityTest -- hello world -- Data/, @mock_logger.message
|
96
|
+
end
|
97
|
+
|
98
|
+
it '#reopen NOOP' do
|
99
|
+
@logger.reopen
|
100
|
+
@logger.info('hello world') { 'Data' }
|
101
|
+
SemanticLogger.flush
|
102
|
+
assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ I \[\d+:#{@thread_name}\] CompatibilityTest -- hello world -- Data/, @mock_logger.message
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
106
|
+
end
|
@@ -11,7 +11,7 @@ class DebugAsTraceLoggerTest < Minitest::Test
|
|
11
11
|
# variable
|
12
12
|
SemanticLogger.default_level = :trace
|
13
13
|
@mock_logger = MockLogger.new
|
14
|
-
appender = SemanticLogger.add_appender(@mock_logger)
|
14
|
+
appender = SemanticLogger.add_appender(logger: @mock_logger)
|
15
15
|
appender.filter = filter
|
16
16
|
|
17
17
|
# Use this test's class name as the application name in the log output
|
data/test/loggable_test.rb
CHANGED
@@ -79,7 +79,7 @@ class AppenderFileTest < Minitest::Test
|
|
79
79
|
@appender = SemanticLogger::Appender::File.new(@io)
|
80
80
|
SemanticLogger.default_level = :trace
|
81
81
|
@mock_logger = MockLogger.new
|
82
|
-
@appender = SemanticLogger.add_appender(@mock_logger)
|
82
|
+
@appender = SemanticLogger.add_appender(logger: @mock_logger)
|
83
83
|
@hash = {session_id: 'HSSKLEU@JDK767', tracking_number: 12345}
|
84
84
|
@hash_str = @hash.inspect.sub("{", "\\{").sub("}", "\\}")
|
85
85
|
@thread_name = Thread.current.name
|
data/test/logger_test.rb
CHANGED
@@ -3,6 +3,105 @@ require_relative 'test_helper'
|
|
3
3
|
# Unit Test for SemanticLogger::Logger
|
4
4
|
class LoggerTest < Minitest::Test
|
5
5
|
describe SemanticLogger::Logger do
|
6
|
+
describe '.add_appender' do
|
7
|
+
after do
|
8
|
+
SemanticLogger.remove_appender(@appender) if @appender
|
9
|
+
File.delete('sample.log') if File.exists?('sample.log')
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'adds file appender' do
|
13
|
+
@appender = SemanticLogger.add_appender(file_name: 'sample.log')
|
14
|
+
assert @appender.is_a?(SemanticLogger::Appender::File)
|
15
|
+
assert SemanticLogger.appenders.include?(@appender)
|
16
|
+
assert @appender.formatter.is_a?(SemanticLogger::Formatters::Default)
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'adds file appender with json format' do
|
20
|
+
@appender = SemanticLogger.add_appender(file_name: 'sample.log', formatter: :json)
|
21
|
+
assert @appender.is_a?(SemanticLogger::Appender::File)
|
22
|
+
assert SemanticLogger.appenders.include?(@appender)
|
23
|
+
assert @appender.formatter.is_a?(SemanticLogger::Formatters::Json)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'adds stream appender' do
|
27
|
+
@appender = SemanticLogger.add_appender(io: STDOUT)
|
28
|
+
assert @appender.is_a?(SemanticLogger::Appender::File)
|
29
|
+
assert SemanticLogger.appenders.include?(@appender)
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'adds symbol appender' do
|
33
|
+
@appender = SemanticLogger.add_appender(appender: :http, url: 'http://localhost:8088/path')
|
34
|
+
assert @appender.is_a?(SemanticLogger::Appender::Http), @appender.ai
|
35
|
+
assert SemanticLogger.appenders.include?(@appender)
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'adds logger wrapper appender' do
|
39
|
+
@appender = SemanticLogger.add_appender(logger: ::Logger.new(STDOUT))
|
40
|
+
assert @appender.is_a?(SemanticLogger::Appender::Wrapper)
|
41
|
+
assert @appender.logger.is_a?(::Logger)
|
42
|
+
assert SemanticLogger.appenders.include?(@appender)
|
43
|
+
assert @appender.formatter.is_a?(SemanticLogger::Formatters::Default)
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'adds logger wrapper appender with color formatter' do
|
47
|
+
@appender = SemanticLogger.add_appender(logger: ::Logger.new(STDOUT), formatter: :color)
|
48
|
+
assert @appender.is_a?(SemanticLogger::Appender::Wrapper)
|
49
|
+
assert @appender.logger.is_a?(::Logger)
|
50
|
+
assert SemanticLogger.appenders.include?(@appender)
|
51
|
+
assert @appender.formatter.is_a?(SemanticLogger::Formatters::Color)
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'adds appender' do
|
55
|
+
@appender = SemanticLogger.add_appender(appender: SemanticLogger::Appender::Http.new(url: 'http://localhost:8088/path'))
|
56
|
+
assert @appender.is_a?(SemanticLogger::Appender::Http), @appender.ai
|
57
|
+
assert SemanticLogger.appenders.include?(@appender)
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'fails to add invalid logger appender' do
|
61
|
+
assert_raises do
|
62
|
+
SemanticLogger.add_appender(logger: 'blah')
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe '.add_appender DEPRECATED' do
|
68
|
+
after do
|
69
|
+
SemanticLogger.remove_appender(@appender) if @appender
|
70
|
+
File.delete('sample.log') if File.exists?('sample.log')
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'adds file appender' do
|
74
|
+
@appender = SemanticLogger.add_appender('sample.log')
|
75
|
+
assert @appender.is_a?(SemanticLogger::Appender::File)
|
76
|
+
assert SemanticLogger.appenders.include?(@appender)
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'adds stream appender' do
|
80
|
+
@appender = SemanticLogger.add_appender(STDOUT)
|
81
|
+
assert @appender.is_a?(SemanticLogger::Appender::File)
|
82
|
+
assert SemanticLogger.appenders.include?(@appender)
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'adds appender' do
|
86
|
+
@appender = SemanticLogger.add_appender(SemanticLogger::Appender::Http.new(url: 'http://localhost:8088/path'))
|
87
|
+
assert @appender.is_a?(SemanticLogger::Appender::Http), @appender.ai
|
88
|
+
assert SemanticLogger.appenders.include?(@appender)
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'adds logger wrapper appender' do
|
92
|
+
@appender = SemanticLogger.add_appender(::Logger.new(STDOUT))
|
93
|
+
assert @appender.is_a?(SemanticLogger::Appender::Wrapper)
|
94
|
+
assert @appender.logger.is_a?(::Logger)
|
95
|
+
assert SemanticLogger.appenders.include?(@appender)
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'fails to add invalid logger appender' do
|
99
|
+
assert_raises do
|
100
|
+
SemanticLogger.add_appender(logger: 'blah')
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
6
105
|
# Test each filter
|
7
106
|
[nil, /\ALogger/, Proc.new { |l| (/\AExclude/ =~ l.message).nil? }].each do |filter|
|
8
107
|
describe "filter: #{filter.class.name}" do
|
@@ -12,13 +111,13 @@ class LoggerTest < Minitest::Test
|
|
12
111
|
SemanticLogger.default_level = :trace
|
13
112
|
SemanticLogger.backtrace_level = nil
|
14
113
|
@mock_logger = MockLogger.new
|
15
|
-
@appender = SemanticLogger.add_appender(@mock_logger)
|
114
|
+
@appender = SemanticLogger.add_appender(logger: @mock_logger)
|
16
115
|
@appender.filter = filter
|
17
116
|
|
18
117
|
# Add mock metric subscriber
|
19
118
|
$last_metric = nil
|
20
|
-
SemanticLogger.on_metric do |
|
21
|
-
$last_metric =
|
119
|
+
SemanticLogger.on_metric do |log|
|
120
|
+
$last_metric = log.dup
|
22
121
|
end
|
23
122
|
|
24
123
|
# Use this test's class name as the application name in the log output
|
@@ -96,6 +195,66 @@ class LoggerTest < Minitest::Test
|
|
96
195
|
assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ #{level_char} \[\d+:#{@thread_name}\] LoggerTest -- Hello world/, @mock_logger.message
|
97
196
|
end
|
98
197
|
|
198
|
+
describe 'hash only argument' do
|
199
|
+
it 'logs message' do
|
200
|
+
@logger.send(level, message: 'Hello world')
|
201
|
+
SemanticLogger.flush
|
202
|
+
assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ #{level_char} \[\d+:#{@thread_name}\] LoggerTest -- Hello world/, @mock_logger.message
|
203
|
+
end
|
204
|
+
|
205
|
+
it 'logs payload and message' do
|
206
|
+
@logger.send(level, message: 'Hello world', tracking_number: '123456', even: 2, more: 'data')
|
207
|
+
hash = {tracking_number: '123456', even: 2, more: 'data'}
|
208
|
+
SemanticLogger.flush
|
209
|
+
hash_str = hash.inspect.sub('{', '\{').sub('}', '\}')
|
210
|
+
assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ #{level_char} \[\d+:#{@thread_name}\] LoggerTest -- Hello world -- #{hash_str}/, @mock_logger.message
|
211
|
+
end
|
212
|
+
|
213
|
+
it 'logs payload and message from block' do
|
214
|
+
@logger.send(level) { {message: 'Hello world', tracking_number: '123456', even: 2, more: 'data'} }
|
215
|
+
hash = {tracking_number: '123456', even: 2, more: 'data'}
|
216
|
+
SemanticLogger.flush
|
217
|
+
hash_str = hash.inspect.sub('{', '\{').sub('}', '\}')
|
218
|
+
assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ #{level_char} \[\d+:#{@thread_name}\] LoggerTest -- Hello world -- #{hash_str}/, @mock_logger.message
|
219
|
+
end
|
220
|
+
|
221
|
+
it 'logs payload only' do
|
222
|
+
hash = {tracking_number: '123456', even: 2, more: 'data'}
|
223
|
+
@logger.send(level, hash)
|
224
|
+
SemanticLogger.flush
|
225
|
+
hash_str = hash.inspect.sub('{', '\{').sub('}', '\}')
|
226
|
+
assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ #{level_char} \[\d+:#{@thread_name}\] LoggerTest -- #{hash_str}/, @mock_logger.message
|
227
|
+
end
|
228
|
+
|
229
|
+
it 'logs duration' do
|
230
|
+
@logger.send(level, duration: 123.45, message: 'Hello world', tracking_number: '123456', even: 2, more: 'data')
|
231
|
+
hash = {tracking_number: '123456', even: 2, more: 'data'}
|
232
|
+
SemanticLogger.flush
|
233
|
+
hash_str = hash.inspect.sub('{', '\{').sub('}', '\}')
|
234
|
+
duration_match = defined?(JRuby) ? '\(123ms\)' : '\(123\.5ms\)'
|
235
|
+
assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ #{level_char} \[\d+:#{@thread_name}\] #{duration_match} LoggerTest -- Hello world -- #{hash_str}/, @mock_logger.message
|
236
|
+
end
|
237
|
+
|
238
|
+
it 'does not log when below min_duration' do
|
239
|
+
@logger.send(level, min_duration: 200, duration: 123.45, message: 'Hello world', tracking_number: '123456', even: 2, more: 'data')
|
240
|
+
hash = {tracking_number: '123456', even: 2, more: 'data'}
|
241
|
+
SemanticLogger.flush
|
242
|
+
assert_nil @mock_logger.message
|
243
|
+
end
|
244
|
+
|
245
|
+
it 'logs metric' do
|
246
|
+
metric_name = '/my/custom/metric'
|
247
|
+
@logger.send(level, metric: metric_name, duration: 123.45, message: 'Hello world', tracking_number: '123456', even: 2, more: 'data')
|
248
|
+
hash = {tracking_number: '123456', even: 2, more: 'data'}
|
249
|
+
SemanticLogger.flush
|
250
|
+
hash_str = hash.inspect.sub('{', '\{').sub('}', '\}')
|
251
|
+
duration_match = defined?(JRuby) ? '\(123ms\)' : '\(123\.5ms\)'
|
252
|
+
assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ #{level_char} \[\d+:#{@thread_name}\] #{duration_match} LoggerTest -- Hello world -- #{hash_str}/, @mock_logger.message
|
253
|
+
assert metric_name, $last_metric.metric
|
254
|
+
end
|
255
|
+
|
256
|
+
end
|
257
|
+
|
99
258
|
end
|
100
259
|
end
|
101
260
|
|
@@ -160,39 +319,39 @@ class LoggerTest < Minitest::Test
|
|
160
319
|
end
|
161
320
|
end
|
162
321
|
|
163
|
-
describe '
|
164
|
-
# Ensure that any log level can be
|
322
|
+
describe 'measure' do
|
323
|
+
# Ensure that any log level can be measured and logged
|
165
324
|
SemanticLogger::LEVELS.each do |level|
|
166
325
|
level_char = level.to_s.upcase[0]
|
167
326
|
|
168
327
|
describe 'direct method' do
|
169
328
|
it "log #{level} info" do
|
170
|
-
assert_equal 'result', @logger.send("
|
329
|
+
assert_equal 'result', @logger.send("measure_#{level}".to_sym, 'hello world') { 'result' } # Measure duration of the supplied block
|
171
330
|
SemanticLogger.flush
|
172
331
|
assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ #{level_char} \[\d+:#{@thread_name}\] \((\d+\.\d+)|(\d+)ms\) LoggerTest -- hello world/, @mock_logger.message
|
173
332
|
end
|
174
333
|
|
175
334
|
it "log #{level} info with payload" do
|
176
|
-
assert_equal 'result', @logger.send("
|
335
|
+
assert_equal 'result', @logger.send("measure_#{level}".to_sym, 'hello world', payload: @hash) { 'result' } # Measure duration of the supplied block
|
177
336
|
SemanticLogger.flush
|
178
337
|
assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ #{level_char} \[\d+:#{@thread_name}\] \((\d+\.\d+)|(\d+)ms\) LoggerTest -- hello world -- #{@hash_str}/, @mock_logger.message
|
179
338
|
end
|
180
339
|
|
181
340
|
it "not log #{level} info when block is faster than :min_duration" do
|
182
|
-
assert_equal 'result', @logger.send("
|
341
|
+
assert_equal 'result', @logger.send("measure_#{level}".to_sym, 'hello world', min_duration: 500) { 'result' } # Measure duration of the supplied block
|
183
342
|
SemanticLogger.flush
|
184
343
|
assert_nil @mock_logger.message
|
185
344
|
end
|
186
345
|
|
187
346
|
it "log #{level} info when block duration exceeds :min_duration" do
|
188
|
-
assert_equal 'result', @logger.send("
|
347
|
+
assert_equal 'result', @logger.send("measure_#{level}".to_sym, 'hello world', min_duration: 200, payload: @hash) { sleep 0.5; 'result' } # Measure duration of the supplied block
|
189
348
|
SemanticLogger.flush
|
190
349
|
assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ #{level_char} \[\d+:#{@thread_name}\] \((\d+\.\d+)|(\d+)ms\) LoggerTest -- hello world -- #{@hash_str}/, @mock_logger.message
|
191
350
|
end
|
192
351
|
|
193
352
|
it "log #{level} info with an exception" do
|
194
353
|
assert_raises RuntimeError do
|
195
|
-
@logger.send("
|
354
|
+
@logger.send("measure_#{level}", 'hello world', payload: @hash) { raise RuntimeError.new("Test") } # Measure duration of the supplied block
|
196
355
|
end
|
197
356
|
SemanticLogger.flush
|
198
357
|
assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ #{level_char} \[\d+:#{@thread_name}#{@file_name_reg_exp}\] \((\d+\.\d+)|(\d+)ms\) LoggerTest -- hello world -- Exception: RuntimeError: Test -- #{@hash_str}/, @mock_logger.message
|
@@ -200,7 +359,7 @@ class LoggerTest < Minitest::Test
|
|
200
359
|
|
201
360
|
it "change log #{level} info with an exception" do
|
202
361
|
assert_raises RuntimeError do
|
203
|
-
@logger.send("
|
362
|
+
@logger.send("measure_#{level}", 'hello world', payload: @hash, on_exception_level: :fatal) { raise RuntimeError.new("Test") } # Measure duration of the supplied block
|
204
363
|
end
|
205
364
|
SemanticLogger.flush
|
206
365
|
assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ F \[\d+:#{@thread_name}#{@file_name_reg_exp}\] \((\d+\.\d+)|(\d+)ms\) LoggerTest -- hello world -- Exception: RuntimeError: Test -- #{@hash_str}/, @mock_logger.message
|
@@ -208,7 +367,7 @@ class LoggerTest < Minitest::Test
|
|
208
367
|
|
209
368
|
it "log #{level} info with metric" do
|
210
369
|
metric_name = '/my/custom/metric'
|
211
|
-
assert_equal 'result', @logger.send("
|
370
|
+
assert_equal 'result', @logger.send("measure_#{level}".to_sym, 'hello world', metric: metric_name) { 'result' } # Measure duration of the supplied block
|
212
371
|
SemanticLogger.flush
|
213
372
|
assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ #{level_char} \[\d+:#{@thread_name}\] \((\d+\.\d+)|(\d+)ms\) LoggerTest -- hello world/, @mock_logger.message
|
214
373
|
assert metric_name, $last_metric.metric
|
@@ -216,7 +375,7 @@ class LoggerTest < Minitest::Test
|
|
216
375
|
|
217
376
|
it "log #{level} info with backtrace" do
|
218
377
|
SemanticLogger.stub(:backtrace_level_index, 0) do
|
219
|
-
assert_equal 'result', @logger.send("
|
378
|
+
assert_equal 'result', @logger.send("measure_#{level}".to_sym, 'hello world') { 'result' }
|
220
379
|
SemanticLogger.flush
|
221
380
|
assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ #{level_char} \[\d+:#{@thread_name}#{@file_name_reg_exp}\] \((\d+\.\d+)|(\d+)ms\) LoggerTest -- hello world/, @mock_logger.message
|
222
381
|
end
|
@@ -225,32 +384,32 @@ class LoggerTest < Minitest::Test
|
|
225
384
|
|
226
385
|
describe 'generic method' do
|
227
386
|
it "log #{level} info" do
|
228
|
-
assert_equal 'result', @logger.
|
387
|
+
assert_equal 'result', @logger.measure(level, 'hello world') { 'result' } # Measure duration of the supplied block
|
229
388
|
SemanticLogger.flush
|
230
389
|
assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ #{level_char} \[\d+:#{@thread_name}\] \((\d+\.\d+)|(\d+)ms\) LoggerTest -- hello world/, @mock_logger.message
|
231
390
|
end
|
232
391
|
|
233
392
|
it "log #{level} info with payload" do
|
234
|
-
assert_equal 'result', @logger.
|
393
|
+
assert_equal 'result', @logger.measure(level, 'hello world', payload: @hash) { 'result' } # Measure duration of the supplied block
|
235
394
|
SemanticLogger.flush
|
236
395
|
assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ #{level_char} \[\d+:#{@thread_name}\] \((\d+\.\d+)|(\d+)ms\) LoggerTest -- hello world -- #{@hash_str}/, @mock_logger.message
|
237
396
|
end
|
238
397
|
|
239
398
|
it "not log #{level} info when block is faster than :min_duration" do
|
240
|
-
assert_equal 'result', @logger.
|
399
|
+
assert_equal 'result', @logger.measure(level, 'hello world', min_duration: 500) { 'result' } # Measure duration of the supplied block
|
241
400
|
SemanticLogger.flush
|
242
401
|
assert_nil @mock_logger.message
|
243
402
|
end
|
244
403
|
|
245
404
|
it "log #{level} info when block duration exceeds :min_duration" do
|
246
|
-
assert_equal 'result', @logger.
|
405
|
+
assert_equal 'result', @logger.measure(level, 'hello world', min_duration: 200, payload: @hash) { sleep 0.5; 'result' } # Measure duration of the supplied block
|
247
406
|
SemanticLogger.flush
|
248
407
|
assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ #{level_char} \[\d+:#{@thread_name}\] \((\d+\.\d+)|(\d+)ms\) LoggerTest -- hello world -- #{@hash_str}/, @mock_logger.message
|
249
408
|
end
|
250
409
|
|
251
410
|
it "log #{level} info with an exception" do
|
252
411
|
assert_raises RuntimeError do
|
253
|
-
@logger.
|
412
|
+
@logger.measure(level, 'hello world', payload: @hash) { raise RuntimeError.new('Test') } # Measure duration of the supplied block
|
254
413
|
end
|
255
414
|
SemanticLogger.flush
|
256
415
|
assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ #{level_char} \[\d+:#{@thread_name}#{@file_name_reg_exp}\] \((\d+\.\d+)|(\d+)ms\) LoggerTest -- hello world -- Exception: RuntimeError: Test -- #{@hash_str}/, @mock_logger.message
|
@@ -258,7 +417,7 @@ class LoggerTest < Minitest::Test
|
|
258
417
|
|
259
418
|
it "log #{level} info with metric" do
|
260
419
|
metric_name = '/my/custom/metric'
|
261
|
-
assert_equal 'result', @logger.
|
420
|
+
assert_equal 'result', @logger.measure(level, 'hello world', metric: metric_name) { 'result' } # Measure duration of the supplied block
|
262
421
|
SemanticLogger.flush
|
263
422
|
assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ #{level_char} \[\d+:#{@thread_name}\] \((\d+\.\d+)|(\d+)ms\) LoggerTest -- hello world/, @mock_logger.message
|
264
423
|
assert metric_name, $last_metric.metric
|
@@ -266,7 +425,7 @@ class LoggerTest < Minitest::Test
|
|
266
425
|
|
267
426
|
it "log #{level} info with backtrace" do
|
268
427
|
SemanticLogger.stub(:backtrace_level_index, 0) do
|
269
|
-
assert_equal 'result', @logger.
|
428
|
+
assert_equal 'result', @logger.measure(level, 'hello world') { 'result' }
|
270
429
|
SemanticLogger.flush
|
271
430
|
assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ #{level_char} \[\d+:#{@thread_name}#{@file_name_reg_exp}\] \((\d+\.\d+)|(\d+)ms\) LoggerTest -- hello world/, @mock_logger.message
|
272
431
|
end
|
@@ -282,7 +441,7 @@ class LoggerTest < Minitest::Test
|
|
282
441
|
|
283
442
|
it 'not log at a level below the silence level' do
|
284
443
|
SemanticLogger.default_level = :info
|
285
|
-
@logger.
|
444
|
+
@logger.measure_info('hello world', silence: :error) do
|
286
445
|
@logger.warn "don't log me"
|
287
446
|
end
|
288
447
|
SemanticLogger.flush
|
@@ -292,7 +451,7 @@ class LoggerTest < Minitest::Test
|
|
292
451
|
it 'log at a silence level below the default level' do
|
293
452
|
SemanticLogger.default_level = :info
|
294
453
|
first_message = nil
|
295
|
-
@logger.
|
454
|
+
@logger.measure_info('hello world', silence: :trace) do
|
296
455
|
@logger.debug('hello world', @hash) { 'Calculations' }
|
297
456
|
SemanticLogger.flush
|
298
457
|
first_message = @mock_logger.message
|
@@ -415,10 +574,10 @@ class LoggerTest < Minitest::Test
|
|
415
574
|
end
|
416
575
|
end
|
417
576
|
|
418
|
-
# Make sure that
|
577
|
+
# Make sure that measure still logs when a block uses return to return from
|
419
578
|
# a function
|
420
579
|
def function_with_return(logger)
|
421
|
-
logger.
|
580
|
+
logger.measure_info('hello world', payload: @hash) do
|
422
581
|
return 'Good'
|
423
582
|
end
|
424
583
|
'Bad'
|