-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmylogging.py
115 lines (108 loc) · 3.44 KB
/
mylogging.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import os
from logging.config import dictConfig
lastinfo_path = "logs/lastinfo.log"
lastdebug_path = "logs/lastdebug.log"
info_path = "logs/info.log"
warn_path = "logs/warning.log"
if not os.path.exists("logs"):
os.makedirs("logs")
try:
os.remove(lastinfo_path)
except OSError:
pass
try:
os.remove(lastdebug_path)
except OSError:
pass
def is_docker():
path = "/proc/self/cgroup"
status = os.path.exists("/.dockerenv") or os.path.isfile(path) and any("docker" in line for line in open(path))
return status
if is_docker() is False or os.environ.get("VERBOSE_LOGGING", "FALSE") == "TRUE":
logging_mode = "Verbose "
LOGGING_CONFIG = {
"version": 1,
"disable_existing_loggers": True,
"loggers": {
"": {
"level": "DEBUG",
"handlers": ["console", "all_warning", "all_info", "last_info", "last_debug"],
},
},
"handlers": {
"console": {
"level": "WARNING",
"formatter": "fmt",
"class": "logging.StreamHandler",
"stream": "ext://sys.stdout",
},
"all_warning": {
"level": "WARNING",
"formatter": "fmt",
"class": "logging.handlers.RotatingFileHandler",
"filename": warn_path,
"maxBytes": 1048576,
"backupCount": 10,
},
"all_info": {
"level": "INFO",
"formatter": "fmt",
"class": "logging.handlers.RotatingFileHandler",
"filename": info_path,
"maxBytes": 1048576,
"backupCount": 10,
},
"last_info": {
"level": "INFO",
"formatter": "fmt",
"class": "logging.handlers.RotatingFileHandler",
"filename": lastinfo_path,
"maxBytes": 1048576,
},
"last_debug": {
"level": "DEBUG",
"formatter": "fmt",
"class": "logging.handlers.RotatingFileHandler",
"filename": lastdebug_path,
"maxBytes": 1048576,
},
},
"formatters": {
"fmt": {"format": "%(asctime)s-%(levelname)s-%(name)s-%(process)d::%(module)s|%(lineno)s:: %(message)s"},
},
}
else:
logging_mode = ""
LOGGING_CONFIG = {
"version": 1,
"disable_existing_loggers": True,
"loggers": {
"": {
"level": "DEBUG",
"handlers": ["console", "all_warning"],
},
},
"handlers": {
"console": {
"level": "WARNING",
"formatter": "fmt",
"class": "logging.StreamHandler",
"stream": "ext://sys.stdout",
},
"all_warning": {
"level": "WARNING",
"formatter": "fmt",
"class": "logging.handlers.RotatingFileHandler",
"filename": warn_path,
"maxBytes": 1048576,
"backupCount": 10,
},
},
"formatters": {
"fmt": {"format": "%(asctime)s-%(levelname)s-%(name)s-%(process)d::%(module)s|%(lineno)s:: %(message)s"},
},
}
dictConfig(LOGGING_CONFIG)
import logging
logger = logging.getLogger(__name__)
logger.warning(f"***{logging_mode}Logging Started***")