用了几年python,log日志还不会写?

目录

  • 瞎比比

  • 与 print 相比 logging 有什么优势?

  • 基础用法

  • 保存到文件

  • 多模块使用 logging

  • 使用配置文件配置 logging

瞎比比

这篇文章其实早在一个月之前就写好了。奈何,加班猛如虎,真的怕了。直至今天才幸运地有了个双休,赶紧排版一下文章发布了。以下为正文。

源码地址:

https://github.com/zonezoen/blog/tree/master/python/logging_model

在初学 Python 的时候,我们使用

print("hello world")

输出了我们的第一行代码。在之后的日子里,便一直使用 print 进行调试(当然,还有 IDE 的 debug 模式)。但是,当你在线上运行 Python 脚本的时候,你并不可能一直守着你的运行终端。可是如果不守着的话,每当出现 bug ,错误又无从查起。这个时候,你需要对你的调试工具进行更新换代了,这里我推荐一个优雅的调试工具 logging。

与 print 相比 logging 有什么优势?

那既然我推荐这个工具,它凭什么要被推荐呢?且来看看它有什么优势:

  • 可以输出到多处,例如:在输出到控制台的同时,可以保存日志到日志文件里面,或者保存到其他远程服务器

  • 可以设置日志等级,DEBUG、INFO、ERROR等,在不同的环境(调试环境、线上环境)使用不同的等级来过滤日志,使用起来很方便

  • 配置灵活,可保存到配置文件,格式化输出

基础用法

