log_module.py
2.54 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#encoding=utf-8
#!/usr/bin/env python
# @auther : Ylei
# @date : 2019/9/18
# @File : logging_test.py
# @Software : PyCharm
import os
import logging
from logging.handlers import TimedRotatingFileHandler
import datetime
import json
LOG_PATH = "log"
LOG_INFO = '_info.log'
LOG_ERROR = '_error.log'
class logger:
def __init__(self, prefix_name="flask"):
if os.path.exists(LOG_PATH):
pass
else:
os.mkdir(LOG_PATH)
self.prefix = prefix_name
self.info_logger = logging.getLogger("info")
self.error_logger = logging.getLogger("error")
self.format = logging.Formatter('[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s]\
[%(filename)s:%(lineno)d]' '[%(levelname)s] : %(message)s')
# 指定文件位置文件名以及输出格式
# info_file_handler = logging.FileHandler("%s/%s%s" % (LOG_PATH, prefix_name, LOG_INFO))
info_file_handler = logging.handlers.RotatingFileHandler("%s/%s%s" % (LOG_PATH, prefix_name, LOG_INFO),
maxBytes=50*1024*1024, backupCount=2)
info_file_handler.setFormatter(self.format)
error_file_handler = logging.handlers.RotatingFileHandler("%s/%s%s" % (LOG_PATH, prefix_name, LOG_ERROR),
maxBytes=50*1024*1024, backupCount=2)
error_file_handler.setFormatter(self.format)
self.info_logger.addHandler(info_file_handler)
self.error_logger.addHandler(error_file_handler)
# 指定日志的最低输出级别
self.info_logger.setLevel(logging.DEBUG)
#self.error_logger.setLevel(logging.WARNING)
def debug(self, msg, *args, **kwargs):
self.info_logger.debug(msg, *args, **kwargs)
def info(self, msg, *args, **kwargs):
self.info_logger.info(msg, *args, **kwargs)
def warn(self, msg, *args, **kwargs):
self.info_logger.warn(msg, *args, **kwargs)
def warning(self, msg, *args, **kwargs):
self.info_logger.warning(msg, *args, **kwargs)
def error(self, msg, *args, **kwargs):
self.error_logger.error(msg, *args, **kwargs)
def fatal(self, msg, *args, **kwargs):
self.error_logger.fatal(msg, *args, **kwargs)
def critical(self, msg, *args, **kwargs):
self.error_logger.critical(msg, *args, **kwargs)
if __name__ == "__main__":
log =logger("celery")
log.info("jdshskh")
log.error("hdskck")
log.debug("1122debug")
# log.warn("warn")
# log.warning("warning")
# log.critical("critical")
# log.fatal("fatal")