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.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/lib/semantic_logger.rb +7 -1
  3. data/lib/semantic_logger/appender.rb +3 -0
  4. data/lib/semantic_logger/appender/async.rb +29 -10
  5. data/lib/semantic_logger/appender/rabbitmq.rb +120 -0
  6. data/lib/semantic_logger/appenders.rb +89 -0
  7. data/lib/semantic_logger/base.rb +3 -3
  8. data/lib/semantic_logger/concerns/compatibility.rb +2 -2
  9. data/lib/semantic_logger/formatters.rb +1 -0
  10. data/lib/semantic_logger/formatters/base.rb +28 -6
  11. data/lib/semantic_logger/formatters/color.rb +4 -3
  12. data/lib/semantic_logger/formatters/fluentd.rb +37 -0
  13. data/lib/semantic_logger/formatters/json.rb +4 -2
  14. data/lib/semantic_logger/formatters/raw.rb +2 -2
  15. data/lib/semantic_logger/formatters/signalfx.rb +4 -3
  16. data/lib/semantic_logger/levels.rb +38 -0
  17. data/lib/semantic_logger/log.rb +11 -6
  18. data/lib/semantic_logger/loggable.rb +1 -1
  19. data/lib/semantic_logger/logger.rb +43 -1
  20. data/lib/semantic_logger/processor.rb +10 -130
  21. data/lib/semantic_logger/reporters/minitest.rb +49 -0
  22. data/lib/semantic_logger/semantic_logger.rb +40 -75
  23. data/lib/semantic_logger/version.rb +1 -1
  24. metadata +9 -81
  25. data/test/appender/async_batch_test.rb +0 -60
  26. data/test/appender/async_test.rb +0 -44
  27. data/test/appender/bugsnag_test.rb +0 -81
  28. data/test/appender/elasticsearch_http_test.rb +0 -74
  29. data/test/appender/elasticsearch_test.rb +0 -248
  30. data/test/appender/file_test.rb +0 -120
  31. data/test/appender/graylog_test.rb +0 -82
  32. data/test/appender/honeybadger_test.rb +0 -45
  33. data/test/appender/http_test.rb +0 -63
  34. data/test/appender/kafka_test.rb +0 -35
  35. data/test/appender/mongodb_test.rb +0 -104
  36. data/test/appender/new_relic_test.rb +0 -80
  37. data/test/appender/newrelic_rpm.rb +0 -14
  38. data/test/appender/sentry_test.rb +0 -47
  39. data/test/appender/splunk_http_test.rb +0 -79
  40. data/test/appender/splunk_test.rb +0 -83
  41. data/test/appender/syslog_test.rb +0 -61
  42. data/test/appender/tcp_test.rb +0 -66
  43. data/test/appender/udp_test.rb +0 -59
  44. data/test/appender/wrapper_test.rb +0 -95
  45. data/test/concerns/compatibility_test.rb +0 -117
  46. data/test/debug_as_trace_logger_test.rb +0 -81
  47. data/test/formatters/color_test.rb +0 -153
  48. data/test/formatters/default_test.rb +0 -175
  49. data/test/formatters/one_line_test.rb +0 -60
  50. data/test/formatters/signalfx_test.rb +0 -197
  51. data/test/formatters_test.rb +0 -36
  52. data/test/in_memory_appender.rb +0 -8
  53. data/test/in_memory_appender_helper.rb +0 -43
  54. data/test/in_memory_batch_appender.rb +0 -8
  55. data/test/in_memory_metrics_appender.rb +0 -13
  56. data/test/loggable_test.rb +0 -103
  57. data/test/logger_test.rb +0 -334
  58. data/test/measure_test.rb +0 -346
  59. data/test/metric/new_relic_test.rb +0 -35
  60. data/test/metric/signalfx_test.rb +0 -77
  61. data/test/semantic_logger_test.rb +0 -303
  62. data/test/test_helper.rb +0 -31
