handle_log.py
1.8 KB
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
# #!/usr/bin/env python3
# # -*- coding: utf-8 -*-
# # @Time : 2022/12/8 23:50
# # @Author : Shitou
# # @FileName: handle_log.py
# # @Software: PyCharm
# """
# 封装日志
# """
import logging
import os
from logging.handlers import TimedRotatingFileHandler, RotatingFileHandler
from common.handle_path import Log_Path
from common.handle_config import conf
file_path = os.path.join(Log_Path, "log.log")
class HandleLog:
def create_logger():
# 创建日志收集器
my_log = logging.getLogger('shitou')
# 设置日志收集器等级
my_log.setLevel(conf.get("logging", "collect"))
# 设置输出渠道
# ---输出到控制台
sh = logging.StreamHandler()
sh.setLevel(conf.get("logging", "console"))
my_log.addHandler(sh)
# ---输出到文件根据时间轮转
fh = TimedRotatingFileHandler(filename=file_path,
when=conf.get("logging", "time_units"),
interval=conf.getint("logging", "time"),
backupCount=conf.getint("logging", "max_quantity"),
encoding="utf-8")
# ---输出到文件根据文件大小轮转
# fh = RotatingFileHandler(filename=file_path,
# mode='a',
# maxBytes=502,
# backupCount=4,
# encoding="utf-8", )
fh.setLevel(conf.get("logging", "file"))
my_log.addHandler(fh)
# 设置日志输出格式
formatter = "%(asctime)s - [%(filename)s-->line:%(lineno)d] - %(levelname)s: %(message)s"
mate = logging.Formatter(formatter)
sh.setFormatter(mate)
fh.setFormatter(mate)
return my_log
log = create_logger()