def test_most_handlers(self): ah = logging.getLogger('').addHandler ah( logging.handlers.RotatingFileHandler('/bar/one.txt', maxBytes=10000, backupCount=3)) ah(logging.handlers.SocketHandler('server.example.com', 514)) ah(logging.handlers.DatagramHandler('server.example.com', 1958)) ah(logging.handlers.SysLogHandler()) ah( logging.handlers.SMTPHandler('mail.example.com', 'Server', 'Sysadmin', 'Logs!')) # ah(logging.handlers.NTEventLogHandler()) ah(logging.handlers.HTTPHandler('api.example.com', '/logs', 'POST')) ah(logging.handlers.BufferingHandler(20000)) sh = logging.StreamHandler() ah(logging.handlers.MemoryHandler(30000, target=sh)) self.assertEqual( build_description(), '''\ <--"" Level WARNING Handler RotatingFile '/bar/one.txt' maxBytes=10000 backupCount=3 Handler Socket server.example.com 514 Handler Datagram server.example.com 1958 Handler SysLog ('localhost', 514) facility=1 Handler SMTP via mail.example.com to ['Sysadmin'] Handler HTTP POST to http://api.example.com//logs Handler Buffering capacity=20000 Handler Memory capacity=30000 Flushes output to: Handler Stream %r ''' % (sh.stream, )) logging.getLogger('').handlers[3].socket.close() # or Python 3 warning
def test_handler_with_wrong_parent_attribute(self): logging.getLogger('celery') logging.getLogger('app.model') logging.getLogger('app.task').parent = logging.getLogger('celery.task') logging.getLogger('app.view') self.assertEqual( build_description(), '''\ <--"" Level WARNING | o<--[app] | | | o<--"app.model" | | Level NOTSET so inherits level WARNING | | | o !-"app.task" | | Broken .parent! Messages propagate to "celery.task" | | Level NOTSET so inherits level WARNING | | | o<--"app.view" | Level NOTSET so inherits level WARNING | o<--"celery" Level NOTSET so inherits level WARNING | o<--"celery.task" Level NOTSET so inherits level WARNING ''')
def test_nested_handlers(self): h1 = logging.StreamHandler() h2 = logging.handlers.MemoryHandler(30000, target=h1) h2.addFilter(logging.Filter('worse')) h2.setLevel(logging.ERROR) h3 = logging.handlers.MemoryHandler(30000, target=h2) h3.addFilter(logging.Filter('bad')) logging.getLogger('').addHandler(h3) self.assertEqual( build_description(), '''\ <--"" Level WARNING Handler Memory capacity=30000 Filter name='bad' Flushes output to: Handler Memory capacity=30000 Level ERROR Filter name='worse' Flushes output to: Handler Stream %r ''' % (h1.stream, ))
def test_most_handlers(self): ah = logging.getLogger('').addHandler ah(logging.handlers.RotatingFileHandler( '/bar/one.txt', maxBytes=10000, backupCount=3)) ah(logging.handlers.SocketHandler('server.example.com', 514)) ah(logging.handlers.DatagramHandler('server.example.com', 1958)) ah(logging.handlers.SysLogHandler()) ah(logging.handlers.SMTPHandler( 'mail.example.com', 'Server', 'Sysadmin', 'Logs!')) # ah(logging.handlers.NTEventLogHandler()) ah(logging.handlers.HTTPHandler('api.example.com', '/logs', 'POST')) ah(logging.handlers.BufferingHandler(20000)) sh = logging.StreamHandler() ah(logging.handlers.MemoryHandler(30000, target=sh)) self.assertEqual(build_description(), '''\ <--"" Level WARNING Handler RotatingFile '/bar/one.txt' maxBytes=10000 backupCount=3 Handler Socket server.example.com 514 Handler Datagram server.example.com 1958 Handler SysLog ('localhost', 514) facility=1 Handler SMTP via mail.example.com to ['Sysadmin'] Handler HTTP POST to http://api.example.com//logs Handler Buffering capacity=20000 Handler Memory capacity=30000 Flushes output to: Handler Stream %r ''' % (sh.stream,)) logging.getLogger('').handlers[3].socket.close() # or Python 3 warning
def test_setup_logging_dict(self, smtp_config_yaml): logging_config_dict = {'version': 1, 'handlers': { 'error_file_handler': { 'class': 'logging.FileHandler', 'level': 'ERROR', 'filename': 'errors.log', 'encoding': 'utf8', 'mode': 'w' }, 'error_mail_handler': { 'class': 'logging.handlers.SMTPHandler', 'level': 'CRITICAL', 'mailhost': 'localhost', 'fromaddr': 'noreply@localhost', 'toaddrs': '*****@*****.**', 'subject': 'SCRAPER FAILED' } }, 'root': { 'level': 'INFO', 'handlers': ['error_file_handler', 'error_mail_handler'] }} setup_logging(logging_config_dict=logging_config_dict) actual_logging_tree = format.build_description() for handler in ['File', 'SMTP']: assert handler in actual_logging_tree assert '*****@*****.**' in actual_logging_tree
def test_handler_with_wrong_parent_attribute(self): logging.getLogger('celery') logging.getLogger('app.model') logging.getLogger('app.task').parent = logging.getLogger('celery.task') logging.getLogger('app.view') self.assertEqual(build_description(), '''\ <--"" Level WARNING | o<--[app] | | | o<--"app.model" | | Level NOTSET so inherits level WARNING | | | o !-"app.task" | | Broken .parent! Messages propagate to "celery.task" | | Level NOTSET so inherits level WARNING | | | o<--"app.view" | Level NOTSET so inherits level WARNING | o<--"celery" Level NOTSET so inherits level WARNING | o<--"celery.task" Level NOTSET so inherits level WARNING ''')
def test_setup_logging_smtp_dict(self): smtp_config_dict = {'handlers': {'error_mail_handler': {'toaddrs': '*****@*****.**', 'subject': 'lala'}}} setup_logging(smtp_config_dict=smtp_config_dict) actual_logging_tree = format.build_description() for handler in ['Stream', 'File', 'SMTP']: assert handler in actual_logging_tree assert '*****@*****.**' in actual_logging_tree
def test_2_dot_6_handlers(self): if sys.version_info < (2, 6): return ah = logging.getLogger('').addHandler ah(logging.handlers.WatchedFileHandler('/bar/three.txt')) self.assertEqual(build_description(), '''\ <--"" Level WARNING Handler WatchedFile '/bar/three.txt' ''')
def test_2_dot_5_handlers(self): if sys.version_info < (2, 5): return ah = logging.getLogger('').addHandler ah(logging.handlers.TimedRotatingFileHandler('/bar/two.txt')) self.assertEqual(build_description(), '''\ <--"" Level WARNING Handler TimedRotatingFile '/bar/two.txt' when='H' interval=3600 backupCount=0 ''')
def test_handler_with_parent_attribute_that_is_none(self): logging.getLogger('app').parent = None self.assertEqual(build_description(), '''\ <--"" Level WARNING | o !-"app" Broken .parent is None, so messages stop here Level NOTSET so inherits level NOTSET ''')
def test_formatter_that_is_not_a_Formatter_instance(self): h = logging.StreamHandler() h.setFormatter("Ceci n'est pas une formatter") logging.getLogger('').addHandler(h) self.assertEqual(build_description(), '''\ <--"" Level WARNING Handler Stream %r Formatter "Ceci n'est pas une formatter" ''' % (h.stream,))
def test_formatter_with_no_fmt_attributes(self): f = logging.Formatter() del f._fmt del f.datefmt h = logging.StreamHandler() h.setFormatter(f) logging.getLogger('').addHandler(h) self.assertEqual(build_description(), '''\ <--"" Level WARNING Handler Stream %r Formatter fmt=None datefmt=None ''' % (h.stream,))
def test_fancy_tree(self): logging.getLogger('').setLevel(logging.DEBUG) log = logging.getLogger('db') log.setLevel(logging.INFO) log.propagate = False log.disabled = 1 log.addFilter(MyFilter()) handler = logging.StreamHandler() handler.setFormatter(logging.Formatter()) log.addHandler(handler) handler.addFilter(logging.Filter('db.errors')) logging.getLogger('db.errors') logging.getLogger('db.stats') log = logging.getLogger('www.status') log.setLevel(logging.DEBUG) log.addHandler(logging.FileHandler('/foo/log.txt')) log.addHandler(MyHandler()) self.assertEqual( build_description(), '''\ <--"" Level DEBUG | o "db" | Level INFO | Propagate OFF | Disabled | Filter <MyFilter> | Handler Stream %r | Filter name='db.errors' | Formatter fmt='%%(message)s' datefmt=None | | | o<--"db.errors" | | Level NOTSET so inherits level INFO | | | o<--"db.stats" | Level NOTSET so inherits level INFO | o<--[www] | o<--"www.status" Level DEBUG Handler File '/foo/log.txt' Handler <MyHandler> ''' % (sys.stderr, ))
def test_fancy_tree(self): logging.getLogger('').setLevel(logging.DEBUG) log = logging.getLogger('db') log.setLevel(logging.INFO) log.propagate = False log.disabled = 1 log.addFilter(MyFilter()) handler = logging.StreamHandler() handler.setFormatter(logging.Formatter()) log.addHandler(handler) handler.addFilter(logging.Filter('db.errors')) logging.getLogger('db.errors') logging.getLogger('db.stats') log = logging.getLogger('www.status') log.setLevel(logging.DEBUG) log.addHandler(logging.FileHandler('/foo/log.txt')) log.addHandler(MyHandler()) self.assertEqual(build_description(), '''\ <--"" Level DEBUG | o "db" | Level INFO | Propagate OFF | Disabled | Filter <MyFilter> | Handler Stream %r | Filter name='db.errors' | Formatter fmt='%%(message)s' datefmt=None | | | o<--"db.errors" | | Level NOTSET so inherits level INFO | | | o<--"db.stats" | Level NOTSET so inherits level INFO | o<--[www] | o<--"www.status" Level DEBUG Handler File '/foo/log.txt' Handler <MyHandler> ''' % (sys.stderr,))
def test_simple_tree(self): logging.getLogger('a') logging.getLogger('a.b').setLevel(logging.DEBUG) logging.getLogger('x.c') self.assertEqual(build_description(), '''\ <--"" Level WARNING | o<--"a" | | | o<--"a.b" | Level DEBUG | o<--[x] | o<--"x.c" ''')
def test_fancy_tree(self): logging.getLogger('').setLevel(logging.DEBUG) log = logging.getLogger('db') log.setLevel(logging.INFO) log.propagate = False log.addFilter(MyFilter()) handler = logging.StreamHandler() log.addHandler(handler) handler.addFilter(logging.Filter('db.errors')) logging.getLogger('db.errors') logging.getLogger('db.stats') log = logging.getLogger('www.status') log.setLevel(logging.DEBUG) log.addHandler(logging.FileHandler('/foo/log.txt')) log.addHandler(MyHandler()) self.assertEqual(build_description(), '''\ <--"" Level DEBUG | o "db" | Level INFO | Propagate OFF | Filter <MyFilter> | Handler Stream %r | Filter name='db.errors' | | | o<--"db.errors" | | | o<--"db.stats" | o<--[www] | o<--"www.status" Level DEBUG Handler File '/foo/log.txt' Handler <MyHandler> ''' % (sys.stderr,))
def test_nested_handlers(self): h1 = logging.StreamHandler() h2 = logging.handlers.MemoryHandler(30000, target=h1) h2.addFilter(logging.Filter('worse')) h3 = logging.handlers.MemoryHandler(30000, target=h2) h3.addFilter(logging.Filter('bad')) logging.getLogger('').addHandler(h3) self.assertEqual(build_description(), '''\ <--"" Level WARNING Handler Memory capacity=30000 dumping to: Filter name='bad' Handler Memory capacity=30000 dumping to: Filter name='worse' Handler Stream %r ''' % (h1.stream,))
def test_2_dot_5_handlers(self): if sys.version_info < (2, 5): return ah = logging.getLogger('').addHandler ah(logging.handlers.TimedRotatingFileHandler('/bar/two.txt')) expected = '''\ <--"" Level WARNING Handler TimedRotatingFile '/bar/two.txt' when='H' interval=3600 backupCount=0 ''' if sys.version_info >= (3, 8): # Apparently the design of the TimedRotatingFileHandler has # become a bit more ambitious as of Python 3.8. expected += '''\ | o<--"asyncio" | Level NOTSET so inherits level WARNING | o<--[concurrent] | o<--"concurrent.futures" Level NOTSET so inherits level WARNING ''' self.assertEqual(build_description(), expected)
def test_setup_logging_json(self, logging_config_json): setup_logging(logging_config_json=logging_config_json) actual_logging_tree = format.build_description() for handler in ['Stream', 'File']: assert handler in actual_logging_tree
def test_setup_logging_smtp_json(self, smtp_config_json): setup_logging(smtp_config_json=smtp_config_json) actual_logging_tree = format.build_description() for handler in ['Stream', 'File', 'SMTP']: assert handler in actual_logging_tree assert '*****@*****.**' in actual_logging_tree
def test_setup_logging_yaml(self, logging_config_yaml): setup_logging(logging_config_yaml=logging_config_yaml) actual_logging_tree = format.build_description() for handler in ['Stream', 'SMTP']: assert handler in actual_logging_tree assert '*****@*****.**' in actual_logging_tree
def log_tree(request): return Response(build_description())