下面涉及到的代码我都省略了导包部分,详见源码(后台回复 logging 获取源码)
base_usage.py
logging.basicConfig(level=log_level, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')logger = logging.getLogger(__name__)logger.info("Log level info")logger.debug("Log level debug")logger.warning("Log level warning")# 捕获异常,并打印出出错行数try:    raise Exception("my exception")except (SystemExit, KeyboardInterrupt):    raiseexcept Exception:    logger.error("there is an error =>", exc_info=True)

level 为日志等级,分为:

FATAL:致命错误CRITICAL:特别糟糕的事情,如内存耗尽、磁盘空间为空,一般很少使用ERROR:发生错误时,如IO操作失败或者连接问题WARNING:发生很重要的事件,但是并不是错误时,如用户登录密码错误INFO:处理请求或者状态变化等日常事务DEBUG:调试过程中使用DEBUG等级,如算法中每个循环的中间状态

foamat 可以格式化输出,其参数有如下:

%(levelno)s:打印日志级别的数值%(levelname)s:打印日志级别的名称%(pathname)s:打印当前执行程序的路径,其实就是sys.argv[0]%(filename)s:打印当前执行程序名%(funcName)s:打印日志的当前函数%(lineno)d:打印日志的当前行号%(asctime)s:打印日志的时间%(thread)d:打印线程ID%(threadName)s:打印线程名称%(process)d:打印进程ID%(message)s:打印日志信息

捕获异常,以下两行代码都具有相同的作用

logger.exception(msg,_args)logger.error(msg,exc_info = True,_args)

保存到文件,并输出到命令行

这个用法直接 copy 使用就行

import logging# 写入文件import logginglogger = logging.getLogger(__name__)logger.setLevel(level=logging.INFO)handler = logging.FileHandler("info.log")handler.setLevel(logging.INFO)formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')handler.setFormatter(formatter)logger.addHandler(handler)logger.info("Log level info")logger.debug("Log level debug")logger.warning("Log level warning")# 写入文件,同时输出到屏幕import logginglogger = logging.getLogger(__name__)logger.setLevel(level = logging.INFO)handler = logging.FileHandler("info.log")handler.setLevel(logging.INFO)formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')handler.setFormatter(formatter)console = logging.StreamHandler()console.setLevel(logging.INFO)logger.addHandler(handler)logger.addHandler(console)logger.info("Log level info")logger.debug("Log level debug")logger.warning("Log level warning")

多模块使用 logging

被调用者的日志格式会与调用者的日志格式一样
main.py

# -*- coding: utf-8 -*-__auth__ = 'zone'__date__ = '2019/6/17 下午11:46''''公众号:zone7小程序:编程面试题库'''import osimport loggingfrom python.logging_model.code import sub_of_mainlogger = logging.getLogger("zone7Model")logger.setLevel(level=logging.INFO)handler = logging.FileHandler("log.txt")handler.setLevel(logging.INFO)formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')handler.setFormatter(formatter)console = logging.StreamHandler()console.setLevel(logging.INFO)console.setFormatter(formatter)logger.addHandler(handler)logger.addHandler(console)sub = sub_of_main.SubOfMain()logger.info("main module log")sub.print_some_log()

sub_of_main.py

# -*- coding: utf-8 -*-__auth__ = 'zone'__date__ = '2019/6/17 下午11:47''''公众号:zone7小程序:编程面试题库'''import loggingmodule_logger = logging.getLogger("zone7Model.sub.module")class SubOfMain(object):    def __init__(self):        self.logger = logging.getLogger("zone7Model.sub.module")        self.logger.info("init sub class")    def print_some_log(self):        self.logger.info("sub class log is printed")def som_function():    module_logger.info("call function some_function")

使用配置文件配置 logging

这里分别给出了两种配置文件的使用案例,都分别使用了三种输出,输出到命令行、输出到文件、将错误信息独立输出到一个文件

log_cfg.json
{    "version":1,    "disable_existing_loggers":false,    "formatters":{        "simple":{            "format":"%(asctime)s - %(name)s - %(levelname)s - %(message)s"        }    },    "handlers":{        "console":{            "class":"logging.StreamHandler",            "level":"DEBUG",            "formatter":"simple",            "stream":"ext://sys.stdout"        },        "info_file_handler":{            "class":"logging.handlers.RotatingFileHandler",            "level":"INFO",            "formatter":"simple",            "filename":"info.log",            "maxBytes":10485760,            "backupCount":20,            "encoding":"utf8"        },        "error_file_handler":{            "class":"logging.handlers.RotatingFileHandler",            "level":"ERROR",            "formatter":"simple",            "filename":"errors.log",            "maxBytes":10485760,            "backupCount":20,            "encoding":"utf8"        }    },    "loggers":{        "my_module":{            "level":"ERROR",            "handlers":["info_file_handler2"],            "propagate":"no"        }    },    "root":{        "level":"INFO",        "handlers":["console","info_file_handler","error_file_handler"]    }}

通过 json 文件读取配置:

import jsonimport logging.configimport osdef set_log_cfg(default_path="log_cfg.json", default_level=logging.INFO, env_key="LOG_CFG"):    path = default_path    value = os.getenv(env_key, None)    if value:        path = value    if os.path.exists(path):        with open(path, "r") as f:            config = json.load(f)            logging.config.dictConfig(config)    else:        logging.basicConfig(level=default_level)def record_some_thing():    logging.info("Log level info")    logging.debug("Log level debug")    logging.warning("Log level warning")if __name__ == "__main__":    set_log_cfg(default_path="log_cfg.json")    record_some_thing()
log_cfg.yaml
version: 1disable_existing_loggers: Falseformatters:        simple:            format: "%(asctime)s - %(name)s - %(levelname)s - %(message)s"handlers:    console:            class: logging.StreamHandler            level: DEBUG            formatter: simple            stream: ext://sys.stdout    info_file_handler:            class: logging.handlers.RotatingFileHandler            level: INFO            formatter: simple            filename: info.log            maxBytes: 10485760            backupCount: 20            encoding: utf8    error_file_handler:            class: logging.handlers.RotatingFileHandler            level: ERROR            formatter: simple            filename: errors.log            maxBytes: 10485760            backupCount: 20            encoding: utf8loggers:    my_module:            level: ERROR            handlers: [info_file_handler]            propagate: noroot:    level: INFO    handlers: [console,info_file_handler,error_file_handler]

通过 yaml 文件读取配置:

import yamlimport logging.configimport osdef set_log_cfg(default_path="log_cfg.yaml", default_level=logging.INFO, env_key="LOG_CFG"):    path = default_path    value = os.getenv(env_key, None)    if value:        path = value    if os.path.exists(path):        with open(path, "r") as f:            config = yaml.load(f)            logging.config.dictConfig(config)    else:        logging.basicConfig(level=default_level)def record_some_thing():    logging.info("Log level info")    logging.debug("Log level debug")    logging.warning("Log level warning")if __name__ == "__main__":    set_log_cfg(default_path="log_cfg.yaml")    record_some_thing()



往期推荐:

公众号荒废了几个月,我开发了一款小程序「编程面试题库」

2019 Python 面试 100 问,你会几道?

自学编程的朋友,我想给你们这 5 个建议


来源:zone7   链接:

https://mp.weixin.qq.com/s/Dp3TxB3cTDT2dmgt3RITJg


python爬虫人工智能大数据公众号

640?wx_fmt=gif


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值