首页 / PYTHON / Python模块:logging
Python模块:logging
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python模块:logging,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3467字,纯文字阅读大概需要5分钟。
内容图文
很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误、警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,logging的日志可以分为 debug、info、warning、error、critical 5个级别,下面我们看一下怎么用模块初识:
#logging初识 import logging logging.warning("user [James] attempted wrong password more than 3 times") logging.critical("server is down") # WARNING:root:user [James] attempted wrong password more than 3 times # CRITICAL:root:server is down
上面的代码是最简单的方式,括号里的内容为打印的信息,logging.后的方法为日志的级别,下面看看logging五个级别的详细信息
如果想把日志写到文件里,也很简单:
#日志打印到文件中 import logging logging.basicConfig(filename="example.log",level=logging.INFO, format="%(asctime)s %(message)s", datefmt="%m/%d/%Y %H:%M:%S [%A]") # H 24小时格式 I 12小时格式 A 周几完整 a 周几简写 p AM/PM logging.debug("This message should go to the log file") logging.info("So should this") logging.warning("And this ,too")
logging.basicConfig里定义了输入文件路径,输入日志信息的级别,输入的格式,格式可自定义;执行完代码后example.log文件会生成信息如下:
10/31/2016 17:16:17 [Monday] So should this 10/31/2016 17:16:17 [Monday] And this ,too
其中下面这句中的level=loggin.INFO意思是,把日志纪录级别设置为INFO,也就是说,只有比日志是INFO或比INFO级别更高的日志才会被纪录到文件里,在这个例子, 第一条日志是不会被纪录的,如果希望纪录debug的日志,那把日志级别改成DEBUG就行了
如果想同时把log打印在屏幕和文件日志里,就需要了解一点复杂的知识了:
The logging library takes a modular approach and offers several categories of components: loggers, handlers, filters, and formatters.
Loggers expose the interface that application code directly uses.
Handlers send the log records (created by loggers) to the appropriate destination.
Filters provide a finer grained facility for determining which log records to output.
Formatters specify the layout of log records in the final output.
#!/usr/bin/env python # -*- coding:utf-8 -*- #-Author-Lian import logging #创建logger logger = logging.getLogger("test_log") #创建logger对象 括号内容随便写 logger.setLevel(logging.INFO) #全局日志级别 ch = logging.StreamHandler() #日志打印到屏幕上 ch.setLevel(logging.DEBUG) #指定ch日志打印级别 fh = logging.FileHandler("access.log") #日志存进文件 fh.setLevel(logging.WARNING) #指定fh日志输入级别 formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") #定义日志格式,可写多个 #添加日志格式到ch,fh ch.setFormatter(formatter) fh.setFormatter(formatter) #添加ch,fh到logger中 logger.addHandler(ch) logger.addHandler(fh) logger.debug('debug message') logger.info('info message') logger.warn('warn message') logger.error('error message') logger.critical('critical message')
全局日志级别为整个程序的底线,局部日志级别要想打印则不能比这个级别再低了
屏幕打印信息
2016-10-31 17:23:42,988 - test_log - INFO - info message 2016-10-31 17:23:42,988 - test_log - WARNING - warn message 2016-10-31 17:23:42,988 - test_log - ERROR - error message 2016-10-31 17:23:42,988 - test_log - CRITICAL - critical message
access.log:
2016-10-31 17:02:06,223 - test_log - WARNING - warn message 2016-10-31 17:02:06,224 - test_log - ERROR - error message 2016-10-31 17:02:06,224 - test_log - CRITICAL - critical message
日志所有的格式:
重要的几个格式:%(lineno)d 输出打印日志代码行 ,%(process)d输出打印日志的进程ID ,%(thread)d输出打印日志的线程ID
内容总结
以上是互联网集市为您收集整理的Python模块:logging全部内容,希望文章能够帮你解决Python模块:logging所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。