-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSLog.py
33 lines (32 loc) · 1.38 KB
/
CSLog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# -*- coding: utf-8 -*-
import logging
from logging.handlers import TimedRotatingFileHandler
import colorlog
import os
class CSLog(object):
def __init__(self, project_name, log_path):
assert (project_name != None)
assert (log_path != None)
if (os.path.exists(os.path.split(log_path)[0])):
pass
else:
os.mkdir(os.path.split(log_path)[0])
self.logger = logging.getLogger(project_name)
self.logger.setLevel(logging.DEBUG)
Colorformatter = colorlog.ColoredFormatter(
"%(log_color)s%(asctime)s %(name)s-%(levelname)s(file: %(filename)s, func: %(funcName)s, line: %(lineno)d, pid: %(process)d): %(message)s")
formatter = logging.Formatter(
"%(asctime)s %(name)s-%(levelname)s(file: %(filename)s, func: %(funcName)s, line: %(lineno)d, pid: %(process)d): %(message)s")
#文件配置
fileHander = TimedRotatingFileHandler(filename=log_path, when='D', interval=1, backupCount=7)
fileHander.setLevel(logging.INFO)
fileHander.setFormatter(formatter)
#屏幕输出
streamHander = logging.StreamHandler()
streamHander.setLevel(logging.INFO)
streamHander.setFormatter(Colorformatter)
#加入logger
self.logger.addHandler(fileHander)
self.logger.addHandler(streamHander)
def getlogger(self):
return self.logger