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. FLUENT质量加权平均和面积加权平均的区别【转载】

    转载自:http://blog.sina.com.cn/s/blog_7ef78d170101bhfn.html 网上关于fluent中质量加强平均(Mass-Weighted Average)和面积 ...

  2. pymongo错误记录

    1.AutoReconnect pymongo.errors.AutoReconnect: connection closed 2.ServerSelectionTimeoutError pymong ...

  3. 大数据技术之kettle(2)——练习三个基本操作

    一.同一数据库两表数据关联更新 实现效果:把stu1的数据按id同步到stu2,stu2有相同id则更新数据 步骤: 1.在mysql中创建两张表: mysql>create database ...

  4. js怎么模拟点击网页元素

    在测试页面中,引入jquery源文件,并添加一个div标签,一个a标签,为了演示效果a标签暂时不添加地址 通过jquery为div标签绑定一个点击事件,这个事件是被动执行的.意思是要点击才会触发的 在 ...

  5. include mime.types;

    include mime.types; (index):1 Resource interpreted as Stylesheet but transferred with MIME type appl ...

  6. Alternatives to Activiti for all platforms with any license

    Activiti Activiti is a light-weight workflow and Business Process Management (BPM) Platform targeted ...

  7. vs 设置护眼背景颜色

    工具 —> 选项 —> 环境 —> 字体和颜色 —> 纯文本(显示项中) —> 项目背景 —> 自定义—> 色调位85.饱和度123.亮度205,保存即可.测 ...

  8. 生产环境zabbix3.2上亿的表数据通过表分区的方式进行历史数据清理

    生产环境zabbix3.2上亿的表数据通过表分区的方式进行历史数据清理 zabbix服务器经常报警io过载,在报警的时候发现是数据库在删除历史数据时耗时较长 数据库积攒了大量的历史数据信息,主要集中在 ...

  9. DataWorks2.0——DataStudio简单对比使用上手

    1.原先的数据管理去哪里了? 悬停在此图标上即可:  2.项目模式有何不同?

  10. Glide升级到4.x版本遇到的问题

    Failed to find GeneratedAppGlideModule. You should include an annotationProcessor compile dependency ...