logging

1.basicConfig方式

import logging

# 以下是日志的级别
logging.debug('debug message')
logging.info('info message')
logging.warning('warning msg')
logging.error('error msg')
logging.critical('critical msg')

设置级别:

logging.basicConfig(level=logging.DEBUG)

如果想要存到文件里:

logging.basicConfig(level=logging.DEBUG,filename='logger.logo')
logging.basicConfig(level=logging.DEBUG,filename='logger.logo',filemode='w')

时间和行号

format='%(asctime)s %(lineno)d %(message)s'

运行之后文件里这样显示的

2019-08-23 10:15:44,510 11 debug message
2019-08-23 10:15:44,511 12 info message
2019-08-23 10:15:44,511 13 warning msg
2019-08-23 10:15:44,512 14 error msg
2019-08-23 10:15:44,513 15 critical msg

2.format参数中可能用到的格式化串:

--- %(name)s        Logger的名字

---%(levelno)s     数字形式的日志级别

---%(levelname)s     文本形式的日志级别

---%(pathname)s       调用日志输出函数的模块的完整路径名,可能没有

---%(filename)s          调用日志输出函数的模块的文件名

---%(module)s  调用日志输出函数的模块名

---%(funcName)s   调用日志输出函数的函数名

---%(lineno)s  调用函数日志输出函数的语句所在的代码行

---%(created)f  当前时间,用UNIX标准的表示时间的浮点数表示

---%(relativeCreated)d  输出日志信息时的,自logger创建以来的毫秒数

---%(asctime)s  字符串形式的当前时间,默认格式是“2003-07-08 16:49:45,896”  逗号后面是毫秒

---%(thread)d  线程ID。可能没有

---%(threadName)s  线程名。可能没有

---%(process)d  进程ID。 可能没有

---%(message)s  用户输出的信息

3.logger对象

import logging

logger = logging.getLogger()           # 创建对象

f = logging.FileHandler('test_log')           # 向文件里发送内容
screen = logging.StreamHandler() # 向屏幕发送内容 ff = logging.Formatter('%(asctime)s' '%(message)s') f.setFormatter(ff)
screen.setFormatter(ff) logger.addHandler(f)
logger.addHandler(screen) logger.setLevel('DEBUG') logger.debug('debug')
logger.info('info')
logger.warning('warning')
logger.error('error')
logger.critical('critical')

运行结果屏幕上也显示,也能存到文件里

改为函数的形式:

import logging
def logger():
logger = logging.getLogger() # 创建对象 f = logging.FileHandler('test_log') # 向文件里发送内容
screen = logging.StreamHandler() # 向屏幕发送内容 ff = logging.Formatter('%(asctime)s' '%(message)s') f.setFormatter(ff)
screen.setFormatter(ff) logger.addHandler(f)
logger.addHandler(screen) logger.setLevel('DEBUG')
return logger logger = logger()
logger.debug('debug')
logger.info('info')
logger.warning('warning')
logger.error('error')
logger.critical('critical')

python学习-57 logging模块的更多相关文章

  1. python学习之-- logging模块

    logging模块功能:提供了标准的日志接口,可以通过它存储各种格式的日志.日志5个级别分:debug(),info(),warning(),error(),critical() logging.ba ...

  2. python 学习笔记 -logging模块(日志)

    模块级函数 logging.getLogger([name]):返回一个logger对象,如果没有指定名字将返回root loggerlogging.debug().logging.info().lo ...

  3. python学习之logging模块

    Logger.setLevel(level) 设置记录器的级别为level.低于该级别的信息将被忽略. 记录器默认级别为NOTSET.如果记录器是根记录器,则默认将记录所有信息: 如果是一个非根记录器 ...

  4. Python自建logging模块

    本章将介绍Python内建模块:日志模块,更多内容请从参考:Python学习指南 简单使用 最开始,我们用最短的代码体验一下logging的基本功能. import logging logger = ...

  5. Python实战之logging模块使用详解

    用Python写代码的时候,在想看的地方写个print xx 就能在控制台上显示打印信息,这样子就能知道它是什么了,但是当我需要看大量的地方或者在一个文件中查看的时候,这时候print就不大方便了,所 ...

  6. Python中的logging模块就这么用

    Python中的logging模块就这么用 1.日志日志一共分成5个等级,从低到高分别是:DEBUG INFO WARNING ERROR CRITICALDEBUG:详细的信息,通常只出现在诊断问题 ...

  7. python中日志logging模块的性能及多进程详解

    python中日志logging模块的性能及多进程详解 使用Python来写后台任务时,时常需要使用输出日志来记录程序运行的状态,并在发生错误时将错误的详细信息保存下来,以别调试和分析.Python的 ...

  8. Python学习day19-常用模块之re模块

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  9. Python日志输出——logging模块

    Python日志输出——logging模块 标签: loggingpythonimportmodulelog4j 2012-03-06 00:18 31605人阅读 评论(8) 收藏 举报 分类: P ...

随机推荐

  1. Spring Boot AOP 简易操作日志管理

    AOP (Aspect Oriented Programming) 面向切面编程. 业务有核心业务和边缘业务. 比如用户管理,菜单管理,权限管理,这些都属于核心业务. 比如日志管理,操作记录管理,这些 ...

  2. 讲座 - Transposable elements, non-coding RNAs and epigenetic control in embryonic stem cells

    Dr. Andrew Paul HutchinsDepartment of BiologySouthern University of Science and Technology, Shenzhen ...

  3. 谱聚类算法及其代码(Spectral Clustering)

    https://blog.csdn.net/liu1194397014/article/details/52990015 https://blog.csdn.net/u011089523/articl ...

  4. PHP使用MongoDB类操作MongoDB数据库总结

    参考:https://www.php.net/manual/zh/class.mongodb-driver-manager.php 参考:https://www.zhaokeli.com/articl ...

  5. ISO/IEC 9899:2011 条款6.5.10——按位与操作符

    6.5.10 按位与操作符 语法 1.AND-expression: equality-expression AND-expression    equality-expression 约束 2.这些 ...

  6. 解决IE浏览器没有网络的情况

    计算机能够连接到网络,但是IE浏览器却显示没有网络. 解决方案: 设置 >> IE internet选项: 选择“高级”: 选择“重置”: 勾选“删除个人设置”,点击重置: 重新打开IE, ...

  7. java-mybaits-015-mybatis逆向工程最佳实践【基础mybatis-generator、tk.mybatis、mubatis-plus】

    一.概述 三款框架的功能对比 Mybatis-generator 通用Mapper Mybatis-Plus 代码生成器 支持自动生成Model,Mapper,Mapper XML文件 生成方式不够灵 ...

  8. c# list 使用Where()方法过滤数据

    //根据任务id过滤数据 Func<RfidCodeResultDto, bool> expression = c => c.lineTaskId == _lineTaskId; r ...

  9. [Feature] Final pipeline: custom transformers

    有视频:https://www.youtube.com/watch?v=BFaadIqWlAg 有代码:https://github.com/jem1031/pandas-pipelines-cust ...

  10. WebSocket接收音频,并推送到声卡上

    使用信息 import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.ImmutableM ...