logging: do not like to write log time to Ay

logging module Introduction

logging模块是python内置的标准模块,主要用于输出程序的运行日志。
可以设置输出日志的等级,日志保存路径,日志文件回滚等等。

The basic use the logging module

import logging


logging.basicConfig(
    level=logging.INFO,  # 指定日志的等级为INFO
    format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"  # 输出的日志格式
)
"""
日志等级如下:
DEBUG:调试过程中使用DEBUG等级,如算法中每个循环的中间状态
INFO:处理请求或者状态变化等日常事务
WARNING:发生很重要的事件,但是并不是错误时,如用户登录密码错误
ERROR:发生错误时,如IO操作失败或者连接问题
CRITICAL:特别糟糕的事情,如内存耗尽、磁盘空间为空,一般很少使用
FATAL:致命错误
"""

"""
日志格式如下:
%(levelno)s:打印日志级别的数值
%(levelname)s:打印日志级别的名称
%(pathname)s:打印当前执行程序的路径,其实就是sys.argv[0]
%(filename)s:打印当前执行程序名
%(funcName)s:打印日志的当前函数
%(lineno)d:打印日志的当前行号
%(asctime)s:打印日志的时间
%(thread)d:打印线程ID
%(threadName)s:打印线程名称
%(process)d:打印进程ID
%(message)s:打印日志信息
datefmt:指定时间格式,同time.strftime();
level:设置日志级别,默认为logging.WARNNING;
stream:指定将日志的输出流,可以指定输出到sys.stderr,sys.stdout或者文件,默认输出到sys.stderr,当stream和filename同时指定时,stream被忽略
"""

# 创建一个日志打印器
logger = logging.getLogger(name="satori")  # 如果name不指定,那么默认为"root"

# 下面就可以打印了
logger.debug("i am a debug")
logger.info("i am a info")
logger.warning("i am a warning")
logger.error("i am a error")
logger.critical("i am a critical")
logger.fatal("i am a fatal")
"""
2019-06-27 17:08:55,206 - satori - INFO - i am a info
2019-06-27 17:08:55,206 - satori - WARNING - i am a warning
2019-06-27 17:08:55,206 - satori - ERROR - i am a error
2019-06-27 17:08:55,206 - satori - CRITICAL - i am a critical
2019-06-27 17:08:55,206 - satori - CRITICAL - i am a fatal
"""

# 但是我们发现debug貌似没有打印,这是因为我们将日志等级设置成了INFO
# 那么等级比它弱的将不会输出,如果我们将日志等级设置为ERROR,那么同理debug、info、warning将不会起作用

The log file is written

import logging


# 我们设置日志等级和输出格式其实有两种,刚才的是一种,下面介绍第二种。
logger = logging.getLogger("satori")

# 可以通过logger设置日志等级
logger.setLevel(level=logging.ERROR)

# 创建FileHandler,类似于open
file_handler = logging.FileHandler("1.txt", mode="a", encoding="utf-8")

# 创建输出的格式
format = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")

# 将格式设置到handler里面去
file_handler.setFormatter(format)

# 再将handler添加到logger里面去,那么此时logger在输出的时候便可以输出到文件里面去
logger.addHandler(file_handler)

logger.warning("i am a warning")
logger.fatal("yabai,fatal occurred")

At this time 1.txt document reads as follows

2019-06-27 17:19:46,525 - satori - CRITICAL - yabai,fatal occurred

And at this time and there is no output to the console, but if I also want to output to the console it? And I want the output to a file and output to the console to set different levels of file I want to record a simple point, but I want to see more detailed console, how to do it?


At the same time write to the log file and the console

import logging


logger = logging.getLogger("satori")

# 这里就不要再使用logger设置日志等级了
# logger.setLevel(level=logging.ERROR)

# 创建FileHandler实例,类似于open
file_handler = logging.FileHandler("2.txt", mode="a", encoding="utf-8")

# 使用FileHandler设置日志等级
file_handler.setLevel(level=logging.ERROR)

# 创建输出的格式
file_format = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")

# 将格式设置到handler里面去
file_handler.setFormatter(file_format)


# 既然文件有一个FileHandler,那么控制台也有一个StreamHandler
# 既然是输出到控制台,那么就不需要参数了
console_handler = logging.StreamHandler()

# 设置日志等级,控制台我想看的详细一点,所以设置为INFO
console_handler.setLevel(level=logging.INFO)

# 同样要设置日志格式
console_format = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")

console_handler.setFormatter(console_format)


# 再将handler添加到logger里面去
logger.addHandler(file_handler)
logger.addHandler(console_handler)


logger.info("i am a info")
logger.warning("i am a warning")
logger.error("i am a error")

The console and outputs the following documents

控制台
2019-06-27 17:32:46,031 - satori - WARNING - i am a warning
2019-06-27 17:32:46,032 - satori - ERROR - i am a error

文件
2019-06-27 17:32:46,032 - satori - ERROR - i am a error

Guess you like

Origin www.cnblogs.com/traditional/p/11111353.html