import logging
import os
import time
import logging
import logging.handlers
log_dir_path=os.getcwd()+"/logs"
log_dir_path = log_dir_path
if os.path.exists(log_dir_path) and os.path.isdir(log_dir_path):
if os.path.exists(os.getcwd()+"/logs/Error") :
pass
else:
os.mkdir(os.getcwd()+"/logs/Error")
if os.path.exists(os.getcwd()+"/logs/Info"):
pass
else:
os.mkdir(os.getcwd()+"/logs/Info")
else:
os.mkdir(log_dir_path)
os.mkdir(os.getcwd()+"/logs/Error")
os.mkdir(os.getcwd()+"/logs/Info")
# logging初始化工作
time_stamp_name = time.strftime("%Y%m%d", time.localtime())
log_name = time_stamp_name + ".log"
E_path = os.path.join(log_dir_path+"/Error", log_name)
Info_path= os.path.join(log_dir_path+"/Info", log_name)
log_format = "%(asctime)s --%(name)s -- ProcessID:%(process)d -- ThreadName:%(threadName)s(ID:%(thread)d) %(filename)s.%(name)s.%(funcName)s():(%(funcName)s:%(lineno)d) [%(levelname)s]:\n%(message)s"
date_format = "%Y-%m-%d %H:%M:%S %a"
logging.basicConfig(level=logging.DEBUG,
format=log_format,
datefmt=date_format,
)
# myapp的初始化工作
logger= logging.getLogger('SshTest.class')
# 写入文件,如果文件超过100个Bytes,仅保留5个文件。
handler = logging.handlers.RotatingFileHandler(filename=E_path, maxBytes=1024*50*1024, backupCount=5,encoding="utf-8",delay=False)
fmt=logging.Formatter(fmt=log_format)
handler.setFormatter(fmt)
handler2 = logging.handlers.RotatingFileHandler(filename=Info_path, maxBytes=1024*50*1024, backupCount=5,encoding="utf-8",delay=False)
fmt=logging.Formatter(fmt=log_format)
handler2.setFormatter(fmt)
handler.setLevel(logging.ERROR)
handler2.setLevel(logging.INFO)
# 设置后缀名称,跟strftime的格式一样
logger.addHandler(handler)
logger.addHandler(handler2)
# myapp.addHandler(ch)
class SshTest(object):
def tes(self):
while True:
time.sleep(0.01)
logger.info("file test")
logger.debug("bebug test")
logger.error("error test")
logger.warning("warning test")
if __name__ == "__main__":
SshTest().tes()
Error日志
2019-03-09 15:34:27,076 --SshTest.class -- ProcessID:9920 -- ThreadName:MainThread(ID:4304) rotating.py.SshTest.class.tes():(tes:44) [ERROR]:
error test
2019-03-09 15:34:27,086 --SshTest.class -- ProcessID:9920 -- ThreadName:MainThread(ID:4304) rotating.py.SshTest.class.tes():(tes:44) [ERROR]:
error test
2019-03-09 15:34:27,096 --SshTest.class -- ProcessID:9920 -- ThreadName:MainThread(ID:4304) rotating.py.SshTest.class.tes():(tes:44) [ERROR]:
error test
2019-03-09 15:34:27,106 --SshTest.class -- ProcessID:9920 -- ThreadName:MainThread(ID:4304) rotating.py.SshTest.class.tes():(tes:44) [ERROR]:
error test
2019-03-09 15:34:27,116 --SshTest.class -- ProcessID:9920 -- ThreadName:MainThread(ID:4304) rotating.py.SshTest.class.tes():(tes:44) [ERROR]:
error test
2019-03-09 15:34:27,126 --SshTest.class -- ProcessID:9920 -- ThreadName:MainThread(ID:4304) rotating.py.SshTest.class.tes():(tes:44) [ERROR]:
error test
2019-03-09 15:34:27,136 --SshTest.class -- ProcessID:9920 -- ThreadName:MainThread(ID:4304) rotating.py.SshTest.class.tes():(tes:44) [ERROR]:
error test
2019-03-09 15:34:27,146 --SshTest.class -- ProcessID:9920 -- ThreadName:MainThread(ID:4304) rotating.py.SshTest.class.tes():(tes:44) [ERROR]:
error test
2019-03-09 15:34:27,157 --SshTest.class -- ProcessID:9920 -- ThreadName:MainThread(ID:4304) rotating.py.SshTest.class.tes():(tes:44) [ERROR]:
Info日志
2019-03-09 15:59:05,829 --SshTest.class -- ProcessID:1196 -- ThreadName:MainThread(ID:9048) rotating.py.SshTest.class.tes():(tes:57) [INFO]: file test 2019-03-09 15:59:05,829 --SshTest.class -- ProcessID:1196 -- ThreadName:MainThread(ID:9048) rotating.py.SshTest.class.tes():(tes:59) [ERROR]: error test 2019-03-09 15:59:05,829 --SshTest.class -- ProcessID:1196 -- ThreadName:MainThread(ID:9048) rotating.py.SshTest.class.tes():(tes:60) [WARNING]: warning test 2019-03-09 15:59:05,839 --SshTest.class -- ProcessID:1196 -- ThreadName:MainThread(ID:9048) rotating.py.SshTest.class.tes():(tes:57) [INFO]: file test 2019-03-09 15:59:05,839 --SshTest.class -- ProcessID:1196 -- ThreadName:MainThread(ID:9048) rotating.py.SshTest.class.tes():(tes:59) [ERROR]: error test 2019-03-09 15:59:05,839 --SshTest.class -- ProcessID:1196 -- ThreadName:MainThread(ID:9048) rotating.py.SshTest.class.tes():(tes:60) [WARNING]: warning test 2019-03-09 15:59:05,849 --SshTest.class -- ProcessID:1196 -- ThreadName:MainThread(ID:9048) rotating.py.SshTest.class.tes():(tes:57) [INFO]: file test 2019-03-09 15:59:05,849 --SshTest.class -- ProcessID:1196 -- ThreadName:MainThread(ID:9048) rotating.py.SshTest.class.tes():(tes:59) [ERROR]: error test 2019-03-09 15:59:05,849 --SshTest.class -- ProcessID:1196 -- ThreadName:MainThread(ID:9048) rotating.py.SshTest.class.tes():(tes:60) [WARNING]: warning test