LOG_FORMAT = "%(asctime)s=====%(levelname)s+++++%(message)s"
logging.basicConfig(filename="ruochen.log", level=logging.DEBUG, format=LOG_FORMAT)
logging.debug("This is a debug log.")
logging.info("This is a info log.")
logging.warning("This is a warning log.")
logging.error("This is a error log.")
logging.critical("This is a critical log.")
# 另外一种写法
logging.log(logging.DEBUG, "This is a debug log.")
logging.log(logging.INFO, "This is a info log.")
logging.log(logging.WARNING, "This is a warning log.")
logging.log(logging.ERROR, "This is a error log.")
logging.log(logging.CRITICAL, "This is a critical log.")
```
```
# ruochen.log
DEBUG:root:This is a debug log.
INFO:root:This is a info log.
WARNING:root:This is a warning log.
ERROR:root:This is a error log.
CRITICAL:root:This is a critical log.
DEBUG:root:This is a debug log.
INFO:root:This is a info log.
WARNING:root:This is a warning log.
ERROR:root:This is a error log.
CRITICAL:root:This is a critical log.
2019-07-01 09:51:01,181=====DEBUG+++++(message)s
2019-07-01 09:51:01,181=====INFO+++++(message)s
2019-07-01 09:51:01,181=====WARNING+++++(message)s
2019-07-01 09:51:01,181=====ERROR+++++(message)s
2019-07-01 09:51:01,181=====CRITICAL+++++(message)s
2019-07-01 09:51:01,181=====DEBUG+++++(message)s
2019-07-01 09:51:01,181=====INFO+++++(message)s
2019-07-01 09:51:01,181=====WARNING+++++(message)s
2019-07-01 09:51:01,182=====ERROR+++++(message)s
2019-07-01 09:51:01,182=====CRITICAL+++++(message)s
2019-07-01 09:52:24,155=====DEBUG+++++This is a debug log.
2019-07-01 09:52:24,155=====INFO+++++This is a info log.
2019-07-01 09:52:24,155=====WARNING+++++This is a warning log.
2019-07-01 09:52:24,155=====ERROR+++++This is a error log.
2019-07-01 09:52:24,155=====CRITICAL+++++This is a critical log.
2019-07-01 09:52:24,155=====DEBUG+++++This is a debug log.
2019-07-01 09:52:24,156=====INFO+++++This is a info log.
2019-07-01 09:52:24,156=====WARNING+++++This is a warning log.
2019-07-01 09:52:24,156=====ERROR+++++This is a error log.
2019-07-01 09:52:24,156=====CRITICAL+++++This is a critical log.
```
- format 参数
asctime %(asctime)s 日志事件发生的时间--人类可读时间,如:2003-07-08 16:49:45,896
created %(created)f 日志事件发生的时间--时间戳,就是当时调用time.time()函数返回的值
relativeCreated %(relativeCreated)d 日志事件发生的时间相对于logging模块加载时间的相对毫秒数(目前还不知道干嘛用的)
msecs %(msecs)d 日志事件发生事件的毫秒部分
levelname %(levelname)s 该日志记录的文字形式的日志级别('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL')
levelno %(levelno)s 该日志记录的数字形式的日志级别(10, 20, 30, 40, 50)
name %(name)s 所使用的日志器名称,默认是'root',因为默认使用的是 rootLogger
message %(message)s 日志记录的文本内容,通过 msg % args计算得到的
pathname %(pathname)s 调用日志记录函数的源码文件的全路径
filename %(filename)s pathname的文件名部分,包含文件后缀
module %(module)s filename的名称部分,不包含后缀
lineno %(lineno)d 调用日志记录函数的源代码所在的行号
funcName %(funcName)s 调用日志记录函数的函数名
process %(process)d 进程ID
processName %(processName)s 进程名称,Python 3.1新增
thread %(thread)d 线程ID
threadName %(thread)s 线程名称
logging.StreamHandler 将日志消息发送到输出到Stream,如std.out, std.err或任何file-like对象。
logging.FileHandler 将日志消息发送到磁盘文件,默认情况下文件大小会无限增长
logging.handlers.RotatingFileHandler 将日志消息发送到磁盘文件,并支持日志文件按大小切割
logging.hanlders.TimedRotatingFileHandler 将日志消息发送到磁盘文件,并支持日志文件按时间切割
logging.handlers.HTTPHandler 将日志消息以GET或POST的方式发送给一个HTTP服务器
logging.handlers.SMTPHandler 将日志消息发送给一个指定的email地址
logging.NullHandler 该Handler实例会忽略error messages,通常被想使用logging的library开发者使用来避免'No handlers could be found for logger XXX'信息的出现。
1. 需求
现在有以下几个日志记录的需求
1) 要求将所有级别的所有日志都写入磁盘文件
2) all.log 文件中记录所有的日志信息,日志格式为:日期和时间 - 日志级别 - 日志信息
3) error.log文件中单独记录error及以上级别的日志信息,日志格式为:日期和时间 - 日志级别 - 文件名[:行号] - 日志信息
4) 要求all.log在每天凌晨进行日志切割
'''
import logging
import logging.handlers
import datetime
# 定义logger
logger = logging.getLogger("mylogger")
logger.setLevel(logging.DEBUG)
# 为两个不同的文件设置不同的Handler
rf_handler = logging.handlers.TimedRotatingFileHandler("all.log", when='midnight', interval=1, backupCount=7, atTime=datetime.time(0, 0, 0, 0))
rf_handler.setFormatter(logging.Formatter("%(asctime)s - &(levelname)s - %(message)s"))
f_handler = logging.FileHandler("error.log")
f_handler.setLevel(logging.ERROR)
f_handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(filename)s[:%(lineno)d] - %(message)s"))
# 把相应的处理器组装到logger上
logger.addHandler(rf_handler)
logger.addHandler(f_handler)
logger.debug('debug message')
logger.info('info message')
logger.warning('warning message')
logger.error('error message')
logger.critical('critical message')
```
```
# all.log
2019-07-01 10:48:06,878 - &(levelname)s - debug message
2019-07-01 10:48:06,878 - &(levelname)s - info message
2019-07-01 10:48:06,878 - &(levelname)s - warning message
2019-07-01 10:48:06,879 - &(levelname)s - error message
2019-07-01 10:48:06,879 - &(levelname)s - critical message
```
```
# error.log
2019-07-01 10:48:06,879 - ERROR - 02.py[:36] - error message
2019-07-01 10:48:06,879 - CRITICAL - 02.py[:37] - critical message
```
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。