本文整理汇总了Python中logging.NullHandler方法的典型用法代码示例。如果您正苦于以下问题:Python logging.NullHandler方法的具体用法?Python logging.NullHandler怎么用?Python logging.NullHandler使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类logging
的用法示例。
在下文中一共展示了logging.NullHandler方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setup_logger
# 需要导入模块: import logging [as 别名]
# 或者: from logging import NullHandler [as 别名]
def setup_logger(log_comp, handler, level):
"""
Setup the logger for the specified log component to add the specified
handler (removing a possibly present NullHandler) and to set it to the
specified log level. The handler is also set to the specified log level
because the default level of a handler is 0 which causes it to process all
levels.
"""
name = LOGGER_NAMES[log_comp]
logger = logging.getLogger(name)
for h in logger.handlers:
if isinstance(h, logging.NullHandler):
logger.removeHandler(h)
handler.setLevel(level)
logger.addHandler(handler)
logger.setLevel(level)
示例2: test_is_tracing
# 需要导入模块: import logging [as 别名]
# 或者: from logging import NullHandler [as 别名]
def test_is_tracing(self):
logger = log.get_logger()
original_handlers = logger.handlers
logger.handlers = [log._NullHandler()]
try:
self.assertFalse(log.is_tracing())
handler = logging.NullHandler()
handler.setLevel(log.DEBUG)
logger.addHandler(handler)
self.assertFalse(log.is_tracing())
handler = logging.NullHandler()
handler.setLevel(log.TRACE)
logger.addHandler(handler)
self.assertTrue(log.is_tracing())
finally:
logger.handlers = original_handlers
示例3: adapt_logger
# 需要导入模块: import logging [as 别名]
# 或者: from logging import NullHandler [as 别名]
def adapt_logger(logger):
"""
Adapt our custom logger.BaseLogger object into a standard logging.Logger object.
Adaptations are:
- NoOpLogger turns into a logger with a single NullHandler.
- SimpleLogger turns into a logger with a StreamHandler and level.
Args:
logger: Possibly a logger.BaseLogger, or a standard python logging.Logger.
Returns: a standard python logging.Logger.
"""
if isinstance(logger, logging.Logger):
return logger
# Use the standard python logger created by these classes.
if isinstance(logger, (SimpleLogger, NoOpLogger)):
return logger.logger
# Otherwise, return whatever we were given because we can't adapt.
return logger
示例4: test_adapt_logger__noop
# 需要导入模块: import logging [as 别名]
# 或者: from logging import NullHandler [as 别名]
def test_adapt_logger__noop(self):
"""Test that adapt_logger returns a standard python logger from a NoOpLogger."""
noop_logger = _logger.NoOpLogger()
standard_logger = _logger.adapt_logger(noop_logger)
# adapt_logger knows about the loggers attached to this class.
self.assertIs(noop_logger.logger, standard_logger)
# Verify properties of the logger
self.assertIsInstance(standard_logger, logging.Logger)
self.assertEqual('optimizely.logger.NoOpLogger', standard_logger.name)
self.assertEqual(logging.NOTSET, standard_logger.level)
# Should have a single NullHandler (with a default formatter).
self.assertEqual(1, len(standard_logger.handlers))
handler = standard_logger.handlers[0]
self.assertIsInstance(handler, logging.NullHandler)
self.assertEqual(
'%(levelname)-8s %(asctime)s %(filename)s:%(lineno)s:%(message)s', handler.formatter._fmt,
)
示例5: _silence_loggers
# 需要导入模块: import logging [as 别名]
# 或者: from logging import NullHandler [as 别名]
def _silence_loggers(self):
nologs = ["deconvolution_scan_processor"]
if not self.deconvolute:
nologs.append("deconvolution")
debug_mode = os.getenv("MS_DEISOTOPE_DEBUG")
if debug_mode:
handler = logging.FileHandler("ms-deisotope-deconvolution-debug-%s.log" % (os.getpid()), 'w')
fmt = logging.Formatter(
"%(asctime)s - %(name)s:%(filename)s:%(lineno)-4d - %(levelname)s - %(message)s",
"%H:%M:%S")
handler.setFormatter(fmt)
for logname in nologs:
logger_to_silence = logging.getLogger(logname)
if debug_mode:
logger_to_silence.setLevel("DEBUG")
logger_to_silence.addHandler(handler)
else:
logger_to_silence.propagate = False
logger_to_silence.setLevel("CRITICAL")
logger_to_silence.addHandler(logging.NullHandler())
示例6: format_ldap_message
# 需要导入模块: import logging [as 别名]
# 或者: from logging import NullHandler [as 别名]
def format_ldap_message(message, prefix):
if isinstance(message, LDAPMessage):
try: # pyasn1 prettyprint raises exception in version 0.4.3
formatted = message.prettyPrint().split('\n') # pyasn1 pretty print
except Exception as e:
formatted = ['pyasn1 exception', str(e)]
else:
formatted = pformat(message).split('\n')
prefixed = ''
for line in formatted:
if line:
if _hide_sensitive_data and line.strip().lower().startswith(_sensitive_lines): # _sensitive_lines is a tuple. startswith() method checks each tuple element
tag, _, data = line.partition('=')
if data.startswith("b'") and data.endswith("'") or data.startswith('b"') and data.endswith('"'):
prefixed += '\n' + prefix + tag + '=<stripped %d characters of sensitive data>' % (len(data) - 3, )
else:
prefixed += '\n' + prefix + tag + '=<stripped %d characters of sensitive data>' % len(data)
else:
prefixed += '\n' + prefix + line
return prefixed
# sets a logger for the library with NullHandler. It can be used by the application with its own logging configuration
示例7: pytest_configure
# 需要导入模块: import logging [as 别名]
# 或者: from logging import NullHandler [as 别名]
def pytest_configure(config):
if config.option.verbose > 1:
root = logging.getLogger()
if not root.handlers:
root.addHandler(logging.NullHandler())
logging.getLogger("testinfra").setLevel(logging.DEBUG)
if config.option.nagios:
# disable & re-enable terminalreporter to write in a tempfile
reporter = config.pluginmanager.getplugin('terminalreporter')
if reporter:
out = SpooledTemporaryFile(encoding=sys.stdout.encoding)
config.pluginmanager.unregister(reporter)
reporter = reporter.__class__(config, out)
config.pluginmanager.register(reporter, 'terminalreporter')
config.pluginmanager.register(NagiosReporter(out),
'nagiosreporter')
示例8: _setup_console_log
# 需要导入模块: import logging [as 别名]
# 或者: from logging import NullHandler [as 别名]
def _setup_console_log(self):
"""Add a console log handler."""
namespace = self._meta.namespace
to_console = self.app.config.get(self._meta.config_section,
'to_console')
if is_true(to_console):
console_handler = logging.StreamHandler()
format = self._get_console_format()
formatter = self._get_console_formatter(format)
console_handler.setFormatter(formatter)
console_handler.setLevel(getattr(logging, self.get_level()))
else:
console_handler = NullHandler()
# FIXME: self._clear_loggers() should be preventing this but its not!
for i in logging.getLogger("cement:app:%s" % namespace).handlers:
if isinstance(i, logging.StreamHandler):
self.backend.removeHandler(i)
self.backend.addHandler(console_handler)
示例9: create_logger
# 需要导入模块: import logging [as 别名]
# 或者: from logging import NullHandler [as 别名]
def create_logger(debug_filename, logger_name, enabled=False):
"""Creates a logger format for error logging
:debug_filename: path and filename of the debug log
:logger_name: name of the logger to create
:enabled: enable to write the log to provided filename, otherwise uses a NullHandler
"""
logger = logging.getLogger(logger_name)
logger.setLevel(logging.DEBUG)
logger.propagate = False
formatter = logging.Formatter(fmt='%(asctime)s %(levelname)s:%(name)s:%(funcName)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
if enabled:
# DEBUG log to 'kodi-addon-checker.log'
debug_log_handler = logging.handlers.RotatingFileHandler(debug_filename, encoding='utf-8', mode="w")
debug_log_handler.setLevel(logging.DEBUG)
debug_log_handler.setFormatter(formatter)
logger.addHandler(debug_log_handler)
else:
logger.addHandler(logging.NullHandler())
return logger
示例10: test_exception
# 需要导入模块: import logging [as 别名]
# 或者: from logging import NullHandler [as 别名]
def test_exception(self):
# Add a handler, or python complains "no handler assigned
# to...."
jl = logging.getLogger('pykit.jobq')
jl.addHandler(logging.NullHandler())
def err_on_even(args):
if args % 2 == 0:
raise Exception('even number')
else:
return args
def collect(args):
rst.append(args)
rst = []
jobq.run(range(10), [err_on_even, collect])
self.assertEqual(list(range(1, 10, 2)), rst)
# remove NullHandler
jl.handlers = []
示例11: init_app
# 需要导入模块: import logging [as 别名]
# 或者: from logging import NullHandler [as 别名]
def init_app(self, app):
"""
Common Flask interface to initialize the logging according to the
application configuration.
"""
# We don't need the default Flask's loggers when using our invoke tasks
# since we set up beautiful colorful loggers globally.
for handler in list(app.logger.handlers):
app.logger.removeHandler(handler)
app.logger.propagate = True
if app.debug:
logging.getLogger('flask_oauthlib').setLevel(logging.DEBUG)
app.logger.setLevel(logging.DEBUG)
# We don't need the default SQLAlchemy loggers when using our invoke
# tasks since we set up beautiful colorful loggers globally.
# NOTE: This particular workaround is for the SQLALCHEMY_ECHO mode,
# when all SQL commands get printed (without these lines, they will get
# printed twice).
sqla_logger = logging.getLogger('sqlalchemy.engine.base.Engine')
for hdlr in list(sqla_logger.handlers):
sqla_logger.removeHandler(hdlr)
sqla_logger.addHandler(logging.NullHandler())
示例12: __init__
# 需要导入模块: import logging [as 别名]
# 或者: from logging import NullHandler [as 别名]
def __init__(self, n_topics, n_iter=2000, alpha=0.01, eta=0.01, random_state=None,
refresh=10):
self.n_topics = n_topics
self.n_iter = n_iter
self.alpha = alpha
self.eta = eta
# if random_state is None, check_random_state(None) does nothing
# other than return the current numpy RandomState
self.random_state = random_state
self.refresh = refresh
if alpha <= 0 or eta <= 0:
raise ValueError("alpha and eta must be greater than zero")
# random numbers that are reused
rng = guidedlda.utils.check_random_state(random_state)
if random_state:
random.seed(random_state)
self._rands = rng.rand(1024**2 // 8) # 1MiB of random variates
# configure console logging if not already configured
if len(logger.handlers) == 1 and isinstance(logger.handlers[0], logging.NullHandler):
logging.basicConfig(level=logging.INFO)
示例13: setup_files
# 需要导入模块: import logging [as 别名]
# 或者: from logging import NullHandler [as 别名]
def setup_files(self):
self.pidfile = os.path.abspath(self.pidfile)
if self.logfile is not None:
self.logdaemon = logging.getLogger('logdaemon')
self.logdaemon.setLevel(self.loglevel)
filehandler = logging.FileHandler(self.logfile)
filehandler.setLevel(self.loglevel)
formatter = logging.Formatter(fmt = '[%(asctime)s] [%(levelname)8s] --- %(message)s',
datefmt = '%Y-%m-%d %H:%M:%S')
filehandler.setFormatter(formatter)
self.logdaemon.addHandler(filehandler)
else:
nullhandler = logging.NullHandler()
self.logdaemon.addHandler(nullhandler)
示例14: __init__
# 需要导入模块: import logging [as 别名]
# 或者: from logging import NullHandler [as 别名]
def __init__(self, process_function):
self._event_handlers = defaultdict(list)
self._logger = logging.getLogger(__name__ + "." + self.__class__.__name__)
self._logger.addHandler(logging.NullHandler())
self._process_function = process_function
self.should_terminate = False
self.should_terminate_single_epoch = False
self.state = None
self._allowed_events = []
self.register_events(*Events)
if self._process_function is None:
raise ValueError("Engine must be given a processing function in order to run.")
self._check_signature(process_function, 'process_function', None)
示例15: __init__
# 需要导入模块: import logging [as 别名]
# 或者: from logging import NullHandler [as 别名]
def __init__(self, patience, score_function, trainer):
if not callable(score_function):
raise TypeError("Argument score_function should be a function.")
if patience < 1:
raise ValueError("Argument patience should be positive integer.")
if not isinstance(trainer, Engine):
raise TypeError("Argument trainer should be an instance of Engine.")
self.score_function = score_function
self.patience = patience
self.trainer = trainer
self.counter = 0
self.best_score = None
self._logger = logging.getLogger(__name__ + "." + self.__class__.__name__)
self._logger.addHandler(logging.NullHandler())