log日志 和回滚日志

# coding=utf8
# import logging
#
# logging.basicConfig(level=logging.DEBUG,
# format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
# datefmt='%a, %d %b %Y %H:%M:%S',
# filename='myapp.log',
# filemode='w')
# # logging.debug('This is debug message')
# # logging.info('This is info message')
# a=[1,2,3,4]
# for aa in a:
# try :
# aa=a[5]
# print aa
# except Exception, e:
# print e
# logging.warning('This is warning message %s'%(e))


import time
# import logging
import logging.handlers

# logging初始化工作
logging.basicConfig()

# myapp的初始化工作
myapp = logging.getLogger('myapp')
myapp.setLevel(logging.INFO)

# 写入文件,如果文件超过100个Bytes,仅保留5个文件。
handler = logging.handlers.RotatingFileHandler(
'myapp.log', maxBytes=10*1024, backupCount=5)

formatter = logging.Formatter('%(asctime)s|%(name)-12s: %(levelname)-8s %(message)s')
handler.setFormatter(formatter)
# 设置后缀名称,跟strftime的格式一样
myapp.addHandler(handler)

while True:
time.sleep(0.1)
myapp.info("file test")
原文地址:https://www.cnblogs.com/xiaoxiaoshuaishuai0219/p/9105952.html