日志级别:
'CRITICAL': CRITICAL,
'ERROR': ERROR,
'WARN': WARNING,
'WARNING': WARNING,
'INFO': INFO,
'DEBUG': DEBUG,
'NOTSET': NOTSET,
import logging
logging.warning("user [wy] attempted wrong password more than 3 times")
logging.critical("server is down")
输出:
WARNING:root:user [wy] attempted wrong password more than 3 times
CRITICAL:root:server is down 如何把日志写入到文件中?
import logging
logging.basicConfig(filename="example.log",level=logging.INFO) #filename后面是日志文件名,level为INFO级别,日志文件会记录INFO级别以上的日志
logging.warning("user [wy] attempted wrong password more than 3 times")
logging.info("show this")
logging.debug("this is erro")
logging.critical("server is down")
生成一个新的文件example.log
example.log记录:
WARNING:root:user [wy] attempted wrong password more than 3 times
INFO:root:show this
CRITICAL:root:server is down 日志级别:debug<info<warning<critical
 怎么在日志中加入时间?
import logging
logging.basicConfig(filename="example.log",level=logging.INFO,format="%(asctime)s %(message)s",datefmt="%m/%d/%Y %I:%M:%S %p") #注意大小写
logging.warning("user [wy] attempted wrong password more than 3 times")
logging.info("show this")
logging.debug("this is erro")
logging.critical("server is down") example.log显示:
10/20/2016 08:49:32 PM user [wy] attempted wrong password more than 3 times
10/20/2016 08:49:32 PM show this
10/20/2016 08:49:32 PM server is down
 实际生产中,我们要把日志打印到文件中,也要打印到屏幕上。
在这里我们需要了解日志模块的一些复杂的知识了,这里有4个功能:Loggers,Handlers,Filters,Formatters
1 Loggers expose the interface that application code directly #直接调用
2 Handlers send the log records (created by loggers) to the appropriate destination #把日志发送到不同的地方(屏幕,本地文件等)
3 Filters provide a finer grained facility for determing which log records to output #日志过滤,
4 Formatters specify the layout of log records in the final output #字符串格式化

要求:我们要把INFO级别以上的日志打印到屏幕上,把warning级别以上的日志打印到文件中。

import logging
logger = logging.getLogger("TEST-LOG") #获取日志对象
logger.setLevel(logging.DEBUGE) #设置全局日志级别 ch = logging.StreamHandler() #把日志打印到屏幕
ch.setLevel(logging.INFO) #打印到屏幕的日志级别 fh = logging.FileHandler("access.log") #输出到access.log文件中
fh.setLevel(logging.WARNING) #输出到文件中的日志级别 formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") #格式化输出日志
ch.setFormatter(formatter) #打印到屏幕上的日志格式
fh.setFormatter(formatter)#打印到文件中的日志 logger.addHandler(ch) #告诉logger输出日志到具体的handler
logger.addHandler(fh)#告诉logger输出日志到具体的handler 我们可以给打印到屏幕上的日志和打印到文件中的日志定义不同的格式 logger.debug("debug message")
logger.info("info message")
logger.error("error message")
logger.warning("warning message")
logger.critical("critical message") 注意:当全局日志级别高于我们自定义到日志文件中和屏幕上的日志级别时,会按照全局日志级别来打印日志到文件中和屏幕上
当全局日志级别低于我们自定义到日志文件中和屏幕上的日志级别时,会按照我们自定义的日志级别来打印

最新文章

  1. C#设计模式-外观模式
  2. UITableViewCell里面separator的设置
  3. 酷炫ILOVEU
  4. iOS的后台任务
  5. Swift开发第十一篇——Designated、Convenience和Required
  6. 同IP不同端口Session冲突问题
  7. php安装,mysql安装
  8. Failed to start component [StandardEngine[Catalina].StandardHost[localhost....
  9. iOS APP上线流程
  10. poj 3469 Dual Core CPU
  11. Networking
  12. UIProgressView-初识IOS
  13. HBase 6、用Phoenix Java api操作HBase
  14. 2012 PHP热门资料64个+经典源码50个——下载目录 :
  15. 敏捷开发(十)- Scrum每日例会
  16. panel的autoscroll属性不起作用
  17. shiro简单配置 (写的不错 收藏一下)
  18. C 语言----- 指针
  19. 第五篇 - Selenium突破反爬获取qq邮件标题
  20. GDOI2018 滑稽子图 [斯特林数,树形DP]

热门文章

  1. opencart在空间中安装出错,连接不上mysql
  2. 如何用Matplotlib绘制三元函数
  3. protobuf C++ 使用示例
  4. [系统集成] 部署 mesos-exporter 和 prometheus 监控 mesos task
  5. 条款19:设计class犹如设计type
  6. Install CodeBlocks in CentOS 7
  7. BestCoder Round #90 A.Kblack loves flag(随机数生成种子)
  8. 说说Web.Config与App.Config
  9. Async Programming - 1 async-await 糖的本质(1)
  10. 检测Linux VPS是Xen、OpenVZ还是KVM真假方法