本文整理汇总了Python中logging.lastResort方法的典型用法代码示例。如果您正苦于以下问题:Python logging.lastResort方法的具体用法?Python logging.lastResort怎么用?Python logging.lastResort使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类logging
的用法示例。
在下文中一共展示了logging.lastResort方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: callHandlers
# 需要导入模块: import logging [as 别名]
# 或者: from logging import lastResort [as 别名]
def callHandlers(self, record):
# this is the same as Python 3.5's logging.Logger.callHandlers
c = self
found = 0
while c:
for hdlr in c.handlers:
found = found + 1
if record.levelno >= hdlr.level:
hdlr.handle(record)
if not c.propagate:
c = None # break out
else:
c = c.parent
if (found == 0):
if logging.lastResort:
if record.levelno >= logging.lastResort.level:
logging.lastResort.handle(record)
elif logging.raiseExceptions and not self.manager.emittedNoHandlerWarning:
sys.stderr.write("No handlers could be found for logger"
" \"%s\"\n" % self.name)
self.manager.emittedNoHandlerWarning = True
示例2: test_last_resort
# 需要导入模块: import logging [as 别名]
# 或者: from logging import lastResort [as 别名]
def test_last_resort(self):
# Test the last resort handler
root = self.root_logger
root.removeHandler(self.root_hdlr)
old_lastresort = logging.lastResort
old_raise_exceptions = logging.raiseExceptions
try:
with support.captured_stderr() as stderr:
root.debug('This should not appear')
self.assertEqual(stderr.getvalue(), '')
root.warning('Final chance!')
self.assertEqual(stderr.getvalue(), 'Final chance!\n')
# No handlers and no last resort, so 'No handlers' message
logging.lastResort = None
with support.captured_stderr() as stderr:
root.warning('Final chance!')
msg = 'No handlers could be found for logger "root"\n'
self.assertEqual(stderr.getvalue(), msg)
# 'No handlers' message only printed once
with support.captured_stderr() as stderr:
root.warning('Final chance!')
self.assertEqual(stderr.getvalue(), '')
# If raiseExceptions is False, no message is printed
root.manager.emittedNoHandlerWarning = False
logging.raiseExceptions = False
with support.captured_stderr() as stderr:
root.warning('Final chance!')
self.assertEqual(stderr.getvalue(), '')
finally:
root.addHandler(self.root_hdlr)
logging.lastResort = old_lastresort
logging.raiseExceptions = old_raise_exceptions
示例3: test_last_resort
# 需要导入模块: import logging [as 别名]
# 或者: from logging import lastResort [as 别名]
def test_last_resort(self):
# Test the last resort handler
root = self.root_logger
root.removeHandler(self.root_hdlr)
old_stderr = sys.stderr
old_lastresort = logging.lastResort
old_raise_exceptions = logging.raiseExceptions
try:
sys.stderr = sio = io.StringIO()
root.debug('This should not appear')
self.assertEqual(sio.getvalue(), '')
root.warning('This is your final chance!')
self.assertEqual(sio.getvalue(), 'This is your final chance!\n')
#No handlers and no last resort, so 'No handlers' message
logging.lastResort = None
sys.stderr = sio = io.StringIO()
root.warning('This is your final chance!')
self.assertEqual(sio.getvalue(), 'No handlers could be found for logger "root"\n')
# 'No handlers' message only printed once
sys.stderr = sio = io.StringIO()
root.warning('This is your final chance!')
self.assertEqual(sio.getvalue(), '')
root.manager.emittedNoHandlerWarning = False
#If raiseExceptions is False, no message is printed
logging.raiseExceptions = False
sys.stderr = sio = io.StringIO()
root.warning('This is your final chance!')
self.assertEqual(sio.getvalue(), '')
finally:
sys.stderr = old_stderr
root.addHandler(self.root_hdlr)
logging.lastResort = old_lastresort
logging.raiseExceptions = old_raise_exceptions
示例4: configure_standard_logging
# 需要导入模块: import logging [as 别名]
# 或者: from logging import lastResort [as 别名]
def configure_standard_logging(verbosity: int, mode: LoggingMode):
"""Configure the standard library's `logging` module.
Get `logging` working with options consistent with Twisted. NOTE CAREFULLY
that `django.utils.log.DEFAULT_LOGGING` may have been applied (though only
if installed and if configured in this environment). Those settings and
the settings this function applies must be mentally combined to understand
the resultant behaviour.
:param verbosity: See `get_logging_level`.
:param mode: The mode in which to configure logging. See `LoggingMode`.
"""
set_standard_verbosity(verbosity)
# Make sure that `logging` is not configured to capture warnings.
logging.captureWarnings(False)
# If a logger is ever configured `propagate=False` but without handlers
# `logging.Logger.callHandlers` will employ the `lastResort` handler in
# order that the log is not lost. This goes to standard error by default.
# Here we arrange for these situations to be logged more distinctively so
# that they're easier to diagnose.
logging.lastResort = logging.StreamHandler(
twistedModern.LoggingFile(
logger=twistedModern.Logger("lost+found"),
level=twistedModern.LogLevel.error,
)
)
示例5: revision_call_handlers
# 需要导入模块: import logging [as 别名]
# 或者: from logging import lastResort [as 别名]
def revision_call_handlers(self, record): # 对logging标准模块打猴子补丁。主要是使父命名空间的handler不重复记录当前命名空间日志已有种类的handler。
"""
重要。这可以使同名logger或父logger随意添加同种类型的handler,确保不会重复打印。
:param self:
:param record:
:return:
"""
"""
Pass a record to all relevant handlers.
Loop through all handlers for this logger and its parents in the
logger hierarchy. If no handler was found, output a one-off error
message to sys.stderr. Stop searching up the hierarchy whenever a
logger with the "propagate" attribute set to zero is found - that
will be the last logger whose handlers are called.
"""
c = self
found = 0
hdlr_type_set = set()
while c:
for hdlr in c.handlers:
hdlr_type = type(hdlr)
if hdlr_type == logging.StreamHandler: # REMIND 因为很多handler都是继承自StreamHandler,包括filehandler,直接判断会逻辑出错。
hdlr_type = ColorHandler
found = found + 1
if record.levelno >= hdlr.level:
if hdlr_type not in hdlr_type_set:
hdlr.handle(record)
hdlr_type_set.add(hdlr_type)
if not c.propagate:
c = None # break out
else:
c = c.parent
# noinspection PyRedundantParentheses
if (found == 0):
if logging.lastResort:
if record.levelno >= logging.lastResort.level:
logging.lastResort.handle(record)
elif logging.raiseExceptions and not self.manager.emittedNoHandlerWarning:
sys.stderr.write("No handlers could be found for logger"
" \"%s\"\n" % self.name)
self.manager.emittedNoHandlerWarning = True
# noinspection PyProtectedMember