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