logger 1.2.8.1 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +8 -0
- data/.travis.yml +6 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +22 -0
- data/README.md +39 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/logger.rb +375 -281
- data/logger.gemspec +27 -0
- metadata +57 -8
- data/test/logger/test_logger.rb +0 -527
data/logger.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
begin
|
2
|
+
require_relative "lib/logger"
|
3
|
+
rescue LoadError
|
4
|
+
# for Ruby core repository
|
5
|
+
require_relative "logger"
|
6
|
+
end
|
7
|
+
|
8
|
+
Gem::Specification.new do |spec|
|
9
|
+
spec.name = "logger"
|
10
|
+
spec.version = Logger::VERSION
|
11
|
+
spec.authors = ["SHIBATA Hiroshi"]
|
12
|
+
spec.email = ["hsbt@ruby-lang.org"]
|
13
|
+
|
14
|
+
spec.summary = %q{Provides a simple logging utility for outputting messages.}
|
15
|
+
spec.description = %q{Provides a simple logging utility for outputting messages.}
|
16
|
+
spec.homepage = "https://github.com/ruby/logger"
|
17
|
+
spec.license = "BSD-2-Clause"
|
18
|
+
|
19
|
+
spec.files = [".gitignore", ".travis.yml", "Gemfile", "LICENSE.txt", "README.md", "Rakefile", "bin/console", "bin/setup", "lib/logger.rb", "logger.gemspec"]
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
27
|
+
end
|
metadata
CHANGED
@@ -1,26 +1,74 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: logger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
- NAKAMURA, Hiroshi
|
8
7
|
- SHIBATA Hiroshi
|
9
8
|
autorequire:
|
10
|
-
bindir:
|
9
|
+
bindir: exe
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
13
|
-
dependencies:
|
11
|
+
date: 2018-12-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
14
55
|
description: Provides a simple logging utility for outputting messages.
|
15
56
|
email:
|
16
|
-
- nahi@ruby-lang.org
|
17
57
|
- hsbt@ruby-lang.org
|
18
58
|
executables: []
|
19
59
|
extensions: []
|
20
60
|
extra_rdoc_files: []
|
21
61
|
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- bin/console
|
69
|
+
- bin/setup
|
22
70
|
- lib/logger.rb
|
23
|
-
-
|
71
|
+
- logger.gemspec
|
24
72
|
homepage: https://github.com/ruby/logger
|
25
73
|
licenses:
|
26
74
|
- BSD-2-Clause
|
@@ -40,7 +88,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
40
88
|
- !ruby/object:Gem::Version
|
41
89
|
version: '0'
|
42
90
|
requirements: []
|
43
|
-
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.7.6
|
44
93
|
signing_key:
|
45
94
|
specification_version: 4
|
46
95
|
summary: Provides a simple logging utility for outputting messages.
|
data/test/logger/test_logger.rb
DELETED
@@ -1,527 +0,0 @@
|
|
1
|
-
require 'test/unit'
|
2
|
-
require 'logger'
|
3
|
-
require 'tempfile'
|
4
|
-
|
5
|
-
|
6
|
-
class TestLoggerSeverity < Test::Unit::TestCase
|
7
|
-
def test_enum
|
8
|
-
logger_levels = Logger.constants
|
9
|
-
levels = ["WARN", "UNKNOWN", "INFO", "FATAL", "DEBUG", "ERROR"]
|
10
|
-
Logger::Severity.constants.each do |level|
|
11
|
-
assert(levels.include?(level.to_s))
|
12
|
-
assert(logger_levels.include?(level))
|
13
|
-
end
|
14
|
-
assert_equal(levels.size, Logger::Severity.constants.size)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
|
19
|
-
class TestLogger < Test::Unit::TestCase
|
20
|
-
include Logger::Severity
|
21
|
-
|
22
|
-
def setup
|
23
|
-
@logger = Logger.new(nil)
|
24
|
-
@filename = __FILE__ + ".#{$$}"
|
25
|
-
end
|
26
|
-
|
27
|
-
def teardown
|
28
|
-
unless $DEBUG
|
29
|
-
File.unlink(@filename) if File.exist?(@filename)
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
class Log
|
34
|
-
attr_reader :label, :datetime, :pid, :severity, :progname, :msg
|
35
|
-
def initialize(line)
|
36
|
-
/\A(\w+), \[([^#]*)#(\d+)\]\s+(\w+) -- (\w*): ([\x0-\xff]*)/ =~ line
|
37
|
-
@label, @datetime, @pid, @severity, @progname, @msg = $1, $2, $3, $4, $5, $6
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
def log_add(logger, severity, msg, progname = nil, &block)
|
42
|
-
log(logger, :add, severity, msg, progname, &block)
|
43
|
-
end
|
44
|
-
|
45
|
-
def log(logger, msg_id, *arg, &block)
|
46
|
-
Log.new(log_raw(logger, msg_id, *arg, &block))
|
47
|
-
end
|
48
|
-
|
49
|
-
def log_raw(logger, msg_id, *arg, &block)
|
50
|
-
logdev = Tempfile.new(File.basename(__FILE__) + '.log')
|
51
|
-
logger.instance_eval { @logdev = Logger::LogDevice.new(logdev) }
|
52
|
-
logger.__send__(msg_id, *arg, &block)
|
53
|
-
logdev.open
|
54
|
-
msg = logdev.read
|
55
|
-
logdev.close
|
56
|
-
msg
|
57
|
-
end
|
58
|
-
|
59
|
-
def test_level
|
60
|
-
@logger.level = UNKNOWN
|
61
|
-
assert_equal(UNKNOWN, @logger.level)
|
62
|
-
@logger.level = INFO
|
63
|
-
assert_equal(INFO, @logger.level)
|
64
|
-
@logger.sev_threshold = ERROR
|
65
|
-
assert_equal(ERROR, @logger.sev_threshold)
|
66
|
-
@logger.sev_threshold = WARN
|
67
|
-
assert_equal(WARN, @logger.sev_threshold)
|
68
|
-
assert_equal(WARN, @logger.level)
|
69
|
-
|
70
|
-
@logger.level = DEBUG
|
71
|
-
assert(@logger.debug?)
|
72
|
-
assert(@logger.info?)
|
73
|
-
@logger.level = INFO
|
74
|
-
assert(!@logger.debug?)
|
75
|
-
assert(@logger.info?)
|
76
|
-
assert(@logger.warn?)
|
77
|
-
@logger.level = WARN
|
78
|
-
assert(!@logger.info?)
|
79
|
-
assert(@logger.warn?)
|
80
|
-
assert(@logger.error?)
|
81
|
-
@logger.level = ERROR
|
82
|
-
assert(!@logger.warn?)
|
83
|
-
assert(@logger.error?)
|
84
|
-
assert(@logger.fatal?)
|
85
|
-
@logger.level = FATAL
|
86
|
-
assert(!@logger.error?)
|
87
|
-
assert(@logger.fatal?)
|
88
|
-
@logger.level = UNKNOWN
|
89
|
-
assert(!@logger.error?)
|
90
|
-
assert(!@logger.fatal?)
|
91
|
-
end
|
92
|
-
|
93
|
-
def test_progname
|
94
|
-
assert_nil(@logger.progname)
|
95
|
-
@logger.progname = "name"
|
96
|
-
assert_equal("name", @logger.progname)
|
97
|
-
end
|
98
|
-
|
99
|
-
def test_datetime_format
|
100
|
-
dummy = STDERR
|
101
|
-
logger = Logger.new(dummy)
|
102
|
-
log = log_add(logger, INFO, "foo")
|
103
|
-
assert_match(/^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d.\s*\d+ $/, log.datetime)
|
104
|
-
logger.datetime_format = "%d%b%Y@%H:%M:%S"
|
105
|
-
log = log_add(logger, INFO, "foo")
|
106
|
-
assert_match(/^\d\d\w\w\w\d\d\d\d@\d\d:\d\d:\d\d$/, log.datetime)
|
107
|
-
logger.datetime_format = ""
|
108
|
-
log = log_add(logger, INFO, "foo")
|
109
|
-
assert_match(/^$/, log.datetime)
|
110
|
-
end
|
111
|
-
|
112
|
-
def test_formatter
|
113
|
-
dummy = STDERR
|
114
|
-
logger = Logger.new(dummy)
|
115
|
-
# default
|
116
|
-
log = log(logger, :info, "foo")
|
117
|
-
assert_equal("foo\n", log.msg)
|
118
|
-
# config
|
119
|
-
logger.formatter = proc { |severity, timestamp, progname, msg|
|
120
|
-
"#{severity}:#{msg}\n\n"
|
121
|
-
}
|
122
|
-
line = log_raw(logger, :info, "foo")
|
123
|
-
assert_equal("INFO:foo\n\n", line)
|
124
|
-
# recover
|
125
|
-
logger.formatter = nil
|
126
|
-
log = log(logger, :info, "foo")
|
127
|
-
assert_equal("foo\n", log.msg)
|
128
|
-
# again
|
129
|
-
o = Object.new
|
130
|
-
def o.call(severity, timestamp, progname, msg)
|
131
|
-
"<<#{severity}-#{msg}>>\n"
|
132
|
-
end
|
133
|
-
logger.formatter = o
|
134
|
-
line = log_raw(logger, :info, "foo")
|
135
|
-
assert_equal("<<INFO-foo>>\n", line)
|
136
|
-
end
|
137
|
-
|
138
|
-
def test_initialize
|
139
|
-
logger = Logger.new(STDERR)
|
140
|
-
assert_nil(logger.progname)
|
141
|
-
assert_equal(DEBUG, logger.level)
|
142
|
-
assert_nil(logger.datetime_format)
|
143
|
-
end
|
144
|
-
|
145
|
-
def test_add
|
146
|
-
logger = Logger.new(nil)
|
147
|
-
logger.progname = "my_progname"
|
148
|
-
assert(logger.add(INFO))
|
149
|
-
log = log_add(logger, nil, "msg")
|
150
|
-
assert_equal("ANY", log.severity)
|
151
|
-
assert_equal("my_progname", log.progname)
|
152
|
-
logger.level = WARN
|
153
|
-
assert(logger.log(INFO))
|
154
|
-
assert_nil(log_add(logger, INFO, "msg").msg)
|
155
|
-
log = log_add(logger, WARN, nil) { "msg" }
|
156
|
-
assert_equal("msg\n", log.msg)
|
157
|
-
log = log_add(logger, WARN, "") { "msg" }
|
158
|
-
assert_equal("\n", log.msg)
|
159
|
-
assert_equal("my_progname", log.progname)
|
160
|
-
log = log_add(logger, WARN, nil, "progname?")
|
161
|
-
assert_equal("progname?\n", log.msg)
|
162
|
-
assert_equal("my_progname", log.progname)
|
163
|
-
end
|
164
|
-
|
165
|
-
def test_level_log
|
166
|
-
logger = Logger.new(nil)
|
167
|
-
logger.progname = "my_progname"
|
168
|
-
log = log(logger, :debug, "custom_progname") { "msg" }
|
169
|
-
assert_equal("msg\n", log.msg)
|
170
|
-
assert_equal("custom_progname", log.progname)
|
171
|
-
assert_equal("DEBUG", log.severity)
|
172
|
-
assert_equal("D", log.label)
|
173
|
-
#
|
174
|
-
log = log(logger, :debug) { "msg_block" }
|
175
|
-
assert_equal("msg_block\n", log.msg)
|
176
|
-
assert_equal("my_progname", log.progname)
|
177
|
-
log = log(logger, :debug, "msg_inline")
|
178
|
-
assert_equal("msg_inline\n", log.msg)
|
179
|
-
assert_equal("my_progname", log.progname)
|
180
|
-
#
|
181
|
-
log = log(logger, :info, "custom_progname") { "msg" }
|
182
|
-
assert_equal("msg\n", log.msg)
|
183
|
-
assert_equal("custom_progname", log.progname)
|
184
|
-
assert_equal("INFO", log.severity)
|
185
|
-
assert_equal("I", log.label)
|
186
|
-
#
|
187
|
-
log = log(logger, :warn, "custom_progname") { "msg" }
|
188
|
-
assert_equal("msg\n", log.msg)
|
189
|
-
assert_equal("custom_progname", log.progname)
|
190
|
-
assert_equal("WARN", log.severity)
|
191
|
-
assert_equal("W", log.label)
|
192
|
-
#
|
193
|
-
log = log(logger, :error, "custom_progname") { "msg" }
|
194
|
-
assert_equal("msg\n", log.msg)
|
195
|
-
assert_equal("custom_progname", log.progname)
|
196
|
-
assert_equal("ERROR", log.severity)
|
197
|
-
assert_equal("E", log.label)
|
198
|
-
#
|
199
|
-
log = log(logger, :fatal, "custom_progname") { "msg" }
|
200
|
-
assert_equal("msg\n", log.msg)
|
201
|
-
assert_equal("custom_progname", log.progname)
|
202
|
-
assert_equal("FATAL", log.severity)
|
203
|
-
assert_equal("F", log.label)
|
204
|
-
#
|
205
|
-
log = log(logger, :unknown, "custom_progname") { "msg" }
|
206
|
-
assert_equal("msg\n", log.msg)
|
207
|
-
assert_equal("custom_progname", log.progname)
|
208
|
-
assert_equal("ANY", log.severity)
|
209
|
-
assert_equal("A", log.label)
|
210
|
-
end
|
211
|
-
|
212
|
-
def test_close
|
213
|
-
r, w = IO.pipe
|
214
|
-
assert(!w.closed?)
|
215
|
-
logger = Logger.new(w)
|
216
|
-
logger.close
|
217
|
-
assert(w.closed?)
|
218
|
-
r.close
|
219
|
-
end
|
220
|
-
|
221
|
-
class MyError < StandardError
|
222
|
-
end
|
223
|
-
|
224
|
-
class MyMsg
|
225
|
-
def inspect
|
226
|
-
"my_msg"
|
227
|
-
end
|
228
|
-
end
|
229
|
-
|
230
|
-
def test_format
|
231
|
-
logger = Logger.new(nil)
|
232
|
-
log = log_add(logger, INFO, "msg\n")
|
233
|
-
assert_equal("msg\n\n", log.msg)
|
234
|
-
begin
|
235
|
-
raise MyError.new("excn")
|
236
|
-
rescue MyError => e
|
237
|
-
log = log_add(logger, INFO, e)
|
238
|
-
assert_match(/^excn \(TestLogger::MyError\)/, log.msg)
|
239
|
-
# expects backtrace is dumped across multi lines. 10 might be changed.
|
240
|
-
assert(log.msg.split(/\n/).size >= 10)
|
241
|
-
end
|
242
|
-
log = log_add(logger, INFO, MyMsg.new)
|
243
|
-
assert_equal("my_msg\n", log.msg)
|
244
|
-
end
|
245
|
-
|
246
|
-
def test_lshift
|
247
|
-
r, w = IO.pipe
|
248
|
-
logger = Logger.new(w)
|
249
|
-
logger << "msg"
|
250
|
-
read_ready, = IO.select([r], nil, nil, 0.1)
|
251
|
-
w.close
|
252
|
-
msg = r.read
|
253
|
-
r.close
|
254
|
-
assert_equal("msg", msg)
|
255
|
-
#
|
256
|
-
r, w = IO.pipe
|
257
|
-
logger = Logger.new(w)
|
258
|
-
logger << "msg2\n\n"
|
259
|
-
read_ready, = IO.select([r], nil, nil, 0.1)
|
260
|
-
w.close
|
261
|
-
msg = r.read
|
262
|
-
r.close
|
263
|
-
assert_equal("msg2\n\n", msg)
|
264
|
-
end
|
265
|
-
end
|
266
|
-
|
267
|
-
class TestLogDevice < Test::Unit::TestCase
|
268
|
-
class LogExcnRaiser
|
269
|
-
def write(*arg)
|
270
|
-
raise 'disk is full'
|
271
|
-
end
|
272
|
-
|
273
|
-
def close
|
274
|
-
end
|
275
|
-
|
276
|
-
def stat
|
277
|
-
Object.new
|
278
|
-
end
|
279
|
-
end
|
280
|
-
|
281
|
-
def setup
|
282
|
-
@filename = __FILE__ + ".#{$$}"
|
283
|
-
end
|
284
|
-
|
285
|
-
def teardown
|
286
|
-
unless $DEBUG
|
287
|
-
File.unlink(@filename) if File.exist?(@filename)
|
288
|
-
end
|
289
|
-
end
|
290
|
-
|
291
|
-
def d(log, opt = {})
|
292
|
-
Logger::LogDevice.new(log, opt)
|
293
|
-
end
|
294
|
-
|
295
|
-
def test_initialize
|
296
|
-
logdev = d(STDERR)
|
297
|
-
assert_equal(STDERR, logdev.dev)
|
298
|
-
assert_nil(logdev.filename)
|
299
|
-
assert_raises(TypeError) do
|
300
|
-
d(nil)
|
301
|
-
end
|
302
|
-
#
|
303
|
-
logdev = d(@filename)
|
304
|
-
begin
|
305
|
-
assert(File.exist?(@filename))
|
306
|
-
assert(logdev.dev.sync)
|
307
|
-
assert_equal(@filename, logdev.filename)
|
308
|
-
logdev.write('hello')
|
309
|
-
ensure
|
310
|
-
logdev.close
|
311
|
-
end
|
312
|
-
# create logfile whitch is already exist.
|
313
|
-
logdev = d(@filename)
|
314
|
-
begin
|
315
|
-
logdev.write('world')
|
316
|
-
logfile = File.read(@filename)
|
317
|
-
assert_equal(2, logfile.split(/\n/).size)
|
318
|
-
assert_match(/^helloworld$/, logfile)
|
319
|
-
ensure
|
320
|
-
logdev.close
|
321
|
-
end
|
322
|
-
end
|
323
|
-
|
324
|
-
def test_write
|
325
|
-
r, w = IO.pipe
|
326
|
-
logdev = d(w)
|
327
|
-
logdev.write("msg2\n\n")
|
328
|
-
read_ready, = IO.select([r], nil, nil, 0.1)
|
329
|
-
w.close
|
330
|
-
msg = r.read
|
331
|
-
r.close
|
332
|
-
assert_equal("msg2\n\n", msg)
|
333
|
-
#
|
334
|
-
logdev = d(LogExcnRaiser.new)
|
335
|
-
class << (stderr = '')
|
336
|
-
alias write <<
|
337
|
-
end
|
338
|
-
$stderr, stderr = stderr, $stderr
|
339
|
-
begin
|
340
|
-
assert_nothing_raised do
|
341
|
-
logdev.write('hello')
|
342
|
-
end
|
343
|
-
ensure
|
344
|
-
logdev.close
|
345
|
-
$stderr, stderr = stderr, $stderr
|
346
|
-
end
|
347
|
-
assert_equal "log writing failed. disk is full\n", stderr
|
348
|
-
end
|
349
|
-
|
350
|
-
def test_close
|
351
|
-
r, w = IO.pipe
|
352
|
-
logdev = d(w)
|
353
|
-
logdev.write("msg2\n\n")
|
354
|
-
read_ready, = IO.select([r], nil, nil, 0.1)
|
355
|
-
assert(!w.closed?)
|
356
|
-
logdev.close
|
357
|
-
assert(w.closed?)
|
358
|
-
r.close
|
359
|
-
end
|
360
|
-
|
361
|
-
def test_shifting_size
|
362
|
-
logfile = File.basename(__FILE__) + '_1.log'
|
363
|
-
logfile0 = logfile + '.0'
|
364
|
-
logfile1 = logfile + '.1'
|
365
|
-
logfile2 = logfile + '.2'
|
366
|
-
logfile3 = logfile + '.3'
|
367
|
-
File.unlink(logfile) if File.exist?(logfile)
|
368
|
-
File.unlink(logfile0) if File.exist?(logfile0)
|
369
|
-
File.unlink(logfile1) if File.exist?(logfile1)
|
370
|
-
File.unlink(logfile2) if File.exist?(logfile2)
|
371
|
-
logger = Logger.new(logfile, 4, 100)
|
372
|
-
logger.error("0" * 15)
|
373
|
-
assert(File.exist?(logfile))
|
374
|
-
assert(!File.exist?(logfile0))
|
375
|
-
logger.error("0" * 15)
|
376
|
-
assert(File.exist?(logfile0))
|
377
|
-
assert(!File.exist?(logfile1))
|
378
|
-
logger.error("0" * 15)
|
379
|
-
assert(File.exist?(logfile1))
|
380
|
-
assert(!File.exist?(logfile2))
|
381
|
-
logger.error("0" * 15)
|
382
|
-
assert(File.exist?(logfile2))
|
383
|
-
assert(!File.exist?(logfile3))
|
384
|
-
logger.error("0" * 15)
|
385
|
-
assert(!File.exist?(logfile3))
|
386
|
-
logger.error("0" * 15)
|
387
|
-
assert(!File.exist?(logfile3))
|
388
|
-
logger.close
|
389
|
-
File.unlink(logfile)
|
390
|
-
File.unlink(logfile0)
|
391
|
-
File.unlink(logfile1)
|
392
|
-
File.unlink(logfile2)
|
393
|
-
|
394
|
-
logfile = File.basename(__FILE__) + '_2.log'
|
395
|
-
logfile0 = logfile + '.0'
|
396
|
-
logfile1 = logfile + '.1'
|
397
|
-
logfile2 = logfile + '.2'
|
398
|
-
logfile3 = logfile + '.3'
|
399
|
-
logger = Logger.new(logfile, 4, 150)
|
400
|
-
logger.error("0" * 15)
|
401
|
-
assert(File.exist?(logfile))
|
402
|
-
assert(!File.exist?(logfile0))
|
403
|
-
logger.error("0" * 15)
|
404
|
-
assert(!File.exist?(logfile0))
|
405
|
-
logger.error("0" * 15)
|
406
|
-
assert(File.exist?(logfile0))
|
407
|
-
assert(!File.exist?(logfile1))
|
408
|
-
logger.error("0" * 15)
|
409
|
-
assert(!File.exist?(logfile1))
|
410
|
-
logger.error("0" * 15)
|
411
|
-
assert(File.exist?(logfile1))
|
412
|
-
assert(!File.exist?(logfile2))
|
413
|
-
logger.error("0" * 15)
|
414
|
-
assert(!File.exist?(logfile2))
|
415
|
-
logger.error("0" * 15)
|
416
|
-
assert(File.exist?(logfile2))
|
417
|
-
assert(!File.exist?(logfile3))
|
418
|
-
logger.error("0" * 15)
|
419
|
-
assert(!File.exist?(logfile3))
|
420
|
-
logger.error("0" * 15)
|
421
|
-
assert(!File.exist?(logfile3))
|
422
|
-
logger.error("0" * 15)
|
423
|
-
assert(!File.exist?(logfile3))
|
424
|
-
logger.close
|
425
|
-
File.unlink(logfile)
|
426
|
-
File.unlink(logfile0)
|
427
|
-
File.unlink(logfile1)
|
428
|
-
File.unlink(logfile2)
|
429
|
-
end
|
430
|
-
|
431
|
-
def test_shifting_age_variants
|
432
|
-
logger = Logger.new(@filename, 'daily')
|
433
|
-
logger.info('daily')
|
434
|
-
logger.close
|
435
|
-
logger = Logger.new(@filename, 'weekly')
|
436
|
-
logger.info('weekly')
|
437
|
-
logger.close
|
438
|
-
logger = Logger.new(@filename, 'monthly')
|
439
|
-
logger.info('monthly')
|
440
|
-
logger.close
|
441
|
-
end
|
442
|
-
|
443
|
-
def test_shifting_age
|
444
|
-
# shift_age other than 'daily', 'weekly', and 'monthly' means 'everytime'
|
445
|
-
yyyymmdd = Time.now.strftime("%Y%m%d")
|
446
|
-
filename1 = @filename + ".#{yyyymmdd}"
|
447
|
-
filename2 = @filename + ".#{yyyymmdd}.1"
|
448
|
-
filename3 = @filename + ".#{yyyymmdd}.2"
|
449
|
-
begin
|
450
|
-
logger = Logger.new(@filename, 'now')
|
451
|
-
assert(File.exist?(@filename))
|
452
|
-
assert(!File.exist?(filename1))
|
453
|
-
assert(!File.exist?(filename2))
|
454
|
-
assert(!File.exist?(filename3))
|
455
|
-
logger.info("0" * 15)
|
456
|
-
assert(File.exist?(@filename))
|
457
|
-
assert(File.exist?(filename1))
|
458
|
-
assert(!File.exist?(filename2))
|
459
|
-
assert(!File.exist?(filename3))
|
460
|
-
logger.warn("0" * 15)
|
461
|
-
assert(File.exist?(@filename))
|
462
|
-
assert(File.exist?(filename1))
|
463
|
-
assert(File.exist?(filename2))
|
464
|
-
assert(!File.exist?(filename3))
|
465
|
-
logger.error("0" * 15)
|
466
|
-
assert(File.exist?(@filename))
|
467
|
-
assert(File.exist?(filename1))
|
468
|
-
assert(File.exist?(filename2))
|
469
|
-
assert(File.exist?(filename3))
|
470
|
-
ensure
|
471
|
-
logger.close if logger
|
472
|
-
[filename1, filename2, filename3].each do |filename|
|
473
|
-
File.unlink(filename) if File.exist?(filename)
|
474
|
-
end
|
475
|
-
end
|
476
|
-
end
|
477
|
-
end
|
478
|
-
|
479
|
-
|
480
|
-
class TestLoggerApplication < Test::Unit::TestCase
|
481
|
-
def setup
|
482
|
-
@app = Logger::Application.new('appname')
|
483
|
-
@filename = __FILE__ + ".#{$$}"
|
484
|
-
end
|
485
|
-
|
486
|
-
def teardown
|
487
|
-
unless $DEBUG
|
488
|
-
File.unlink(@filename) if File.exist?(@filename)
|
489
|
-
end
|
490
|
-
end
|
491
|
-
|
492
|
-
def test_initialize
|
493
|
-
app = Logger::Application.new('appname')
|
494
|
-
assert_equal('appname', app.appname)
|
495
|
-
end
|
496
|
-
|
497
|
-
def test_start
|
498
|
-
@app.set_log(@filename)
|
499
|
-
begin
|
500
|
-
@app.level = Logger::UNKNOWN
|
501
|
-
@app.start # logs FATAL log
|
502
|
-
assert_equal(1, File.read(@filename).split(/\n/).size)
|
503
|
-
ensure
|
504
|
-
@app.logger.close
|
505
|
-
end
|
506
|
-
end
|
507
|
-
|
508
|
-
def test_logger
|
509
|
-
@app.level = Logger::WARN
|
510
|
-
@app.set_log(@filename)
|
511
|
-
begin
|
512
|
-
assert_equal(Logger::WARN, @app.logger.level)
|
513
|
-
ensure
|
514
|
-
@app.logger.close
|
515
|
-
end
|
516
|
-
@app.logger = logger = Logger.new(STDOUT)
|
517
|
-
assert_equal(logger, @app.logger)
|
518
|
-
assert_equal(Logger::WARN, @app.logger.level)
|
519
|
-
@app.log = @filename
|
520
|
-
begin
|
521
|
-
assert(logger != @app.logger)
|
522
|
-
assert_equal(Logger::WARN, @app.logger.level)
|
523
|
-
ensure
|
524
|
-
@app.logger.close
|
525
|
-
end
|
526
|
-
end
|
527
|
-
end
|