/
test_logging.rb
271 lines (210 loc) · 8.31 KB
/
test_logging.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
require File.expand_path('../setup', __FILE__)
module TestLogging
class TestLogging < Test::Unit::TestCase
include LoggingTestCase
def setup
super
@levels = ::Logging::LEVELS
@lnames = ::Logging::LNAMES
@fn = File.join(TMP, 'test.log')
@glob = File.join(TMP, '*.log')
end
def test_backtrace
assert_equal true, ::Logging.backtrace
assert_equal false, ::Logging.backtrace('off')
assert_equal false, ::Logging.backtrace
assert_equal true, ::Logging.backtrace('on')
assert_equal true, ::Logging.backtrace
assert_equal false, ::Logging.backtrace(:off)
assert_equal false, ::Logging.backtrace
assert_equal true, ::Logging.backtrace(:on)
assert_equal true, ::Logging.backtrace
assert_equal false, ::Logging.backtrace(false)
assert_equal false, ::Logging.backtrace
assert_equal true, ::Logging.backtrace(true)
assert_equal true, ::Logging.backtrace
assert_raise(ArgumentError) {::Logging.backtrace 'foo'}
end
def test_configure
assert_raise(ArgumentError) {::Logging.configure 'blah.txt'}
::Logging.configure 'data/logging.yaml'
names = %w[DEB INF PRT WRN ERR FAT]
assert_equal names, ::Logging::LNAMES
assert_equal :inspect, ::Logging::OBJ_FORMAT
assert_equal 3, ::Logging::Logger.root.level
# verify the appenders
h = ::Logging::Appenders.instance_variable_get :@appenders
assert_equal ['logfile', 'stderr'], h.keys.sort
# start with the File appender
logfile = ::Logging::Appenders['logfile']
assert_instance_of ::Logging::Appenders::File, logfile
assert_equal 0, logfile.level
assert_equal ::File.expand_path('tmp/temp.log'), logfile.instance_variable_get(:@fn)
layout = logfile.layout
assert_instance_of ::Logging::Layouts::Pattern, layout
assert_equal '[%d] %l %c : %m\\n', layout.pattern
assert_equal 'to_s', layout.date_method
assert_nil layout.date_pattern
# and now the Stderr appender
stderr = ::Logging::Appenders['stderr']
assert_instance_of ::Logging::Appenders::Stderr, stderr
assert_equal 0, stderr.level
layout = stderr.layout
assert_instance_of ::Logging::Layouts::Basic, layout
# verify the loggers
h = ::Logging::Repository.instance.instance_variable_get :@h
assert_equal 4, h.length
# mylogger
mylogger = ::Logging::Logger['mylogger']
assert_equal 0, mylogger.level
assert_equal false, mylogger.additive
assert_equal false, mylogger.trace
appenders = mylogger.instance_variable_get :@appenders
assert_equal 2, appenders.length
assert_equal ['logfile', 'stderr'], appenders.map {|a| a.name}.sort
# yourlogger
yourlogger = ::Logging::Logger['yourlogger']
assert_equal 1, yourlogger.level
assert_equal true, yourlogger.additive
assert_equal false, yourlogger.trace
appenders = yourlogger.instance_variable_get :@appenders
assert_equal 2, appenders.length
assert_equal ['logfile', 'stderr'], appenders.map {|a| a.name}.sort
end
def test_logger
assert_raise(TypeError) {::Logging.logger []}
logger = ::Logging.logger STDOUT
assert_match %r/\A-?\d+\z/, logger.name
assert_same logger, ::Logging.logger(STDOUT)
logger.close
assert !STDOUT.closed?
assert !File.exist?(@fn)
fd = File.new @fn, 'w'
logger = ::Logging.logger fd, 2, 100
assert_equal @fn, logger.name
logger.debug 'this is a debug message'
logger.warn 'this is a warning message'
logger.error 'and now we should have over 100 bytes of data ' +
'in the log file'
logger.info 'but the log file should not roll since we provided ' +
'a file descriptor -- not a file name'
logger.close
assert fd.closed?
assert File.exist?(@fn)
assert_equal 1, Dir.glob(@glob).length
FileUtils.rm_f @fn
assert !File.exist?(@fn)
logger = ::Logging.logger @fn, 2, 100
assert File.exist?(@fn)
assert_equal @fn, logger.name
logger.debug 'this is a debug message'
logger.warn 'this is a warning message'
logger.error 'and now we should have over 100 bytes of data ' +
'in the log file'
logger.info 'but the log file should not roll since we provided ' +
'a file descriptor -- not a file name'
logger.close
assert_equal 3, Dir.glob(@glob).length
end
def test_init_default
assert_equal({}, @levels)
assert_equal([], @lnames)
assert_same false, ::Logging.initialized?
::Logging::Repository.instance
assert_equal 5, @levels.length
assert_equal 5, @lnames.length
assert_equal 5, ::Logging::MAX_LEVEL_LENGTH
assert_equal 0, @levels['debug']
assert_equal 1, @levels['info']
assert_equal 2, @levels['warn']
assert_equal 3, @levels['error']
assert_equal 4, @levels['fatal']
assert_equal 'DEBUG', @lnames[0]
assert_equal 'INFO', @lnames[1]
assert_equal 'WARN', @lnames[2]
assert_equal 'ERROR', @lnames[3]
assert_equal 'FATAL', @lnames[4]
end
def test_init_special
assert_equal({}, @levels)
assert_equal([], @lnames)
assert_same false, ::Logging.initialized?
assert_raise(ArgumentError) {::Logging.init(1, 2, 3, 4)}
::Logging.init :one, 'two', :THREE, 'FoUr', :sIx
assert_equal 5, @levels.length
assert_equal 5, @lnames.length
assert_equal 5, ::Logging::MAX_LEVEL_LENGTH
assert_equal 0, @levels['one']
assert_equal 1, @levels['two']
assert_equal 2, @levels['three']
assert_equal 3, @levels['four']
assert_equal 4, @levels['six']
assert_equal 'ONE', @lnames[0]
assert_equal 'TWO', @lnames[1]
assert_equal 'THREE', @lnames[2]
assert_equal 'FOUR', @lnames[3]
assert_equal 'SIX', @lnames[4]
end
def test_init_all_off
assert_equal({}, @levels)
assert_equal([], @lnames)
assert_same false, ::Logging.initialized?
::Logging.init %w(a b all c off d)
assert_equal 4, @levels.length
assert_equal 4, @lnames.length
assert_equal 3, ::Logging::MAX_LEVEL_LENGTH
assert_equal 0, @levels['a']
assert_equal 1, @levels['b']
assert_equal 2, @levels['c']
assert_equal 3, @levels['d']
assert_equal 'A', @lnames[0]
assert_equal 'B', @lnames[1]
assert_equal 'C', @lnames[2]
assert_equal 'D', @lnames[3]
end
def test_format_as
assert_equal false, ::Logging.const_defined?('OBJ_FORMAT')
assert_raises(ArgumentError) {::Logging.format_as 'bob'}
assert_raises(ArgumentError) {::Logging.format_as String}
assert_raises(ArgumentError) {::Logging.format_as :what?}
remove_const = lambda do |const|
::Logging.class_eval {remove_const const if const_defined? const}
end
::Logging.format_as :string
assert ::Logging.const_defined?('OBJ_FORMAT')
assert_equal :string, ::Logging::OBJ_FORMAT
remove_const[:OBJ_FORMAT]
::Logging.format_as :inspect
assert ::Logging.const_defined?('OBJ_FORMAT')
assert_equal :inspect, ::Logging::OBJ_FORMAT
remove_const[:OBJ_FORMAT]
::Logging.format_as :json
assert ::Logging.const_defined?('OBJ_FORMAT')
assert_equal :json, ::Logging::OBJ_FORMAT
remove_const[:OBJ_FORMAT]
::Logging.format_as :yaml
assert ::Logging.const_defined?('OBJ_FORMAT')
assert_equal :yaml, ::Logging::OBJ_FORMAT
remove_const[:OBJ_FORMAT]
::Logging.format_as 'string'
assert ::Logging.const_defined?('OBJ_FORMAT')
assert_equal :string, ::Logging::OBJ_FORMAT
remove_const[:OBJ_FORMAT]
::Logging.format_as 'inspect'
assert ::Logging.const_defined?('OBJ_FORMAT')
assert_equal :inspect, ::Logging::OBJ_FORMAT
remove_const[:OBJ_FORMAT]
::Logging.format_as 'yaml'
assert ::Logging.const_defined?('OBJ_FORMAT')
assert_equal :yaml, ::Logging::OBJ_FORMAT
remove_const[:OBJ_FORMAT]
end
def test_path
path = ::Logging.path(*%w[one two three])
assert_match %r/one\/two\/three$/, path
end
def test_version
assert_match %r/\d+\.\d+\.\d+/, ::Logging.version
end
end # class TestLogging
end # module TestLogging