本文整理匯總了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