PageRenderTime 59ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/test/externals/ruby1.9/logger/test_logger.rb

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