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