Python logging 模块,logThreads() 实例源码
我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用logging.logThreads()。
def test_optional(self):
r = logging.makeLogRecord({})
NOT_NONE = self.assertIsNotNone
if threading:
NOT_NONE(r.thread)
NOT_NONE(r.threadName)
NOT_NONE(r.process)
NOT_NONE(r.processName)
log_threads = logging.logThreads
log_processes = logging.logProcesses
log_multiprocessing = logging.logMultiprocessing
try:
logging.logThreads = False
logging.logProcesses = False
logging.logMultiprocessing = False
r = logging.makeLogRecord({})
NONE = self.assertIsNone
NONE(r.thread)
NONE(r.threadName)
NONE(r.process)
NONE(r.processName)
finally:
logging.logThreads = log_threads
logging.logProcesses = log_processes
logging.logMultiprocessing = log_multiprocessing
def test_optional(self):
r = logging.makeLogRecord({})
NOT_NONE = self.assertIsNotNone
if threading:
NOT_NONE(r.thread)
NOT_NONE(r.threadName)
NOT_NONE(r.process)
NOT_NONE(r.processName)
log_threads = logging.logThreads
log_processes = logging.logProcesses
log_multiprocessing = logging.logMultiprocessing
try:
logging.logThreads = False
logging.logProcesses = False
logging.logMultiprocessing = False
r = logging.makeLogRecord({})
NONE = self.assertIsNone
NONE(r.thread)
NONE(r.threadName)
NONE(r.process)
NONE(r.processName)
finally:
logging.logThreads = log_threads
logging.logProcesses = log_processes
logging.logMultiprocessing = log_multiprocessing
def config_logging(level=LEVEL, prefix=PREFIX, other_loggers=None):
# Little speed up
if "%(process)d" not in prefix:
logging.logProcesses = False
if "%(processName)s" not in prefix:
logging.logMultiprocessing = False
if "%(thread)d" not in prefix and "%(threadName)s" not in prefix:
logging.logThreads = False
handler = logging.StreamHandler()
handler.setFormatter(LogFormatter())
loggers = [logging.getLogger('pyftpdlib')]
if other_loggers is not None:
loggers.extend(other_loggers)
for logger in loggers:
logger.setLevel(level)
logger.addHandler(handler)
def test_optional(self):
r = logging.makeLogRecord({})
NOT_NONE = self.assertIsNotNone
if threading:
NOT_NONE(r.thread)
NOT_NONE(r.threadName)
NOT_NONE(r.process)
NOT_NONE(r.processName)
log_threads = logging.logThreads
log_processes = logging.logProcesses
log_multiprocessing = logging.logMultiprocessing
try:
logging.logThreads = False
logging.logProcesses = False
logging.logMultiprocessing = False
r = logging.makeLogRecord({})
NONE = self.assertIsNone
NONE(r.thread)
NONE(r.threadName)
NONE(r.process)
NONE(r.processName)
finally:
logging.logThreads = log_threads
logging.logProcesses = log_processes
logging.logMultiprocessing = log_multiprocessing
def configlog4download(logger, db_session, download_id, isterminal):
"""configs for download and returns the handler used to store the log to the db
and to a tmp file. The file is accessible via logger..baseFilename
"""
# https://docs.python.org/2/howto/logging.html#optimization:
logging._srcfile = None
logging.logThreads = 0
logging.logProcesses = 0
# FIXME above: move elsewhere (maybe restoring defaults?)
logger.setLevel(logging.INFO) # necessary to forward to handlers
# custom StreamHandler: count errors and warnings:
dbstream_handler = DbStreamHandler(db_session, download_id)
logger.addHandler(dbstream_handler)
if isterminal:
# configure print to stdout (by default only info and critical messages)
logger.addHandler(SysOutStreamHandler(sys.stdout))
return dbstream_handler
# def configlog4stdout(logger):
# logger.setLevel(logging.INFO) # necessary to forward to handlers
# # configure print to stdout (by default only info and critical messages):
# logger.addHandler(SysOutStreamHandler(sys.stdout))
def setup(mute_stdout=False):
# logging.basicConfig()
if mute_stdout:
handler = logging.NullHandler()
else:
formatter_str = '%(asctime)s %(levelname)s %(message)s'
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter(formatter_str))
# Add handler to logger
logger = logging.getLogger(_product_name)
logger.addHandler(handler)
# disable unnecessary information capture
logging.logThreads = 0
logging.logProcesses = 0
# to make sure each log record does not have a source file name attached
# pylint: disable=protected-access
logging._srcfile = None
# pylint: enable=protected-access
def create_logger(level=logging.NOTSET):
"""Create a logger for python-gnupg at a specific message level.
:type level: :obj:`int` or :obj:`str`
:param level: A string or an integer for the lowest level to include in
logs.
**Available levels:**
==== ======== ========================================
int str description
==== ======== ========================================
0 NOTSET Disable all logging.
9 GNUPG Log GnuPG's internal status messages.
10 DEBUG Log module level debuging messages.
20 INFO Normal user-level messages.
30 WARN Warning messages.
40 ERROR Error messages and tracebacks.
50 CRITICAL Unhandled exceptions and tracebacks.
==== ======== ========================================
"""
_test = os.path.join(os.path.join(os.getcwd(), 'gnupg'), 'test')
_now = datetime.now().strftime("%Y-%m-%d_%H%M%S")
_fn = os.path.join(_test, "%s_test_gnupg.log" % _now)
_fmt = "%(relativeCreated)-4d L%(lineno)-4d:%(funcName)-18.18s %(levelname)-7.7s %(message)s"
## Add the GNUPG_STATUS_LEVEL LogRecord to all Loggers in the module:
logging.addLevelName(GNUPG_STATUS_LEVEL, "GNUPG")
logging.Logger.status = status
if level > logging.NOTSET:
logging.basicConfig(level=level, filename=_fn,
filemode="a", format=_fmt)
logging.logThreads = True
if hasattr(logging,'captureWarnings'):
logging.captureWarnings(True)
colouriser = _ansistrm.ColorizingStreamHandler
colouriser.level_map[9] = (None, 'blue', False)
colouriser.level_map[10] = (None, 'cyan', False)
handler = colouriser(sys.stderr)
handler.setLevel(level)
formatr = logging.Formatter(_fmt)
handler.setFormatter(formatr)
else:
handler = NullHandler()
log = logging.getLogger('gnupg')
log.addHandler(handler)
log.setLevel(level)
log.info("Log opened: %s UTC" % datetime.ctime(datetime.utcnow()))
return log