semantic_logger 2.16.0 → 2.17.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 +15 -1
- data/lib/semantic_logger/appender/base.rb +22 -6
- data/lib/semantic_logger/appender/bugsnag.rb +22 -9
- data/lib/semantic_logger/appender/mongodb.rb +17 -5
- data/lib/semantic_logger/appender/new_relic.rb +12 -14
- data/lib/semantic_logger/base.rb +38 -6
- data/lib/semantic_logger/semantic_logger.rb +31 -2
- data/lib/semantic_logger/version.rb +1 -1
- data/test/appender/bugsnag_test.rb +65 -0
- data/test/appender/file_test.rb +79 -0
- data/test/appender/mongodb_test.rb +105 -0
- data/test/appender/new_relic_test.rb +64 -0
- data/test/{newrelic_rpm.rb → appender/newrelic_rpm.rb} +0 -0
- data/test/appender/splunk_test.rb +50 -0
- data/test/appender/syslog_test.rb +63 -0
- data/test/{appender_wrapper_test.rb → appender/wrapper_test.rb} +10 -8
- data/test/loggable_test.rb +21 -11
- data/test/logger_test.rb +97 -69
- data/test/test_helper.rb +2 -1
- metadata +18 -18
- data/test/appender_bugsnag_test.rb +0 -60
- data/test/appender_file_test.rb +0 -77
- data/test/appender_mongodb_test.rb +0 -103
- data/test/appender_new_relic_test.rb +0 -83
- data/test/appender_splunk_test.rb +0 -47
- data/test/appender_syslog_test.rb +0 -61
@@ -1,60 +0,0 @@
|
|
1
|
-
require_relative 'test_helper'
|
2
|
-
|
3
|
-
# Unit Test for SemanticLogger::Appender::Bugsnag
|
4
|
-
#
|
5
|
-
class AppenderBugsnagTest < Minitest::Test
|
6
|
-
describe SemanticLogger::Appender::Bugsnag do
|
7
|
-
before do
|
8
|
-
@appender = SemanticLogger::Appender::Bugsnag.new(:warn)
|
9
|
-
@message = 'AppenderBugsnagTest log message'
|
10
|
-
end
|
11
|
-
|
12
|
-
(SemanticLogger::LEVELS - [:warn, :error]).each do |level|
|
13
|
-
it "not send :#{level} notifications to Bugsnag" do
|
14
|
-
message = hash = nil
|
15
|
-
Bugsnag.stub(:notify, -> msg, h { message = msg; hash = h }) do
|
16
|
-
@appender.send(level, "AppenderBugsnagTest #{level.to_s} message")
|
17
|
-
end
|
18
|
-
assert_nil message
|
19
|
-
assert_nil hash
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
it "send error notifications to Bugsnag with severity" do
|
24
|
-
message = hash = nil
|
25
|
-
Bugsnag.stub(:notify, -> msg, h { message = msg; hash = h }) do
|
26
|
-
@appender.error @message
|
27
|
-
end
|
28
|
-
assert_equal RuntimeError.new(@message), message
|
29
|
-
assert_equal 'error', hash[:severity]
|
30
|
-
end
|
31
|
-
|
32
|
-
it 'send warn notifications to Bugsnag replace warn severity with warning' do
|
33
|
-
message = hash = nil
|
34
|
-
Bugsnag.stub(:notify, -> msg, h { message = msg; hash = h }) do
|
35
|
-
@appender.warn @message
|
36
|
-
end
|
37
|
-
assert_equal RuntimeError.new(@message), message
|
38
|
-
assert_equal 'warning', hash[:severity]
|
39
|
-
end
|
40
|
-
|
41
|
-
it 'send notification to Bugsnag with custom attributes' do
|
42
|
-
message = hash = nil
|
43
|
-
Bugsnag.stub(:notify, -> msg, h { message = msg; hash = h }) do
|
44
|
-
@appender.error @message, {key1: 1, key2: 'a'}
|
45
|
-
end
|
46
|
-
assert_equal RuntimeError.new(@message), message
|
47
|
-
assert_equal(1, hash[:key1], hash)
|
48
|
-
assert_equal('a', hash[:key2], hash)
|
49
|
-
end
|
50
|
-
|
51
|
-
it 'send notification to Bugsnag with exception' do
|
52
|
-
message = hash = nil
|
53
|
-
error = RuntimeError.new('Hello World')
|
54
|
-
Bugsnag.stub(:notify, -> msg, h { message = msg; hash = h }) do
|
55
|
-
@appender.error error
|
56
|
-
end
|
57
|
-
assert_equal error, message
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
data/test/appender_file_test.rb
DELETED
@@ -1,77 +0,0 @@
|
|
1
|
-
require_relative 'test_helper'
|
2
|
-
require 'stringio'
|
3
|
-
|
4
|
-
# Unit Test for SemanticLogger::Appender::File
|
5
|
-
#
|
6
|
-
class AppenderFileTest < Minitest::Test
|
7
|
-
describe SemanticLogger::Appender::File do
|
8
|
-
before do
|
9
|
-
SemanticLogger.default_level = :trace
|
10
|
-
@time = Time.new
|
11
|
-
@io = StringIO.new
|
12
|
-
@appender = SemanticLogger::Appender::File.new(@io)
|
13
|
-
@hash = {session_id: 'HSSKLEU@JDK767', tracking_number: 12345}
|
14
|
-
@hash_str = @hash.inspect.sub("{", "\\{").sub("}", "\\}")
|
15
|
-
@thread_name = Thread.current.name
|
16
|
-
end
|
17
|
-
|
18
|
-
describe "format logs into text form" do
|
19
|
-
it "handle no message or payload" do
|
20
|
-
@appender.debug
|
21
|
-
assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ D \[\d+:#{@thread_name}\] SemanticLogger::Appender::File -- \n/, @io.string
|
22
|
-
end
|
23
|
-
|
24
|
-
it "handle message" do
|
25
|
-
@appender.debug 'hello world'
|
26
|
-
assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ D \[\d+:#{@thread_name}\] SemanticLogger::Appender::File -- hello world\n/, @io.string
|
27
|
-
end
|
28
|
-
|
29
|
-
it "handle message and payload" do
|
30
|
-
@appender.debug 'hello world', @hash
|
31
|
-
assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ D \[\d+:#{@thread_name}\] SemanticLogger::Appender::File -- hello world -- #{@hash_str}\n/, @io.string
|
32
|
-
end
|
33
|
-
|
34
|
-
it "handle message, payload, and exception" do
|
35
|
-
@appender.debug 'hello world', @hash, StandardError.new("StandardError")
|
36
|
-
assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ D \[\d+:#{@thread_name}\] SemanticLogger::Appender::File -- hello world -- #{@hash_str} -- Exception: StandardError: StandardError\n\n/, @io.string
|
37
|
-
end
|
38
|
-
|
39
|
-
it "handle exception only" do
|
40
|
-
@appender.debug StandardError.new("StandardError")
|
41
|
-
assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ D \[\d+:#{@thread_name}\] SemanticLogger::Appender::File -- #<StandardError: StandardError> -- Exception: StandardError: StandardError\n\n/, @io.string
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
describe "for each log level" do
|
46
|
-
# Ensure that any log level can be logged
|
47
|
-
SemanticLogger::LEVELS.each do |level|
|
48
|
-
it "log #{level} information" do
|
49
|
-
@appender.send(level, 'hello world', @hash)
|
50
|
-
assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ \w \[\d+:#{@thread_name}\] SemanticLogger::Appender::File -- hello world -- #{@hash_str}\n/, @io.string
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
describe "custom formatter" do
|
56
|
-
before do
|
57
|
-
@appender = SemanticLogger::Appender::File.new(@io) do |log|
|
58
|
-
tags = log.tags.collect { |tag| "[#{tag}]" }.join(" ") + " " if log.tags && (log.tags.size > 0)
|
59
|
-
|
60
|
-
message = log.message.to_s
|
61
|
-
message << " -- " << log.payload.inspect if log.payload
|
62
|
-
message << " -- " << "#{log.exception.class}: #{log.exception.message}\n#{(log.exception.backtrace || []).join("\n")}" if log.exception
|
63
|
-
|
64
|
-
duration_str = log.duration ? " (#{'%.1f' % log.duration}ms)" : ''
|
65
|
-
|
66
|
-
"#{SemanticLogger::Appender::Base.formatted_time(log.time)} #{log.level.to_s.upcase} [#{$$}:#{log.thread_name}] #{tags}#{log.name} -- #{message}#{duration_str}"
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
it "format using formatter" do
|
71
|
-
@appender.debug
|
72
|
-
assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ DEBUG \[\d+:#{@thread_name}\] SemanticLogger::Appender::File -- \n/, @io.string
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
end
|
77
|
-
end
|
@@ -1,103 +0,0 @@
|
|
1
|
-
require_relative 'test_helper'
|
2
|
-
|
3
|
-
# Unit Test for SemanticLogger::Appender::MongoDB
|
4
|
-
#
|
5
|
-
class AppenderMongoDBTest < Minitest::Test
|
6
|
-
describe SemanticLogger::Appender::MongoDB do
|
7
|
-
before do
|
8
|
-
@db = Mongo::Connection.new['test']
|
9
|
-
@appender = SemanticLogger::Appender::MongoDB.new(
|
10
|
-
db: @db,
|
11
|
-
collection_size: 10*1024**2, # 10MB
|
12
|
-
host_name: 'test',
|
13
|
-
application: 'test_application',
|
14
|
-
level: :trace
|
15
|
-
)
|
16
|
-
@hash = {tracking_number: 12345, session_id: 'HSSKLEU@JDK767'}
|
17
|
-
Thread.current.name = 'thread'
|
18
|
-
end
|
19
|
-
|
20
|
-
after do
|
21
|
-
@appender.purge_all if @appender
|
22
|
-
end
|
23
|
-
|
24
|
-
describe "format logs into documents" do
|
25
|
-
|
26
|
-
it "handle nil name, message and hash" do
|
27
|
-
@appender.debug
|
28
|
-
document = @appender.collection.find_one
|
29
|
-
assert_equal :debug, document['level']
|
30
|
-
assert_equal nil, document['message']
|
31
|
-
assert_equal 'thread', document['thread_name']
|
32
|
-
assert document['time'].is_a?(Time)
|
33
|
-
assert_equal nil, document['payload']
|
34
|
-
assert_equal $$, document['pid']
|
35
|
-
assert_equal 'test', document['host_name']
|
36
|
-
assert_equal 'test_application', document['application']
|
37
|
-
end
|
38
|
-
|
39
|
-
it "handle nil message and payload" do
|
40
|
-
@appender.debug(@hash)
|
41
|
-
|
42
|
-
document = @appender.collection.find_one
|
43
|
-
assert_equal :debug, document['level']
|
44
|
-
assert_equal @hash.inspect, document['message']
|
45
|
-
assert_equal 'thread', document['thread_name']
|
46
|
-
assert document['time'].is_a?(Time)
|
47
|
-
assert_nil document['payload']
|
48
|
-
assert_equal $$, document['pid']
|
49
|
-
assert_equal 'test', document['host_name']
|
50
|
-
assert_equal 'test_application', document['application']
|
51
|
-
end
|
52
|
-
|
53
|
-
it "handle message and payload" do
|
54
|
-
@appender.debug('hello world', @hash)
|
55
|
-
|
56
|
-
document = @appender.collection.find_one
|
57
|
-
assert_equal :debug, document['level']
|
58
|
-
assert_equal 'hello world', document['message']
|
59
|
-
assert_equal 'thread', document['thread_name']
|
60
|
-
assert document['time'].is_a?(Time)
|
61
|
-
assert_equal({"tracking_number" => 12345, "session_id" => 'HSSKLEU@JDK767'}, document['payload'])
|
62
|
-
assert_equal $$, document['pid']
|
63
|
-
assert_equal 'test', document['host_name']
|
64
|
-
assert_equal 'test_application', document['application']
|
65
|
-
end
|
66
|
-
|
67
|
-
it "handle message without payload" do
|
68
|
-
log = SemanticLogger::Base::Log.new(:debug)
|
69
|
-
@appender.debug('hello world')
|
70
|
-
|
71
|
-
document = @appender.collection.find_one
|
72
|
-
assert_equal :debug, document['level']
|
73
|
-
assert_equal 'hello world', document['message']
|
74
|
-
assert_equal 'thread', document['thread_name']
|
75
|
-
assert document['time'].is_a?(Time)
|
76
|
-
assert_equal nil, document['payload']
|
77
|
-
assert_equal $$, document['pid']
|
78
|
-
assert_equal 'test', document['host_name']
|
79
|
-
assert_equal 'test_application', document['application']
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
describe "for each log level" do
|
84
|
-
# Ensure that any log level can be logged
|
85
|
-
SemanticLogger::LEVELS.each do |level|
|
86
|
-
it "log #{level} information" do
|
87
|
-
@appender.send(level, 'hello world -- Calculations', @hash)
|
88
|
-
document = @appender.collection.find_one
|
89
|
-
assert_equal level, document['level']
|
90
|
-
assert_equal 'hello world -- Calculations', document['message']
|
91
|
-
assert_equal 'thread', document['thread_name']
|
92
|
-
assert document['time'].is_a?(Time)
|
93
|
-
assert_equal({"tracking_number" => 12345, "session_id" => 'HSSKLEU@JDK767'}, document['payload'])
|
94
|
-
assert_equal $$, document['pid']
|
95
|
-
assert_equal 'test', document['host_name']
|
96
|
-
assert_equal 'test_application', document['application']
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
end
|
101
|
-
|
102
|
-
end
|
103
|
-
end
|
@@ -1,83 +0,0 @@
|
|
1
|
-
# So that the NewRelic appender will load the mock
|
2
|
-
$LOAD_PATH.unshift File.dirname(__FILE__)
|
3
|
-
require_relative 'test_helper'
|
4
|
-
|
5
|
-
# Unit Test for SemanticLogger::Appender::NewRelic
|
6
|
-
#
|
7
|
-
class AppenderNewRelicTest < Minitest::Test
|
8
|
-
describe SemanticLogger::Appender::NewRelic do
|
9
|
-
|
10
|
-
before do
|
11
|
-
@appender = SemanticLogger::Appender::NewRelic.new(:error)
|
12
|
-
@message = 'AppenderNewRelicTest log message'
|
13
|
-
@multi_line_message = <<-EOSTR
|
14
|
-
|
15
|
-
|
16
|
-
first non-blank line
|
17
|
-
second non-blank line
|
18
|
-
|
19
|
-
third non-blank line
|
20
|
-
|
21
|
-
EOSTR
|
22
|
-
end
|
23
|
-
|
24
|
-
(SemanticLogger::LEVELS - [:error, :fatal]).each do |level|
|
25
|
-
it "not send :#{level.to_s} notifications to New Relic" do
|
26
|
-
message = hash = nil
|
27
|
-
NewRelic::Agent.stub(:notice_error, -> msg, h { message = msg; hash = h }) do
|
28
|
-
@appender.tagged('test') do
|
29
|
-
@appender.send(level, "AppenderNewRelicTest #{level.to_s} message")
|
30
|
-
end
|
31
|
-
end
|
32
|
-
assert_nil message
|
33
|
-
assert_nil hash
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
[:error, :fatal].each do |level|
|
38
|
-
it "send :#{level.to_s} notifications to New Relic" do
|
39
|
-
message = hash = nil
|
40
|
-
NewRelic::Agent.stub(:notice_error, -> msg, h { message = msg; hash = h }) do
|
41
|
-
@appender.tagged('test') do
|
42
|
-
@appender.send(level, @message)
|
43
|
-
end
|
44
|
-
end
|
45
|
-
assert_equal @message, message
|
46
|
-
assert_equal ['test'], hash[:custom_params][:tags]
|
47
|
-
assert_equal "SemanticLogger::Appender::NewRelic/#{@message}", hash[:metric]
|
48
|
-
assert_nil hash[:custom_params][:duration]
|
49
|
-
assert hash[:custom_params][:thread_name], hash.inspect
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
it 'send notification to New Relic with custom attributes' do
|
54
|
-
message = hash = nil
|
55
|
-
NewRelic::Agent.stub(:notice_error, -> msg, h { message = msg; hash = h }) do
|
56
|
-
@appender.tagged('test') do
|
57
|
-
@appender.with_payload({key1: 1, key2: 'a'}) do
|
58
|
-
@appender.benchmark(:error, @message) do
|
59
|
-
sleep 0.001
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
assert_equal @message, message
|
65
|
-
assert_equal ['test'], hash[:custom_params][:tags], hash
|
66
|
-
assert_equal "SemanticLogger::Appender::NewRelic/#{@message}", hash[:metric]
|
67
|
-
assert_equal({key1: 1, key2: 'a'}, hash[:custom_params][:payload], hash)
|
68
|
-
assert hash[:custom_params][:duration], hash
|
69
|
-
assert hash[:custom_params][:thread_name], hash
|
70
|
-
end
|
71
|
-
|
72
|
-
it 'use the first non-blank line for a multi-line message' do
|
73
|
-
message = hash = nil
|
74
|
-
NewRelic::Agent.stub(:notice_error, -> msg, h { message = msg; hash = h }) do
|
75
|
-
@appender.tagged('test') do
|
76
|
-
@appender.error @multi_line_message
|
77
|
-
end
|
78
|
-
end
|
79
|
-
assert_equal 'first non-blank line', message
|
80
|
-
end
|
81
|
-
|
82
|
-
end
|
83
|
-
end
|
@@ -1,47 +0,0 @@
|
|
1
|
-
require_relative 'test_helper'
|
2
|
-
|
3
|
-
# Unit Test for SemanticLogger::Appender::Splunk
|
4
|
-
#
|
5
|
-
class AppenderSplunkTest < Minitest::Test
|
6
|
-
describe SemanticLogger::Appender::Splunk do
|
7
|
-
|
8
|
-
describe '#parse_options' do
|
9
|
-
describe 'argument errors' do
|
10
|
-
it 'raise argument error for missing username' do
|
11
|
-
error = assert_raises ArgumentError do
|
12
|
-
SemanticLogger::Appender::Splunk.new({})
|
13
|
-
end
|
14
|
-
|
15
|
-
assert_equal 'Must supply a username.', error.message
|
16
|
-
end
|
17
|
-
|
18
|
-
it 'raise argument error for missing password' do
|
19
|
-
error = assert_raises ArgumentError do
|
20
|
-
SemanticLogger::Appender::Splunk.new(username: 'username')
|
21
|
-
end
|
22
|
-
|
23
|
-
assert_equal 'Must supply a password.', error.message
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
describe 'set default values' do
|
28
|
-
it 'have default values' do
|
29
|
-
appender = Splunk.stub(:connect, Splunk::Service.new({})) do
|
30
|
-
Splunk::Service.stub_any_instance(:indexes, {}) do
|
31
|
-
SemanticLogger::Appender::Splunk.new(username: 'username', password: 'password')
|
32
|
-
end
|
33
|
-
end
|
34
|
-
config = appender.config
|
35
|
-
# Default host
|
36
|
-
assert_equal 'localhost', config[:host]
|
37
|
-
# Default port
|
38
|
-
assert_equal 8089, config[:port]
|
39
|
-
# Default scheme
|
40
|
-
assert_equal :https, config[:scheme]
|
41
|
-
#Default index
|
42
|
-
assert_equal 'main', appender.index
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
@@ -1,61 +0,0 @@
|
|
1
|
-
require_relative 'test_helper'
|
2
|
-
require 'net/tcp_client'
|
3
|
-
|
4
|
-
# Unit Test for SemanticLogger::Appender::Syslog
|
5
|
-
#
|
6
|
-
class AppenderSyslogTest < Minitest::Test
|
7
|
-
describe SemanticLogger::Appender::Syslog do
|
8
|
-
|
9
|
-
it 'handle local syslog' do
|
10
|
-
message = nil
|
11
|
-
Syslog.stub(:open, nil) do
|
12
|
-
Syslog.stub(:log, -> level, msg { message = msg }) do
|
13
|
-
syslog_appender = SemanticLogger::Appender::Syslog.new(level: :debug)
|
14
|
-
syslog_appender.debug 'AppenderSyslogTest log message'
|
15
|
-
end
|
16
|
-
end
|
17
|
-
assert_match /D (.*?) SemanticLogger::Appender::Syslog -- AppenderSyslogTest log message/, message
|
18
|
-
end
|
19
|
-
|
20
|
-
it 'handle remote syslog over TCP' do
|
21
|
-
message = nil
|
22
|
-
Net::TCPClient.stub_any_instance(:closed?, false) do
|
23
|
-
Net::TCPClient.stub_any_instance(:connect, nil) do
|
24
|
-
syslog_appender = SemanticLogger::Appender::Syslog.new(server: 'tcp://localhost:88888', level: :debug)
|
25
|
-
syslog_appender.remote_syslog.stub(:write, Proc.new { |data| message = data }) do
|
26
|
-
syslog_appender.debug 'AppenderSyslogTest log message'
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
assert_match /<70>(.*?)SemanticLogger::Appender::Syslog -- AppenderSyslogTest log message\r\n/, message
|
31
|
-
end
|
32
|
-
|
33
|
-
it 'handle remote syslog over UDP' do
|
34
|
-
message = nil
|
35
|
-
syslog_appender = SemanticLogger::Appender::Syslog.new(server: 'udp://localhost:88888', level: :debug)
|
36
|
-
UDPSocket.stub_any_instance(:send, -> msg, num, host, port { message = msg }) do
|
37
|
-
syslog_appender.debug 'AppenderSyslogTest log message'
|
38
|
-
end
|
39
|
-
assert_match /<70>(.*?)SemanticLogger::Appender::Syslog -- AppenderSyslogTest log message/, message
|
40
|
-
end
|
41
|
-
|
42
|
-
# Should be able to log each level.
|
43
|
-
SemanticLogger::LEVELS.each do |level|
|
44
|
-
it "log #{level} information" do
|
45
|
-
Syslog.stub(:open, nil) do
|
46
|
-
Syslog.stub(:log, nil) do
|
47
|
-
syslog_appender = SemanticLogger::Appender::Syslog.new
|
48
|
-
syslog_appender.send(level, 'AppenderSyslogTest #{level.to_s} message')
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
it "allow logging with %" do
|
55
|
-
message = "AppenderSyslogTest %test"
|
56
|
-
syslog_appender = SemanticLogger::Appender::Syslog.new
|
57
|
-
syslog_appender.debug(message)
|
58
|
-
end
|
59
|
-
|
60
|
-
end
|
61
|
-
end
|