前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python标准库 - logging

Python标准库 - logging

作者头像
py3study
发布2020-01-07 10:25:20
4560
发布2020-01-07 10:25:20
举报
文章被收录于专栏:python3python3

编写代码时, 常要跟踪下其运行过程, 记录日志是常用的方式. 较简单的就是print命令打印到终端, 或通过open函数写入文件. 但随着代码量的增加, 该方式不可控的弊端, 也凸显出来, 这也正是logging模块出现的背景.

对于logging模块, 简单来说直接import进来, 就可以用了.

In [1]: import logging

In [2]: logging.warning('Watch out!')

WARNING:root:Watch out!

In [3]: logging.info('I told you so')

In [4]:

In [4]:

第二个语句没有输出呢, 这需了解下日志等级的概念. logging模块默认的日志等级是WARNING, 大于该等级的日志才会输出, 细节如下.

Level

When it’s used

DEBUG

Detailed information, typically of interest only when diagnosing problems.

INFO

Confirmation that things are working as expected.

WARNING

An indication that something unexpected happened, or indicative of some problem in the near future (e.g. ‘disk space low’). The software is still working as expected.

ERROR

Due to a more serious problem, the software has not been able to perform some function.

CRITICAL

A serious error, indicating that the program itself may be unable to continue running.

借助basicConfig方法, 可对日志记录做些配置.

In [1]: import logging

In [2]: logging.basicConfig(filename='/tmp/example.log',level=logging.DEBUG)

In [3]: logging.debug('This message should go to the log file')

In [4]: logging.info('So should this')

In [5]: logging.warning('And this, too')

$ cat /tmp/example.log

DEBUG:root:This message should go to the log file

INFO:root:So should this

WARNING:root:And this, too

在脚本中, 可按照下面的方式使用logging模块.

#!/usr/bin/env python

# -*- coding: utf-8 -*-

# myapp.py

import logging

import mylib

def main():

    logging.basicConfig(filename='myapp.log', format='%(asctime)s - %(name)s - %(levelname)s - %(pathname)s - %(message)s', level=logging.INFO)

    logging.info('Started')

    mylib.do_something()

    logging.info('Finished')

if __name__ == '__main__':

    main()

#!/usr/bin/env python

# -*- coding: utf-8 -*-

# mylib.py

import logging

def do_something():

    logging.info('Doing something')

$ python myapp.py

$ cat myapp.log

2018-01-19 17:00:14,821 - root - INFO - myapp.py - Started

2018-01-19 17:00:14,821 - root - INFO - /home/zz/stage/mylib.py - Doing something

2018-01-19 17:00:14,821 - root - INFO - myapp.py - Finished

简单了解了logging模块的使用, 接着看下其中几个重要的概念.

1. Logger 日志记录器, 暴露给应用程序直接使用的接口, 几个方法用于设置记录器.

Logger.setLevel() - 指定日志记录器处理的日志最低级别.

Logger.addHandler(), Logger.removeHandler() - 为日志记录器添加, 或移除处理器.

2. Handler 处理器, 将记录器获取的日志, 传送到其定义的目的地, 也有几个方法设置处理器.

setLevel() - 指定处理器处理的日志最低级别.

setFormatter() - 为处理器选择一个格式化器.

3. Formatter 格式化器, 指定日志的输出格式.

最开始说了logging模块的简单用法, 以及了解了上面的概念后, 看下开发较大应用时, 如何使用logging呢.

这主要介绍两种方式, 其一是, 将logging模块的配置直接写在应用代码中, 如下所示.

#!/usr/bin/env python

# -*- coding: utf-8 -*-

# simple_logging_module.py

import logging

# create logger

logger = logging.getLogger('simple_example')

logger.setLevel(logging.DEBUG)

# create console handler and set level to debug

ch = logging.StreamHandler()

ch.setLevel(logging.DEBUG)

# create formatter

formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

# add formatter to ch

ch.setFormatter(formatter)

# add ch to logger

logger.addHandler(ch)

# 'application' code

logger.debug('debug message')

logger.info('info message')

logger.warn('warn message')

logger.error('error message')

logger.critical('critical message')

$ python simple_logging_module.py

2018-01-19 21:41:58,180 - simple_example - DEBUG - debug message

2018-01-19 21:41:58,180 - simple_example - INFO - info message

2018-01-19 21:41:58,180 - simple_example - WARNING - warn message

2018-01-19 21:41:58,180 - simple_example - ERROR - error message

2018-01-19 21:41:58,180 - simple_example - CRITICAL - critical message

其二, 配置信息写入文件中, 借助fileConfig方法加载该文件, 如下所示.

#!/usr/bin/env python

# -*- coding: utf-8 -*-

# simple_logging_config.py

import logging

import logging.config

logging.config.fileConfig('logging.conf')

# create logger

logger = logging.getLogger('simpleExample')

# 'application' code

logger.debug('debug message')

logger.info('info message')

logger.warn('warn message')

logger.error('error message')

logger.critical('critical message')

# logging.conf

[loggers]

keys=root,simpleExample

[handlers]

keys=consoleHandler

[formatters]

keys=simpleFormatter

[logger_root]

level=DEBUG

handlers=consoleHandler

[logger_simpleExample]

level=DEBUG

handlers=consoleHandler

qualname=simpleExample

propagate=0

[handler_consoleHandler]

class=StreamHandler

level=DEBUG

formatter=simpleFormatter

args=(sys.stdout,)

[formatter_simpleFormatter]

format=%(asctime)s - %(name)s - %(levelname)s - %(message)s

datefmt=

$ python simple_logging_config.py

2018-01-19 21:53:46,216 - simpleExample - DEBUG - debug message

2018-01-19 21:53:46,216 - simpleExample - INFO - info message

2018-01-19 21:53:46,216 - simpleExample - WARNING - warn message

2018-01-19 21:53:46,216 - simpleExample - ERROR - error message

2018-01-19 21:53:46,216 - simpleExample - CRITICAL - critical message

最后简单说下, 如何总结一个模块, 可围绕下面三点进行说明.

1. 该模块解决的问题, 其它方法是如何做的.

2. 模块的使用, 以及核心概念(或定义).

3. 核心API的梳理.

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
日志服务
日志服务(Cloud Log Service,CLS)是腾讯云提供的一站式日志服务平台,提供了从日志采集、日志存储到日志检索,图表分析、监控告警、日志投递等多项服务,协助用户通过日志来解决业务运维、服务监控、日志审计等场景问题。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档