-
Notifications
You must be signed in to change notification settings - Fork 88
/
commonlog.py
59 lines (43 loc) · 1.72 KB
/
commonlog.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""
author: Zakkoree
"""
import os
import logging
class Logger():
CmdLevel = logging.DEBUG
FileLevel = logging.INFO
FileName = "out.log"
FilePath = os.path.dirname(os.path.abspath(__file__))
def __init__(self, LoggerName):
"""
"""
# LoggerName:实例化对象的名字 FileName:外部文件名 CmdLevel:设置控制台中日志输出的级别 FileLevel:设置文件日志输出的级别
self.logger = logging.getLogger(LoggerName)
# 判断条件,如果存在handlers则不创建,解决日志重复输出问题
if not self.logger.handlers:
# 设置日志的级别
self.logger.setLevel(logging.DEBUG)
# 设置日志的输出格式
fmt = logging.Formatter('%(asctime)s-%(name)s-%(levelname)s-%(message)s')
# 借助handle将日志输出到文件中
fh = logging.FileHandler(self.FilePath + "/" + self.FileName)
fh.setLevel(self.FileLevel)
# 借助handle将日志输出到控制台
ch = logging.StreamHandler()
ch.setLevel(self.CmdLevel)
# 配置logger
fh.setFormatter(fmt)
ch.setFormatter(fmt)
# 给logger添加handle
self.logger.addHandler(fh)
self.logger.addHandler(ch)
def debug(self,message):
self.logger.debug(message)
def info(self,message):
self.logger.info(message)
def warn(self,message):
self.logger.warning(message)
def error(self,message):
self.logger.error(message)
def critical(self,message):
self.logger.critical(message)