logging_tests.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. """Tests for cement.ext.ext_logging."""
  2. import os
  3. import logging
  4. from tempfile import mkstemp
  5. from cement.core import handler, backend, log
  6. from cement.ext import ext_logging
  7. from cement.utils import test
  8. from cement.utils.misc import init_defaults, rando
  9. APP = rando()[:12]
  10. class MyLog(ext_logging.LoggingLogHandler):
  11. class Meta:
  12. label = 'mylog'
  13. level = 'INFO'
  14. def __init__(self, *args, **kw):
  15. super(MyLog, self).__init__(*args, **kw)
  16. @test.attr('core')
  17. class LoggingExtTestCase(test.CementExtTestCase):
  18. def test_alternate_namespaces(self):
  19. defaults = init_defaults(APP, 'log.logging')
  20. defaults['log.logging']['to_console'] = False
  21. defaults['log.logging']['file'] = '/dev/null'
  22. defaults['log.logging']['level'] = 'debug'
  23. app = self.make_app(config_defaults=defaults)
  24. app.setup()
  25. app.log.info('TEST', extra=dict(namespace=__name__))
  26. app.log.warn('TEST', extra=dict(namespace=__name__))
  27. app.log.error('TEST', extra=dict(namespace=__name__))
  28. app.log.fatal('TEST', extra=dict(namespace=__name__))
  29. app.log.debug('TEST', extra=dict(namespace=__name__))
  30. app.log.info('TEST', __name__, extra=dict(foo='bar'))
  31. app.log.warn('TEST', __name__, extra=dict(foo='bar'))
  32. app.log.error('TEST', __name__, extra=dict(foo='bar'))
  33. app.log.fatal('TEST', __name__, extra=dict(foo='bar'))
  34. app.log.debug('TEST', __name__, extra=dict(foo='bar'))
  35. app.log.info('TEST', __name__)
  36. app.log.warn('TEST', __name__)
  37. app.log.error('TEST', __name__)
  38. app.log.fatal('TEST', __name__)
  39. app.log.debug('TEST', __name__)
  40. def test_bad_level(self):
  41. defaults = init_defaults()
  42. defaults['log.logging'] = dict(
  43. level='BOGUS',
  44. to_console=False,
  45. )
  46. app = self.make_app(config_defaults=defaults)
  47. app.setup()
  48. self.eq(app.log.get_level(), 'INFO')
  49. def test_clear_loggers(self):
  50. self.app.setup()
  51. han = handler.get('log', 'logging')
  52. Log = han()
  53. Log.clear_loggers(self.app._meta.label)
  54. #previous_logger = logging.getLogger(name)
  55. MyLog = ext_logging.LoggingLogHandler(clear_loggers="%s:%s" %
  56. (self.app._meta.label,
  57. self.app._meta.label))
  58. MyLog._setup(self.app)
  59. def test_rotate(self):
  60. log_file = os.path.join(self.tmp_dir, '%s.log' % APP)
  61. defaults = init_defaults()
  62. defaults['log.logging'] = dict(
  63. file=log_file,
  64. level='DEBUG',
  65. rotate=True,
  66. max_bytes=10,
  67. max_files=2,
  68. )
  69. app = self.make_app(config_defaults=defaults)
  70. app.setup()
  71. app.log.info('test log message')
  72. app.log.info('test log message 2')
  73. app.log.info('test log message 3')
  74. app.log.info('test log message 4')
  75. # check that a second file was created, because this log is over 12
  76. # bytes.
  77. self.ok(os.path.exists("%s.1" % log_file))
  78. self.ok(os.path.exists("%s.2" % log_file))
  79. # this file should exist because of max files
  80. self.eq(os.path.exists("%s.3" % log_file), False)
  81. def test_missing_log_dir(self):
  82. _, tmp_path = mkstemp()
  83. if os.path.exists(tmp_path):
  84. os.remove(tmp_path)
  85. defaults = init_defaults()
  86. defaults['log.logging'] = dict(
  87. file=os.path.join(tmp_path, '%s.log' % APP),
  88. )
  89. app = self.make_app(config_defaults=defaults)
  90. app.setup()