@@ -1,117 +0,0 @@
1
- require_relative '../test_helper'
2
-
3
- class TestLogger < Minitest::Test
4
- describe SemanticLogger::Logger do
5
- include InMemoryAppenderHelper
6
-
7
- it '#add' do
8
- logger.add(Logger::INFO, 'hello world', 'progname') { 'Data' }
9
-
10
- assert log = log_message
11
- assert_equal 'hello world -- progname -- Data', log.message
12
- assert_equal :info, log.level
13
- end
14
-
15
- it '#log' do
16
- logger.log(Logger::FATAL, 'hello world', 'progname') { 'Data' }
17
-
18
- assert log = log_message
19
- assert_equal 'hello world -- progname -- Data', log.message
20
- assert_equal :fatal, log.level
21
- end
22
-
23
- it '#unknown' do
24
- logger.unknown('hello world') { 'Data' }
25
-
26
- assert log = log_message
27
- assert_equal 'hello world -- Data', log.message
28
- assert_equal :error, log.level
29
- assert_equal 'TestLogger', log.name
30
- end
31
-
32
- it '#unknown? as error?' do
33
- SemanticLogger.default_level = :error
34
- assert logger.unknown?
35
- logger.log(Logger::UNKNOWN, 'hello world', 'progname') { 'Data' }
36
-
37
- assert log = log_message
38
- assert_equal 'hello world -- progname -- Data', log.message
39
- assert_equal :error, log.level
40
- end
41
-
42
- it '#unknown? as error? when false' do
43
- SemanticLogger.default_level = :fatal
44
- refute logger.unknown?
45
- logger.log(Logger::UNKNOWN, 'hello world', 'progname') { 'Data' }
46
-
47
- refute log_message
48
- end
49
-
50
- it '#silence_logger' do
51
- logger.silence_logger do
52
- logger.info 'hello world'
53
- end
54
- refute log_message
55
- end
56
-
57
- it '#<< as info' do
58
- logger << 'hello world'
59
-
60
- assert log = log_message
61
- assert_equal 'hello world', log.message
62
- assert_equal :info, log.level
63
- end
64
-
65
- it '#progname= as #name=' do
66
- assert_equal 'TestLogger', logger.name
67
- logger.progname = 'NewTest'
68
- assert_equal 'NewTest', logger.name
69
- end
70
-
71
- it '#progname as #name' do
72
- assert_equal 'TestLogger', logger.name
73
- assert_equal 'TestLogger', logger.progname
74
- end
75
-
76
- it '#sev_threshold= as #level=' do
77
- assert_equal :trace, logger.level
78
- logger.sev_threshold = Logger::DEBUG
79
- assert_equal :debug, logger.level
80
- end
81
-
82
- it '#sev_threshold as #level' do
83
- assert_equal :trace, logger.level
84
- assert_equal :trace, logger.sev_threshold
85
- end
86
-
87
- it '#formatter NOOP' do
88
- assert_nil logger.formatter
89
- logger.formatter = 'blah'
90
- assert_equal 'blah', logger.formatter
91
- end
92
-
93
- it '#datetime_format NOOP' do
94
- assert_nil logger.datetime_format
95
- logger.datetime_format = 'blah'
96
- assert_equal 'blah', logger.datetime_format
97
- end
98
-
99
- it '#close NOOP' do
100
- logger.close
101
- logger.info('hello world')
102
-
103
- assert log = log_message
104
- assert_equal 'hello world', log.message
105
- assert_equal :info, log.level
106
- end
107
-
108
- it '#reopen NOOP' do
109
- logger.reopen
110
- logger.info('hello world')
111
-
112
- assert log = log_message
113
- assert_equal 'hello world', log.message
114
- assert_equal :info, log.level
115
- end
116
- end
117
- end
@@ -1,81 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- class DebugAsTraceLoggerTest < Minitest::Test
4
- describe SemanticLogger::Logger do
5
- describe '.level?' do
6
- let :logger do
7
- SemanticLogger::DebugAsTraceLogger.new('TestLogger')
8
- end
9
-
10
- it 'return true for debug? with :trace level' do
11
- SemanticLogger.default_level = :trace
12
- assert_equal :trace, logger.level
13
- assert_equal true, logger.debug?
14
- assert_equal true, logger.trace?
15
- end
16
-
17
- it 'return false for debug? with global :debug level' do
18
- SemanticLogger.default_level = :debug
19
- assert_equal :debug, logger.level, logger
20
- assert logger.info?, logger.inspect
21
- refute logger.debug?, logger.inspect
22
- refute logger.trace?, logger.inspect
23
- end
24
-
25
- it 'return true for debug? with global :info level' do
26
- SemanticLogger.default_level = :info
27
- assert_equal :info, logger.level, logger.inspect
28
- refute logger.debug?, logger.inspect
29
- refute logger.trace?, logger.inspect
30
- end
31
-
32
- it 'return false for debug? with instance :debug level' do
33
- logger.level = :debug
34
- assert_equal :debug, logger.level, logger.inspect
35
- refute logger.debug?, logger.inspect
36
- refute logger.trace?, logger.inspect
37
- end
38
-
39
- it 'return true for debug? with instance :info level' do
40
- logger.level = :info
41
- assert_equal :info, logger.level, logger.inspect
42
- refute logger.debug?, logger.inspect
43
- refute logger.trace?, logger.inspect
44
- end
45
- end
46
-
47
- describe 'log' do
48
- include InMemoryAppenderHelper
49
-
50
- let :logger do
51
- SemanticLogger::DebugAsTraceLogger.new('TestLogger')
52
- end
53
-
54
- it 'not log trace when level is debug' do
55
- logger.level = :debug
56
- logger.trace('hello world', payload) { 'Calculations' }
57
- refute log_message
58
- end
59
-
60
- it 'not log debug when level is debug' do
61
- logger.level = :debug
62
- logger.debug('hello world', payload) { 'Calculations' }
63
- refute log_message
64
- end
65
-
66
- it 'map debug to trace' do
67
- logger.level = :trace
68
- logger.debug('hello world')
69
- assert log = log_message
70
- assert_equal :trace, log.level
71
- end
72
-
73
- it 'log trace as trace' do
74
- logger.level = :trace
75
- logger.trace('hello world', payload) { 'Calculations' }
76
- assert log = log_message
77
- assert_equal :trace, log.level
78
- end
79
- end
80
- end
81
- end
@@ -1,153 +0,0 @@
1
- require_relative '../test_helper'
2
-
3
- module SemanticLogger
4
- module Formatters
5
- class ColorTest < Minitest::Test
6
- describe Color do
7
- let(:log_time) do
8
- Time.utc(2017, 1, 14, 8, 32, 5.375276)
9
- end
10
-
11
- let(:level) do
12
- :debug
13
- end
14
-
15
- let(:log) do
16
- # :level, :thread_name, :name, :message, :payload, :time, :duration, :tags, :level_index, :exception, :metric, :backtrace, :metric_amount, :named_tags
17
- log = SemanticLogger::Log.new('ColorTest', level)
18
- log.time = log_time
19
- log
20
- end
21
-
22
- let(:expected_time) do
23
- SemanticLogger::Formatters::Base::PRECISION == 3 ? '2017-01-14 08:32:05.375' : '2017-01-14 08:32:05.375276'
24
- end
25
-
26
- let(:set_exception) do
27
- begin
28
- raise 'Oh no'
29
- rescue Exception => exc
30
- log.exception = exc
31
- end
32
- end
33
-
34
- let(:backtrace) do
35
- [
36
- "test/formatters/default_test.rb:35:in `block (2 levels) in <class:DefaultTest>'",
37
- "gems/ruby-2.3.3/gems/minitest-5.10.1/lib/minitest/spec.rb:247:in `instance_eval'",
38
- "gems/ruby-2.3.3/gems/minitest-5.10.1/lib/minitest/spec.rb:247:in `block (2 levels) in let'",
39
- "gems/ruby-2.3.3/gems/minitest-5.10.1/lib/minitest/spec.rb:247:in `fetch'",
40
- "ruby-2.3.3/gems/minitest-5.10.1/lib/minitest/spec.rb:247:in `block in let'",
41
- "test/formatters/default_test.rb:65:in `block (3 levels) in <class:DefaultTest>'",
42
- "ruby-2.3.3/gems/minitest-5.10.1/lib/minitest/test.rb:105:in `block (3 levels) in run'"
43
- ]
44
- end
45
-
46
- let(:bold) do
47
- SemanticLogger::AnsiColors::BOLD
48
- end
49
-
50
- let(:clear) do
51
- SemanticLogger::AnsiColors::CLEAR
52
- end
53
-
54
- let(:color) do
55
- SemanticLogger::AnsiColors::GREEN
56
- end
57
-
58
- let(:formatter) do
59
- formatter = SemanticLogger::Formatters::Color.new
60
- # Does not use the logger instance for formatting purposes
61
- formatter.call(log, nil)
62
- formatter
63
- end
64
-
65
- describe 'level' do
66
- it 'logs single character' do
67
- assert_equal "#{color}D#{clear}", formatter.level
68
- end
69
- end
70
-
71
- describe 'tags' do
72
- it 'logs tags' do
73
- log.tags = %w[first second third]
74
- assert_equal "[#{color}first#{clear}] [#{color}second#{clear}] [#{color}third#{clear}]", formatter.tags
75
- end
76
- end
77
-
78
- describe 'named_tags' do
79
- it 'logs named tags' do
80
- log.named_tags = {first: 1, second: 2, third: 3}
81
- assert_equal "{#{color}first: 1#{clear}, #{color}second: 2#{clear}, #{color}third: 3#{clear}}", formatter.named_tags
82
- end
83
- end
84
-
85
- describe 'duration' do
86
- it 'logs long duration' do
87
- log.duration = 1_000_000.34567
88
- assert_equal "(#{bold}16m 40s#{clear})", formatter.duration
89
- end
90
-
91
- it 'logs short duration' do
92
- log.duration = 1.34567
93
- duration = SemanticLogger::Formatters::Base::PRECISION == 3 ? "(#{bold}1ms#{clear})" : "(#{bold}1.346ms#{clear})"
94
- assert_equal duration, formatter.duration
95
- end
96
- end
97
-
98
- describe 'name' do
99
- it 'logs name' do
100
- assert_equal "#{color}ColorTest#{clear}", formatter.name
101
- end
102
- end
103
-
104
- describe 'payload' do
105
- it 'logs hash payload' do
106
- log.payload = {first: 1, second: 2, third: 3}
107
- assert_equal "-- #{log.payload.ai(multiline: false)}", formatter.payload
108
- end
109
-
110
- it 'skips nil payload' do
111
- refute formatter.payload
112
- end
113
-
114
- it 'skips empty payload' do
115
- log.payload = {}
116
- refute formatter.payload
117
- end
118
- end
119
-
120
- describe 'exception' do
121
- it 'logs exception' do
122
- set_exception
123
- str = "-- Exception: #{color}RuntimeError: Oh no#{clear}\n"
124
- assert_equal str, formatter.exception.lines.first
125
- end
126
-
127
- it 'skips nil exception' do
128
- refute formatter.exception
129
- end
130
- end
131
-
132
- describe 'call' do
133
- it 'returns minimal elements' do
134
- assert_equal "#{expected_time} #{color}D#{clear} [#{$$}:#{Thread.current.name}] #{color}ColorTest#{clear}", formatter.call(log, nil)
135
- end
136
-
137
- it 'retuns all elements' do
138
- log.tags = %w[first second third]
139
- log.named_tags = {first: 1, second: 2, third: 3}
140
- log.duration = 1.34567
141
- log.message = 'Hello World'
142
- log.payload = {first: 1, second: 2, third: 3}
143
- log.backtrace = backtrace
144
- set_exception
145
- duration = SemanticLogger::Formatters::Base::PRECISION == 3 ? '1' : '1.346'
146
- str = "#{expected_time} #{color}D#{clear} [#{$$}:#{Thread.current.name} default_test.rb:35] [#{color}first#{clear}] [#{color}second#{clear}] [#{color}third#{clear}] {#{color}first: 1#{clear}, #{color}second: 2#{clear}, #{color}third: 3#{clear}} (#{bold}#{duration}ms#{clear}) #{color}ColorTest#{clear} -- Hello World -- #{{first: 1, second: 2, third: 3}.ai(multiline: false)} -- Exception: #{color}RuntimeError: Oh no#{clear}\n"
147
- assert_equal str, formatter.call(log, nil).lines.first
148
- end
149
- end
150
- end
151
- end
152
- end
153
- end
@@ -1,175 +0,0 @@
1
- require_relative '../test_helper'
2
-
3
- module SemanticLogger
4
- module Formatters
5
- class DefaultTest < Minitest::Test
6
- describe Default do
7
- let(:log_time) do
8
- Time.utc(2017, 1, 14, 8, 32, 5.375276)
9
- end
10
-
11
- let(:level) do
12
- :debug
13
- end
14
-
15
- let(:log) do
16
- # :level, :thread_name, :name, :message, :payload, :time, :duration, :tags, :level_index, :exception, :metric, :backtrace, :metric_amount, :named_tags
17
- log = SemanticLogger::Log.new('DefaultTest', level)
18
- log.time = log_time
19
- log
20
- end
21
-
22
- let(:expected_time) do
23
- SemanticLogger::Formatters::Base::PRECISION == 3 ? '2017-01-14 08:32:05.375' : '2017-01-14 08:32:05.375276'
24
- end
25
-
26
- let(:set_exception) do
27
- begin
28
- raise 'Oh no'
29
- rescue Exception => exc
30
- log.exception = exc
31
- end
32
- end
33
-
34
- let(:backtrace) do
35
- [
36
- "test/formatters/default_test.rb:99:in `block (2 levels) in <class:DefaultTest>'",
37
- "gems/ruby-2.3.3/gems/minitest-5.10.1/lib/minitest/spec.rb:247:in `instance_eval'",
38
- "gems/ruby-2.3.3/gems/minitest-5.10.1/lib/minitest/spec.rb:247:in `block (2 levels) in let'",
39
- "gems/ruby-2.3.3/gems/minitest-5.10.1/lib/minitest/spec.rb:247:in `fetch'",
40
- "ruby-2.3.3/gems/minitest-5.10.1/lib/minitest/spec.rb:247:in `block in let'",
41
- "test/formatters/default_test.rb:65:in `block (3 levels) in <class:DefaultTest>'",
42
- "ruby-2.3.3/gems/minitest-5.10.1/lib/minitest/test.rb:105:in `block (3 levels) in run'"
43
- ]
44
- end
45
-
46
- let(:formatter) do
47
- formatter = SemanticLogger::Formatters::Default.new
48
- # Does not use the logger instance for formatting purposes
49
- formatter.call(log, nil)
50
- formatter
51
- end
52
-
53
- describe 'time' do
54
- it 'logs time' do
55
- assert_equal expected_time, formatter.time
56
- end
57
-
58
- it 'supports time_format' do
59
- formatter = SemanticLogger::Formatters::Default.new(time_format: '%H:%M:%S')
60
- formatter.call(log, nil)
61
- assert_equal '08:32:05', formatter.time
62
- end
63
- end
64
-
65
- describe 'level' do
66
- it 'logs single character' do
67
- assert_equal 'D', formatter.level
68
- end
69
- end
70
-
71
- describe 'process_info' do
72
- it 'logs pid and thread name' do
73
- assert_equal "[#{$$}:#{Thread.current.name}]", formatter.process_info
74
- end
75
-
76
- it 'logs pid, thread name, and file name' do
77
- set_exception
78
- log.backtrace = backtrace
79
- assert_equal "[#{$$}:#{Thread.current.name} default_test.rb:99]", formatter.process_info
80
- end
81
- end
82
-
83
- describe 'tags' do
84
- it 'logs tags' do
85
- log.tags = %w[first second third]
86
- assert_equal '[first] [second] [third]', formatter.tags
87
- end
88
- end
89
-
90
- describe 'named_tags' do
91
- it 'logs named tags' do
92
- log.named_tags = {first: 1, second: 2, third: 3}
93
- assert_equal '{first: 1, second: 2, third: 3}', formatter.named_tags
94
- end
95
- end
96
-
97
- describe 'duration' do
98
- it 'logs long duration' do
99
- log.duration = 1_000_000.34567
100
- assert_equal '(16m 40s)', formatter.duration
101
- end
102
-
103
- it 'logs short duration' do
104
- log.duration = 1.34567
105
- duration = SemanticLogger::Formatters::Base::PRECISION == 3 ? '(1ms)' : '(1.346ms)'
106
- assert_equal duration, formatter.duration
107
- end
108
- end
109
-
110
- describe 'name' do
111
- it 'logs name' do
112
- assert_equal 'DefaultTest', formatter.name
113
- end
114
- end
115
-
116
- describe 'message' do
117
- it 'logs message' do
118
- log.message = 'Hello World'
119
- assert_equal '-- Hello World', formatter.message
120
- end
121
-
122
- it 'skips empty message' do
123
- refute formatter.message
124
- end
125
- end
126
-
127
- describe 'payload' do
128
- it 'logs hash payload' do
129
- log.payload = {first: 1, second: 2, third: 3}
130
- assert_equal '-- {:first=>1, :second=>2, :third=>3}', formatter.payload
131
- end
132
-
133
- it 'skips nil payload' do
134
- refute formatter.payload
135
- end
136
-
137
- it 'skips empty payload' do
138
- log.payload = {}
139
- refute formatter.payload
140
- end
141
- end
142
-
143
- describe 'exception' do
144
- it 'logs exception' do
145
- set_exception
146
- assert_match /-- Exception: RuntimeError: Oh no/, formatter.exception
147
- end
148
-
149
- it 'skips nil exception' do
150
- refute formatter.exception
151
- end
152
- end
153
-
154
- describe 'call' do
155
- it 'returns minimal elements' do
156
- assert_equal "#{expected_time} D [#{$$}:#{Thread.current.name}] DefaultTest", formatter.call(log, nil)
157
- end
158
-
159
- it 'retuns all elements' do
160
- log.tags = %w[first second third]
161
- log.named_tags = {first: 1, second: 2, third: 3}
162
- log.duration = 1.34567
163
- log.message = 'Hello World'
164
- log.payload = {first: 1, second: 2, third: 3}
165
- log.backtrace = backtrace
166
- set_exception
167
- duration = SemanticLogger::Formatters::Base::PRECISION == 3 ? '1' : '1.346'
168
- str = "#{expected_time} D [#{$$}:#{Thread.current.name} default_test.rb:99] [first] [second] [third] {first: 1, second: 2, third: 3} (#{duration}ms) DefaultTest -- Hello World -- {:first=>1, :second=>2, :third=>3} -- Exception: RuntimeError: Oh no\n"
169
- assert_equal str, formatter.call(log, nil).lines.first
170
- end
171
- end
172
- end
173
- end
174
- end
175
- end