ougai 1.5.0 → 1.5.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/ougai/formatters/base.rb +18 -1
- data/lib/ougai/formatters/bunyan.rb +1 -1
- data/lib/ougai/formatters/readable.rb +1 -1
- data/lib/ougai/logger.rb +1 -1
- data/lib/ougai/version.rb +1 -1
- data/spec/formatters/base_spec.rb +18 -0
- data/spec/formatters/bunyan_spec.rb +16 -0
- data/spec/formatters/readable_spec.rb +36 -7
- data/spec/logger_spec.rb +15 -9
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 19d87c28c425031fb5819e07593210f5078ef857
|
4
|
+
data.tar.gz: 4dce59c9d66c03394b471e72715dfbf131cb6720
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aefa5448b0a64c2332ee30f7432fa6c941c929da5393a94ce664b0248723f1c4c7fb5a70e47c0e49864863b8cddba5ef1d5a41ae50032ec304db753fce831d85
|
7
|
+
data.tar.gz: 603ec53229871b4ea8df521a35873a37842a96b3fd8bc7a3a8542a26a8737f5b10f21f22f44eea77bc45031bdcf7b5db74d6a2759d949ab870731f11155f5ab5
|
@@ -1,4 +1,3 @@
|
|
1
|
-
require 'logger'
|
2
1
|
require 'time'
|
3
2
|
require 'socket'
|
4
3
|
|
@@ -13,6 +12,11 @@ module Ougai
|
|
13
12
|
@hostname = hostname || Socket.gethostname.force_encoding('UTF-8')
|
14
13
|
@trace_indent = 2
|
15
14
|
@trace_max_lines = 100
|
15
|
+
self.datetime_format = nil
|
16
|
+
end
|
17
|
+
|
18
|
+
def datetime_format=(value)
|
19
|
+
@datetime_format = value || default_datetime_format
|
16
20
|
end
|
17
21
|
|
18
22
|
def serialize_exc(ex)
|
@@ -30,6 +34,19 @@ module Ougai
|
|
30
34
|
sp = "\n" + ' ' * @trace_indent
|
31
35
|
trace.slice(0, @trace_max_lines).join(sp)
|
32
36
|
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def format_datetime(time)
|
41
|
+
time.strftime(@datetime_format)
|
42
|
+
end
|
43
|
+
|
44
|
+
def default_datetime_format
|
45
|
+
t = Time.new
|
46
|
+
f = '%FT%T.%3N'
|
47
|
+
f << (t.utc? ? 'Z' : '%:z')
|
48
|
+
f.freeze
|
49
|
+
end
|
33
50
|
end
|
34
51
|
end
|
35
52
|
end
|
@@ -20,7 +20,7 @@ module Ougai
|
|
20
20
|
def call(severity, time, progname, data)
|
21
21
|
msg = data.delete(:msg)
|
22
22
|
level = @plain ? severity : colored_level(severity)
|
23
|
-
strs = ["[#{time
|
23
|
+
strs = ["[#{format_datetime(time)}] #{level}: #{msg}"]
|
24
24
|
if err_str = create_err_str(data)
|
25
25
|
strs.push(err_str)
|
26
26
|
end
|
data/lib/ougai/logger.rb
CHANGED
@@ -21,7 +21,7 @@ module Ougai
|
|
21
21
|
# @param logger [Logger] The logger receiving broadcast logs.
|
22
22
|
def self.broadcast(logger)
|
23
23
|
Module.new do |mdl|
|
24
|
-
|
24
|
+
Logger::Severity.constants.each do |severity|
|
25
25
|
method_name = severity.downcase.to_sym
|
26
26
|
|
27
27
|
mdl.send(:define_method, method_name) do |*args|
|
data/lib/ougai/version.rb
CHANGED
@@ -3,6 +3,24 @@ require 'spec_helper'
|
|
3
3
|
describe Ougai::Formatters::Base do
|
4
4
|
subject { described_class.new(app_name, hostname) }
|
5
5
|
|
6
|
+
context 'default' do
|
7
|
+
let (:app_name) { nil }
|
8
|
+
let (:hostname) { nil }
|
9
|
+
|
10
|
+
it 'has datetime format default ISO8601' do
|
11
|
+
expect(subject.datetime_format).to match(/^\%FT\%T\.\%3N(Z|\%\:z)$/)
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'has datetime_format accessor' do
|
15
|
+
subject.datetime_format = '%I:%M:%S %p'
|
16
|
+
expect(subject.datetime_format).to eq('%I:%M:%S %p')
|
17
|
+
|
18
|
+
# revert default format by to set nil
|
19
|
+
subject.datetime_format = nil
|
20
|
+
expect(subject.datetime_format).to match(/^\%FT\%T\.\%3N(Z|\%\:z)$/)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
6
24
|
context 'without arguments and hostname contains a UTF-8 char' do
|
7
25
|
let (:app_name) { nil }
|
8
26
|
let (:hostname) { nil }
|
@@ -114,4 +114,20 @@ describe Ougai::Formatters::Bunyan do
|
|
114
114
|
end
|
115
115
|
end
|
116
116
|
|
117
|
+
describe '#datetime_format' do
|
118
|
+
subject do
|
119
|
+
formatter.call('DEBUG', Time.now, nil, data)
|
120
|
+
end
|
121
|
+
|
122
|
+
context 'is time AM/PM format' do
|
123
|
+
before do
|
124
|
+
formatter.datetime_format = '%I:%M:%S %p'
|
125
|
+
end
|
126
|
+
|
127
|
+
it 'applys output' do
|
128
|
+
result = JSON.parse(subject, symbolize_names: true)
|
129
|
+
expect(result[:time]).to match(/^\d{2}:\d{2}:\d{2} [AP]M$/)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
117
133
|
end
|
@@ -1,6 +1,8 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Ougai::Formatters::Readable do
|
4
|
+
let!(:re_start_with_datetime) { /^\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}(Z|[\+\-\:0-9]{4,6})]/ }
|
5
|
+
|
4
6
|
let(:data) do
|
5
7
|
{
|
6
8
|
msg: 'Log Message!',
|
@@ -18,62 +20,73 @@ describe Ougai::Formatters::Readable do
|
|
18
20
|
}
|
19
21
|
end
|
20
22
|
|
23
|
+
let(:formatter) { described_class.new }
|
24
|
+
|
21
25
|
context 'when severity is TRACE' do
|
22
|
-
subject {
|
26
|
+
subject { formatter.call('TRACE', Time.now, nil, data) }
|
23
27
|
|
24
28
|
it 'includes valid strings' do
|
29
|
+
expect(subject).to match(re_start_with_datetime)
|
25
30
|
expect(subject).to include("\e[0;34mTRACE\e[0m: Log Message!")
|
26
31
|
expect(subject.gsub(/\e\[([;\d]+)?m/, '')).to include(':status => 200')
|
27
32
|
end
|
28
33
|
end
|
29
34
|
|
30
35
|
context 'when severity is DEBUG' do
|
31
|
-
subject {
|
36
|
+
subject { formatter.call('DEBUG', Time.now, nil, data) }
|
32
37
|
|
33
38
|
it 'includes valid strings' do
|
39
|
+
expect(subject).to match(re_start_with_datetime)
|
34
40
|
expect(subject).to include("\e[0;37mDEBUG\e[0m: Log Message!")
|
35
41
|
expect(subject.gsub(/\e\[([;\d]+)?m/, '')).to include(':status => 200')
|
36
42
|
end
|
37
43
|
end
|
38
44
|
|
39
45
|
context 'when severity is INFO' do
|
40
|
-
subject {
|
46
|
+
subject { formatter.call('INFO', Time.now, nil, data) }
|
41
47
|
|
42
48
|
it 'includes valid strings' do
|
49
|
+
expect(subject).to match(re_start_with_datetime)
|
43
50
|
expect(subject).to include("\e[0;36mINFO\e[0m: Log Message!")
|
44
51
|
expect(subject.gsub(/\e\[([;\d]+)?m/, '')).to include(':method => "GET"')
|
45
52
|
end
|
46
53
|
end
|
47
54
|
|
48
55
|
context 'when severity is WARN' do
|
49
|
-
subject {
|
56
|
+
subject { formatter.call('WARN', Time.now, nil, data) }
|
50
57
|
|
51
58
|
it 'includes valid strings' do
|
59
|
+
expect(subject).to match(re_start_with_datetime)
|
52
60
|
expect(subject).to include("\e[0;33mWARN\e[0m: Log Message!")
|
53
61
|
expect(subject.gsub(/\e\[([;\d]+)?m/, '')).to include(':path => "/"')
|
54
62
|
end
|
55
63
|
end
|
56
64
|
|
57
65
|
context 'when severity is ERROR' do
|
58
|
-
subject {
|
66
|
+
subject { formatter.call('ERROR', Time.now, nil, data.merge({ err: err })) }
|
59
67
|
|
60
68
|
it 'includes valid strings' do
|
69
|
+
expect(subject).to match(re_start_with_datetime)
|
61
70
|
expect(subject).to include("\e[0;31mERROR\e[0m: Log Message!")
|
62
71
|
expect(subject.gsub(/\e\[([;\d]+)?m/, '')).to include('DummyError (it is dummy.):')
|
63
72
|
end
|
64
73
|
end
|
65
74
|
|
66
75
|
context 'when severity is FATAL' do
|
67
|
-
subject {
|
76
|
+
subject { formatter.call('FATAL', Time.now, nil, { msg: 'TheEnd', err: err }) }
|
77
|
+
|
68
78
|
it 'includes valid strings' do
|
79
|
+
expect(subject).to match(re_start_with_datetime)
|
69
80
|
expect(subject).to include("\e[0;35mFATAL\e[0m: TheEnd")
|
70
81
|
expect(subject.gsub(/\e\[([;\d]+)?m/, '')).to include("error1.rb\n error2.rb")
|
71
82
|
end
|
72
83
|
end
|
73
84
|
|
74
85
|
context 'when severity is UNKNOWN' do
|
75
|
-
subject {
|
86
|
+
subject { formatter.call('ANY', Time.now, nil, { msg: 'unknown msg' }) }
|
87
|
+
|
76
88
|
it 'includes valid strings' do
|
89
|
+
expect(subject).to match(re_start_with_datetime)
|
77
90
|
expect(subject).to include("\e[0;32mANY\e[0m: unknown msg")
|
78
91
|
end
|
79
92
|
end
|
@@ -91,4 +104,20 @@ describe Ougai::Formatters::Readable do
|
|
91
104
|
expect(plain_subject).not_to include(':method => "GET"')
|
92
105
|
end
|
93
106
|
end
|
107
|
+
|
108
|
+
describe '#datetime_format' do
|
109
|
+
subject do
|
110
|
+
formatter.call('DEBUG', Time.now, nil, data)
|
111
|
+
end
|
112
|
+
|
113
|
+
context 'is time AM/PM format' do
|
114
|
+
before do
|
115
|
+
formatter.datetime_format = '%I:%M:%S %p'
|
116
|
+
end
|
117
|
+
|
118
|
+
it 'applys output' do
|
119
|
+
expect(subject).to match(/^\[\d{2}:\d{2}:\d{2} [AP]M\]/)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
94
123
|
end
|
data/spec/logger_spec.rb
CHANGED
@@ -565,6 +565,12 @@ describe Ougai::Logger do
|
|
565
565
|
logger.level = Logger::INFO # propagate severity to another one
|
566
566
|
end
|
567
567
|
|
568
|
+
it 'does not output trace log on both loggers' do
|
569
|
+
logger.trace(log_msg, foo: 0)
|
570
|
+
expect(item).to be_nil
|
571
|
+
expect(another_item).to be_nil
|
572
|
+
end
|
573
|
+
|
568
574
|
it 'does not output debug log on both loggers' do
|
569
575
|
logger.debug(log_msg, foo: 1)
|
570
576
|
expect(item).to be_nil
|
@@ -602,20 +608,20 @@ describe Ougai::Logger do
|
|
602
608
|
|
603
609
|
context 'another logger level is lower than original one' do
|
604
610
|
before do
|
605
|
-
logger.level = Logger::
|
606
|
-
another_logger.level =
|
611
|
+
logger.level = Logger::DEBUG
|
612
|
+
another_logger.level = :trace
|
607
613
|
end
|
608
614
|
|
609
|
-
it '
|
610
|
-
logger.
|
615
|
+
it 'does not output trace log on both loggers' do
|
616
|
+
logger.trace(log_msg)
|
611
617
|
expect(item).to be_nil
|
612
|
-
expect(another_item).to be_log_message(log_msg,
|
618
|
+
expect(another_item).to be_log_message(log_msg, 10)
|
613
619
|
end
|
614
620
|
|
615
|
-
it 'outputs
|
616
|
-
logger.
|
617
|
-
expect(item).to be_log_message(log_msg,
|
618
|
-
expect(another_item).to be_log_message(log_msg,
|
621
|
+
it 'outputs debug log on both loggers' do
|
622
|
+
logger.debug(log_msg)
|
623
|
+
expect(item).to be_log_message(log_msg, 20)
|
624
|
+
expect(another_item).to be_log_message(log_msg, 20)
|
619
625
|
end
|
620
626
|
end
|
621
627
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ougai
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.5.
|
4
|
+
version: 1.5.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Toshimitsu Takahashi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-09-
|
11
|
+
date: 2017-09-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|