Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Programing Python logging #97

Open
junxnone opened this issue May 26, 2020 · 1 comment
Open

Programing Python logging #97

junxnone opened this issue May 26, 2020 · 1 comment

Comments

@junxnone
Copy link
Owner

junxnone commented May 26, 2020

Logging

  • python 自带的 log 管理工具
    • 打印
    • 输出到文件
    • 输出格式控制
    • Log Level 控制

Log Level 定义

Level Description
DEBUG 详细信息,典型地调试问题时会感兴趣。 详细的debug信息。
INFO 证明事情按预期工作。 关键事件。
WARNING 表明发生了一些意外,或者不久的将来会发生问题(如‘磁盘满了’)。软件还是在正常工作。
NOTICE 不是错误,但是可能需要处理。普通但是重要的事件。
ERROR 由于更严重的问题,软件已不能执行一些功能了。 一般错误消息。
CRITICAL 严重错误,表明软件已不能继续运行了。
ALERT 需要立即修复,例如系统数据库损坏。
EMERGENCY 紧急情况,系统不可用(例如系统崩溃),一般会通知所有用户。

UseCase

函数 说明
logging.debug(msg, *args, **kwargs) 创建一条严重级别为DEBUG的日志记录
logging.info(msg, *args, **kwargs) 创建一条严重级别为INFO的日志记录
logging.warning(msg, *args, **kwargs) 创建一条严重级别为WARNING的日志记录
logging.error(msg, *args, **kwargs) 创建一条严重级别为ERROR的日志记录
logging.critical(msg, *args, **kwargs) 创建一条严重级别为CRITICAL的日志记录
logging.log(level, *args, **kwargs) 创建一条严重级别为level的日志记录
logging.basicConfig(**kwargs) 对root logger进行一次性配置

Examples

import logging
def get_logger(name=__name__):
    logger = logging.getLogger(name)
    logger.setLevel(logging.DEBUG)
    formatter = logging.Formatter('%(levelname)s %(asctime)s %(filename)s L%(lineno)s: %(message)s')
    consoleHandler = logging.StreamHandler()
    consoleHandler.setFormatter(formatter)
    consoleHandler.setLevel(logging.DEBUG)
    logger.addHandler(consoleHandler)
    return logger

log = get_logger()
log.info('test logging')
INFO 2020-09-09 02:51:06,311 test.py L15: test logging

Reference

@junxnone
Copy link
Owner Author

junxnone commented Sep 8, 2020

  • Issue : 重复打印

最后发现和虚拟环境有关系。。。。

@junxnone junxnone changed the title Python - logging Python logging Mar 29, 2022
@junxnone junxnone transferred this issue from junxnone/examples Mar 29, 2022
@junxnone junxnone changed the title Python logging Programing Python logging Dec 22, 2022
@junxnone junxnone transferred this issue from junxnone/techwiki Dec 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant