logging模块

#  logging模块 ------ 日志
#  为啥写log?
# 为了排错    为了做数据分析   操作审计
# 无论想打印日志里面的什么内容,都需要自己写
# basicConfig 进行设置格式

import logging
# 输出内容有五个级别  默认
# logging.debug('debug message')  #调试
# logging.info('info')            信息
# logging.warning('warning')         警告
# logging.error('error')           错误
# logging.critical('critical')        批判性的

# logging.basicConfig函数各参数:
# filename:指定日志文件名;
# filemode:和file函数意义相同,指定日志文件的打开模式,'w'或者'a';
# format:指定输出的格式和内容,format可以输出很多有用的信息,
# datefmt:指定时间格式,同time.strftime()
#
# %(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:打印日志信息

# logging.warning('warning message text1')
# logging.basicConfig(
#     format='%(asctime)s - %(name)s - %(levelname)s[line: %(lineno)d]-%(module)s: %(message)s',
#     datefmt='%Y-%M-%d %H:%M:%S %p',
#     #level= logging.DEBUG,
# )
#
# logging.warning('warning message text2')
# logging.error('error message text2')
# logging.critical('cietical message text2')

# 输出到文件

# logging.basicConfig(
#     format='%(asctime)s - %(name)s - %(levelname)s[line: %(lineno)d]-%(module)s: %(message)s',
#     datefmt='%Y-%M-%d %H:%M:%S %p',
#     filename='tmp.log',
#     filemode='w',
#     level= logging.DEBUG
#
# )
#
# logging.warning('warning message text1')
# logging.error('error message text2')
# logging.critical('cietical message text3')



# 同时输出到屏幕和文件

# fh = logging.FileHandler('tmp.log',encoding='utf-8')
# fh2 = logging.FileHandler('tmp2.log',encoding='utf-8')
#
# sh = logging.StreamHandler()
# logging.basicConfig(
#     format='%(asctime)s - %(name)s - %(levelname)s[line: %(lineno)d]-%(module)s: %(message)s',
#     datefmt='%Y-%M-%d %H:%M:%S %p',
#
#     level= logging.ERROR,
#     handlers=[fh,sh,fh2]
# )
#
# logging.warning('warning message text1')
# logging.error('error message text2')
# logging.critical('cietical message text3')


# 做日志切割
# import time
# from logging import handlers
# sh = logging.StreamHandler()
# rh = handlers.RotatingFileHandler('myapp.log',maxBytes=1024,backupCount=5)  按照文件大小切割
# fh = handlers.TimedRotatingFileHandler(filename='x2.log', when='s', interval=5,encoding='utf-8') 按照时间切割
# logging.basicConfig(
#     format='%(asctime)s - %(name)s - %(levelname)s[line: %(lineno)d]-%(module)s: %(message)s',
#     datefmt='%Y-%M-%d %H:%M:%S %p',
#
#     level= logging.DEBUG,
#     handlers=[fh,rh,sh]
# )
#
# for i in range(1,100000):
#     time.sleep(0.1)
#     logging.error('KeyboardInterrupt error %s'%str(i))

 

posted @ 2020-04-20 12:34  luoluokang  阅读(91)  评论(0编辑  收藏  举报