-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlogging_config.py
More file actions
executable file
·73 lines (63 loc) · 2.19 KB
/
logging_config.py
File metadata and controls
executable file
·73 lines (63 loc) · 2.19 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2018 Jun.
@author: huanglipang, cyuanhengluo
logging config doc:
https://docs.python.org/2/library/logging.config.html
"""
import logging
import datetime
import time
class UTCFormatter(logging.Formatter):
converter = time.gmtime
class RotatingFileNameHandler(logging.handlers.RotatingFileHandler):
def __init__(self, filename, logPath):
# set format type
formatter = logging.Formatter(fmt="%(asctime)s - PID: %(process)d"\
" - %(levelname)s - %(filename)s - %(message)s",
datefmt="%Y-%m-%d %I:%M:%S %p")
# set filename by the name of scripts
logPath = logPath + "/" + filename.split('.')[-2] + ".log"
# please set the maxBytes by yourself
# it will backup three files and delete the oldest one when create a new one
super(RotatingFileNameHandler, self).__init__(filename=logPath, maxBytes=1024, backupCount=3)
super(RotatingFileNameHandler, self).setFormatter(fmt=formatter)
# NOTSET, DEBUG, INFO, WARNING, ERROR, CRITICAL
super(RotatingFileNameHandler, self).setLevel(logging.INFO)
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
# local time
"standard": {
"format": "%(asctime)s - %(message)s",
"datefmt": "%Y-%m-%d %I:%M:%S %p"
},
"complete": {
"format": "%(asctime)s - PID: %(process)d"\
" - %(levelname)s - %(filename)s - %(message)s",
"datefmt": "%Y-%m-%d %I:%M:%S %p"
},
"utc": {
# "()" is a special key, which indicates a custom instantiation.
"()": UTCFormatter,
"format": "%(asctime)s %(message)s",
"datefmt": "%Y-%m-%d %I:%M:%S %p"
}
},
"handlers": {
# StreamHandler will show log in console
"default": {
"level": "INFO",
"formatter": "complete",
"class": "logging.StreamHandler"
}
},
# root logger
"root": {
"handlers": ["default"],
"level": "INFO",
"propagate": True
}
}