python学习---logging模块

# shutil  主要更目录和文件有关模块
# 拷贝目录 shutil,copy2('原路径', ’目标路径‘)
# 拷贝文件
# 删除目录
'''
#为什么要写LOG
# log是为了排错;
#log 是为了数据分析
一个购物商城---数据库里
# 什么时间购买了什么商品
#把那些商品加入购物车
# 1一个用户什么时间在地方,登录了购物程序
# 2 搜索了那些信息,
# 3什么是关闭软件
#4 对那些商品点进行进行看了
# 1 用来记录用的行为---数据分析
# 2 用来记录用户的行为--操作审计
#3 排查代码中的错误
# import logging
'''
import logging
#logging 模块常用5个级别的日志输出
# logging.debug('debug message')
# logging.info ('info message')
# logging.warning('warning ,message')
# logging.error('error message')
# logging.certical('critical message')

import logging

file_handler = logging.FileHandler(filename='x1.log', mode='a', encoding='utf-8',) # 获取一个存储文件句柄
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S %p',
handlers=[file_handler,],
level=logging.ERROR
)

logging.error('你好')
logging.debug('debug message')
logging.info('info message')
logging.warning('warning message')
logging.error('error message')
logging.critical('critical message')

#对日志的切割问题
import time
import logging
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 -%(module)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S %p',
handlers=[fh,sh,rh],
level=logging.ERROR
)

for i in range(1,100000):
time.sleep(1)
logging.error('KeyboardInterrupt error %s'%str(i))

# logging.basicConfig()函数中可通过具体参数来更改logging模块默认行为,可用参数有:
#
# filename:用指定的文件名创建FiledHandler,这样日志会被存储在指定的文件中。
# filemode:文件打开方式,在指定了filename时使用这个参数,默认值为“a”还可指定为“w”。
# format:指定handler使用的日志显示格式。
# datefmt:指定日期时间格式。
# level:设置rootlogger(后边会讲解具体概念)的日志级别
# stream:用指定的stream创建StreamHandler。可以指定输出到sys.stderr,sys.stdout或者文件(f=open(‘test.log’,’w’)),默认为sys.stderr。若同时列出了filename和stream两个参数,则stream参数会被忽略。

#format参数中可能用到的格式化串:
# %(name)s Logger的名字
# %(levelno)s 数字形式的日志级别
# %(levelname)s 文本形式的日志级别
# %(pathname)s 调用日志输出函数的模块的完整路径名,可能没有
# %(filename)s 调用日志输出函数的模块的文件名
# %(module)s 调用日志输出函数的模块名
# %(funcName)s 调用日志输出函数的函数名
# %(lineno)d 调用日志输出函数的语句所在的代码行
# %(created)f 当前时间,用UNIX标准的表示时间的浮 点数表示
# %(relativeCreated)d 输出日志信息时的,自Logger创建以 来的毫秒数
# %(asctime)s 字符串形式的当前时间。默认格式是 “2003-07-08 16:49:45,896”。逗号后面的是毫秒
# %(thread)d 线程ID。可能没有
# %(threadName)s 线程名。可能没有
# %(process)d 进程ID。可能没

# import logging
#
# logger = logging.getLogger()
# # 创建一个handler,用于写入日志文件
# fh = logging.FileHandler('test.log',encoding='utf-8')
#
# # 再创建一个handler,用于输出到控制台
# ch = logging.StreamHandler()
# formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# fh.setLevel(logging.DEBUG)
#
# fh.setFormatter(formatter)
# ch.setFormatter(formatter)
# logger.addHandler(fh) #logger对象可以添加多个fh和ch对象
# logger.addHandler(ch)
#
# logger.debug('logger debug message')
# logger.info('logger info message')
# logger.warning('logger warning message')
# logger.error('logger error message')
# logger.critical('logger critical message')
原文地址:https://www.cnblogs.com/pushuiyu/p/12516292.html