2 # frozen_string_literal: false
6 require 'syslog/logger'
8 # skip. see the bottom of this file.
11 # These tests ensure Syslog::Logger works like Logger
13 class TestSyslogRootLogger < Test::Unit::TestCase
17 PRIMASK = Syslog::Level.constants.inject(0) { |mask, name| mask | Syslog::Level.const_get(name) }
20 Syslog::LOG_ALERT => 'ALERT',
21 Syslog::LOG_ERR => 'ERR',
22 Syslog::LOG_WARNING => 'WARNING',
23 Syslog::LOG_NOTICE => 'NOTICE',
24 Syslog::LOG_INFO => 'INFO',
25 Syslog::LOG_DEBUG => 'DEBUG'
28 @facility = Syslog::LOG_USER
34 attr_reader :program_name
36 def log(priority, format, *args)
37 level = priority & PRIMASK
38 @line = "<#{priority}> #{LEVEL_LABEL_MAP[level]} - #{format % args}"
41 def open(program_name)
42 @program_name = program_name
52 Syslog::Logger.syslog = MockSyslog
55 Logger::DEBUG => 'DEBUG',
56 Logger::INFO => 'INFO',
57 Logger::WARN => 'WARN',
58 Logger::ERROR => 'ERROR',
59 Logger::FATAL => 'FATAL',
60 Logger::UNKNOWN => 'ANY',
64 @logger = Logger.new(nil)
68 attr_reader :line, :label, :datetime, :pid, :severity, :progname, :msg
71 /\A(\w+), \[([^#]*)#(\d+)\]\s+(\w+) -- (\w*): ([\x0-\xff]*)/ =~ @line
72 @label, @datetime, @pid, @severity, @progname, @msg = $1, $2, $3, $4, $5, $6
76 def log_add(severity, msg, progname = nil, &block)
77 log(:add, severity, msg, progname, &block)
80 def log(msg_id, *arg, &block)
81 Log.new(log_raw(msg_id, *arg, &block))
84 def log_raw(msg_id, *arg, &block)
85 Tempfile.create(File.basename(__FILE__) + '.log') {|logdev|
86 @logger.instance_eval { @logdev = Logger::LogDevice.new(logdev) }
87 assert_equal true, @logger.__send__(msg_id, *arg, &block)
94 assert_equal Logger::DEBUG, @logger.level
97 def test_custom_formatter
98 @logger.formatter = Class.new {
99 def call severity, time, progname, msg
104 assert_match(/hi mom!/, log_raw(:fatal, 'fatal level message'))
108 msg = log_add nil, 'unknown level message' # nil == unknown
109 assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
111 msg = log_add Logger::FATAL, 'fatal level message'
112 assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
114 msg = log_add Logger::ERROR, 'error level message'
115 assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
117 msg = log_add Logger::WARN, 'warn level message'
118 assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
120 msg = log_add Logger::INFO, 'info level message'
121 assert_equal LEVEL_LABEL_MAP[Logger::INFO], msg.severity
123 msg = log_add Logger::DEBUG, 'debug level message'
124 assert_equal LEVEL_LABEL_MAP[Logger::DEBUG], msg.severity
127 def test_add_level_unknown
128 @logger.level = Logger::UNKNOWN
130 msg = log_add nil, 'unknown level message' # nil == unknown
131 assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
133 msg = log_add Logger::FATAL, 'fatal level message'
134 assert_equal '', msg.line
136 msg = log_add Logger::ERROR, 'error level message'
137 assert_equal '', msg.line
139 msg = log_add Logger::WARN, 'warn level message'
140 assert_equal '', msg.line
142 msg = log_add Logger::INFO, 'info level message'
143 assert_equal '', msg.line
145 msg = log_add Logger::DEBUG, 'debug level message'
146 assert_equal '', msg.line
149 def test_add_level_fatal
150 @logger.level = Logger::FATAL
152 msg = log_add nil, 'unknown level message' # nil == unknown
153 assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
155 msg = log_add Logger::FATAL, 'fatal level message'
156 assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
158 msg = log_add Logger::ERROR, 'error level message'
159 assert_equal '', msg.line
161 msg = log_add Logger::WARN, 'warn level message'
162 assert_equal '', msg.line
164 msg = log_add Logger::INFO, 'info level message'
165 assert_equal '', msg.line
167 msg = log_add Logger::DEBUG, 'debug level message'
168 assert_equal '', msg.line
171 def test_add_level_error
172 @logger.level = Logger::ERROR
174 msg = log_add nil, 'unknown level message' # nil == unknown
175 assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
177 msg = log_add Logger::FATAL, 'fatal level message'
178 assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
180 msg = log_add Logger::ERROR, 'error level message'
181 assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
183 msg = log_add Logger::WARN, 'warn level message'
184 assert_equal '', msg.line
186 msg = log_add Logger::INFO, 'info level message'
187 assert_equal '', msg.line
189 msg = log_add Logger::DEBUG, 'debug level message'
190 assert_equal '', msg.line
193 def test_add_level_warn
194 @logger.level = Logger::WARN
196 msg = log_add nil, 'unknown level message' # nil == unknown
197 assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
199 msg = log_add Logger::FATAL, 'fatal level message'
200 assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
202 msg = log_add Logger::ERROR, 'error level message'
203 assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
205 msg = log_add Logger::WARN, 'warn level message'
206 assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
208 msg = log_add Logger::INFO, 'info level message'
209 assert_equal '', msg.line
211 msg = log_add Logger::DEBUG, 'debug level message'
212 assert_equal '', msg.line
215 def test_add_level_info
216 @logger.level = Logger::INFO
218 msg = log_add nil, 'unknown level message' # nil == unknown
219 assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
221 msg = log_add Logger::FATAL, 'fatal level message'
222 assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
224 msg = log_add Logger::ERROR, 'error level message'
225 assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
227 msg = log_add Logger::WARN, 'warn level message'
228 assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
230 msg = log_add Logger::INFO, 'info level message'
231 assert_equal LEVEL_LABEL_MAP[Logger::INFO], msg.severity
233 msg = log_add Logger::DEBUG, 'debug level message'
234 assert_equal '', msg.line
237 def test_add_level_debug
238 @logger.level = Logger::DEBUG
240 msg = log_add nil, 'unknown level message' # nil == unknown
241 assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
243 msg = log_add Logger::FATAL, 'fatal level message'
244 assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
246 msg = log_add Logger::ERROR, 'error level message'
247 assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
249 msg = log_add Logger::WARN, 'warn level message'
250 assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
252 msg = log_add Logger::INFO, 'info level message'
253 assert_equal LEVEL_LABEL_MAP[Logger::INFO], msg.severity
255 msg = log_add Logger::DEBUG, 'debug level message'
256 assert_equal LEVEL_LABEL_MAP[Logger::DEBUG], msg.severity
260 msg = log :unknown, 'unknown level message'
261 assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
263 @logger.level = Logger::UNKNOWN
264 msg = log :unknown, 'unknown level message'
265 assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
267 @logger.level = Logger::FATAL
268 msg = log :unknown, 'unknown level message'
269 assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
271 @logger.level = Logger::ERROR
272 msg = log :unknown, 'unknown level message'
273 assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
275 @logger.level = Logger::WARN
276 msg = log :unknown, 'unknown level message'
277 assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
279 @logger.level = Logger::INFO
280 msg = log :unknown, 'unknown level message'
281 assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
283 @logger.level = Logger::DEBUG
284 msg = log :unknown, 'unknown level message'
285 assert_equal LEVEL_LABEL_MAP[Logger::UNKNOWN], msg.severity
289 msg = log :fatal, 'fatal level message'
290 assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
292 @logger.level = Logger::UNKNOWN
293 msg = log :fatal, 'fatal level message'
294 assert_equal '', msg.line
296 @logger.level = Logger::FATAL
297 msg = log :fatal, 'fatal level message'
298 assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
300 @logger.level = Logger::ERROR
301 msg = log :fatal, 'fatal level message'
302 assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
304 @logger.level = Logger::WARN
305 msg = log :fatal, 'fatal level message'
306 assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
308 @logger.level = Logger::INFO
309 msg = log :fatal, 'fatal level message'
310 assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
312 @logger.level = Logger::DEBUG
313 msg = log :fatal, 'fatal level message'
314 assert_equal LEVEL_LABEL_MAP[Logger::FATAL], msg.severity
318 @logger.level = Logger::FATAL
319 assert_equal true, @logger.fatal?
321 @logger.level = Logger::UNKNOWN
322 assert_equal false, @logger.fatal?
326 msg = log :error, 'error level message'
327 assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
329 @logger.level = Logger::UNKNOWN
330 msg = log :error, 'error level message'
331 assert_equal '', msg.line
333 @logger.level = Logger::FATAL
334 msg = log :error, 'error level message'
335 assert_equal '', msg.line
337 @logger.level = Logger::ERROR
338 msg = log :error, 'error level message'
339 assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
341 @logger.level = Logger::WARN
342 msg = log :error, 'error level message'
343 assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
345 @logger.level = Logger::INFO
346 msg = log :error, 'error level message'
347 assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
349 @logger.level = Logger::DEBUG
350 msg = log :error, 'error level message'
351 assert_equal LEVEL_LABEL_MAP[Logger::ERROR], msg.severity
355 @logger.level = Logger::ERROR
356 assert_equal true, @logger.error?
358 @logger.level = Logger::FATAL
359 assert_equal false, @logger.error?
363 msg = log :warn, 'warn level message'
364 assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
366 @logger.level = Logger::UNKNOWN
367 msg = log :warn, 'warn level message'
368 assert_equal '', msg.line
370 @logger.level = Logger::FATAL
371 msg = log :warn, 'warn level message'
372 assert_equal '', msg.line
374 @logger.level = Logger::ERROR
375 msg = log :warn, 'warn level message'
376 assert_equal '', msg.line
378 @logger.level = Logger::WARN
379 msg = log :warn, 'warn level message'
380 assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
382 @logger.level = Logger::INFO
383 msg = log :warn, 'warn level message'
384 assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
386 @logger.level = Logger::DEBUG
387 msg = log :warn, 'warn level message'
388 assert_equal LEVEL_LABEL_MAP[Logger::WARN], msg.severity
392 @logger.level = Logger::WARN
393 assert_equal true, @logger.warn?
395 @logger.level = Logger::ERROR
396 assert_equal false, @logger.warn?
400 msg = log :info, 'info level message'
401 assert_equal LEVEL_LABEL_MAP[Logger::INFO], msg.severity
403 @logger.level = Logger::UNKNOWN
404 msg = log :info, 'info level message'
405 assert_equal '', msg.line
407 @logger.level = Logger::FATAL
408 msg = log :info, 'info level message'
409 assert_equal '', msg.line
411 @logger.level = Logger::ERROR
412 msg = log :info, 'info level message'
413 assert_equal '', msg.line
415 @logger.level = Logger::WARN
416 msg = log :info, 'info level message'
417 assert_equal '', msg.line
419 @logger.level = Logger::INFO
420 msg = log :info, 'info level message'
421 assert_equal LEVEL_LABEL_MAP[Logger::INFO], msg.severity
423 @logger.level = Logger::DEBUG
424 msg = log :info, 'info level message'
425 assert_equal LEVEL_LABEL_MAP[Logger::INFO], msg.severity
429 @logger.level = Logger::INFO
430 assert_equal true, @logger.info?
432 @logger.level = Logger::WARN
433 assert_equal false, @logger.info?
437 msg = log :debug, 'debug level message'
438 assert_equal LEVEL_LABEL_MAP[Logger::DEBUG], msg.severity
440 @logger.level = Logger::UNKNOWN
441 msg = log :debug, 'debug level message'
442 assert_equal '', msg.line
444 @logger.level = Logger::FATAL
445 msg = log :debug, 'debug level message'
446 assert_equal '', msg.line
448 @logger.level = Logger::ERROR
449 msg = log :debug, 'debug level message'
450 assert_equal '', msg.line
452 @logger.level = Logger::WARN
453 msg = log :debug, 'debug level message'
454 assert_equal '', msg.line
456 @logger.level = Logger::INFO
457 msg = log :debug, 'debug level message'
458 assert_equal '', msg.line
460 @logger.level = Logger::DEBUG
461 msg = log :debug, 'debug level message'
462 assert_equal LEVEL_LABEL_MAP[Logger::DEBUG], msg.severity
466 @logger.level = Logger::DEBUG
467 assert_equal true, @logger.debug?
469 @logger.level = Logger::INFO
470 assert_equal false, @logger.debug?
473 end if defined?(Syslog)
475 class TestSyslogLogger < TestSyslogRootLogger
477 @facility = Syslog::LOG_USER
480 self.class.instance_variable_get("@facility")
485 @logger = Syslog::Logger.new
488 SEVERITY_MAP = {}.tap { |map|
489 level2severity = Syslog::Logger::LEVEL_MAP.invert
491 MockSyslog::LEVEL_LABEL_MAP.each { |level, name|
492 map[name] = TestSyslogRootLogger::LEVEL_LABEL_MAP[level2severity[level]]
497 attr_reader :line, :label, :datetime, :pid, :severity, :progname, :msg, :priority
500 return unless /\A<(\d+)> (\w+) - (.*)\Z/ =~ @line
501 priority, severity, @msg = $1, $2, $3
502 @severity = SEVERITY_MAP[severity]
503 @priority = priority.to_i
507 def log_add(severity, msg, progname = nil, &block)
508 log(:add, severity, msg, progname, &block)
511 def log(msg_id, *arg, &block)
512 Log.new(log_raw(msg_id, *arg, &block))
515 def log_raw(msg_id, *arg, &block)
516 assert_equal true, @logger.__send__(msg_id, *arg, &block)
517 msg = MockSyslog.line
523 @logger.level = Logger::UNKNOWN
524 assert_equal true, @logger.unknown?
526 @logger.level = Logger::UNKNOWN + 1
527 assert_equal false, @logger.unknown?
531 assert_equal facility, @logger.facility
535 msg = log_add nil, 'unknown level message' # nil == unknown
536 assert_equal facility|Syslog::LOG_ALERT, msg.priority
538 msg = log_add Logger::FATAL, 'fatal level message'
539 assert_equal facility|Syslog::LOG_ERR, msg.priority
541 msg = log_add Logger::ERROR, 'error level message'
542 assert_equal facility|Syslog::LOG_WARNING, msg.priority
544 msg = log_add Logger::WARN, 'warn level message'
545 assert_equal facility|Syslog::LOG_NOTICE, msg.priority
547 msg = log_add Logger::INFO, 'info level message'
548 assert_equal facility|Syslog::LOG_INFO, msg.priority
550 msg = log_add Logger::DEBUG, 'debug level message'
551 assert_equal facility|Syslog::LOG_DEBUG, msg.priority
554 class CustomSyslogLogger < Syslog::Logger
560 def test_overriding_level
561 @logger = CustomSyslogLogger.new
562 log = log_add Logger::INFO, 'msg'
563 assert_equal 'msg', log.msg
565 log = log_add Logger::DEBUG, 'msg'
569 end if defined?(Syslog)
572 # Create test class for each available facility
574 Syslog::Facility.constants.each do |facility_symb|
576 test_syslog_class = Class.new(TestSyslogLogger) do
578 @facility = Syslog.const_get(facility_symb)
582 @logger.facility = facility
586 Object.const_set("TestSyslogLogger_#{facility_symb}", test_syslog_class)
588 end if defined?(Syslog)