python [Python3日志记录]简单的Python3日志记录配置#tags:logs,python3

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python [Python3日志记录]简单的Python3日志记录配置#tags:logs,python3相关的知识,希望对你有一定的参考价值。

import logging
import structlog

# create logger
logger = logging.getLogger('simple_example')
logger.setLevel(logging.DEBUG)

# We need factory, to return application-wide logger
def logger_factory():
    return logger

structlog.configure(
    processors=[
            structlog.processors.JSONRenderer(indent=2, sort_keys=True)      
        ],
        logger_factory=logger_factory
    )

log = structlog.getLogger()
log.debug("Now you see me")
logger.setLevel(logging.ERROR)
log.debug("Now you don't")
# Keep logs quiet (so only critical messages are shown, not INFO messages)
logging.getLogger("boto").setLevel(logging.CRITICAL)
logging.getLogger("nsq").setLevel(logging.CRITICAL)
# https://docs.python.org/3/library/logging.html#logrecord-attributes

import logging
log = logging.getLogger()  # <logging.RootLogger at 0x107f72f98>
log.setLevel(logging.DEBUG)
log.debug('123')  # DEBUG:root:123
log.info('456')  # INFO:root:456

# Alternatively...
import logging
logging.basicConfig(level=logging.DEBUG, format='%(relativeCreated)6d %(threadName)s %(message)s')

# Something I like...
import logging
logging.basicConfig(
    level=logging.INFO,
    format='[%(levelname)s %(asctime)s path:%(pathname)s lineno:%(lineno)s] %(message)s',
    datefmt='%Y/%m/%d %I:%M:%S'
)

以上是关于python [Python3日志记录]简单的Python3日志记录配置#tags:logs,python3的主要内容,如果未能解决你的问题,请参考以下文章

bot删除消息Discord.py python 3.9.2时的日志记录问题

python3-flask-3将信息写入日志

Python中logging日志使用

python3 logging模块

python3 logging模块

Python3之logging模块浅析