forked from openedx-unsupported/edx-certificates
-
Notifications
You must be signed in to change notification settings - Fork 2
/
logsettings.py
102 lines (91 loc) · 3.08 KB
/
logsettings.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
# -*- coding: utf-8 -*-
import os
import platform
import sys
from logging.handlers import SysLogHandler
def get_logger_config(log_dir,
logging_env="no_env",
edx_filename="edx.log",
dev_env=False,
debug=False,
LOCAL_LOGLEVEL='INFO'):
"""
Return the appropriate logging config dictionary. You should assign the
result of this to the LOGGING var in your settings. The reason it's done
this way instead of registering directly is because I didn't want to worry
about resetting the logging state if this is called multiple times when
settings are extended.
If dev_env is set to true logging will not be done via local rsyslogd,
instead, application logs will be dropped in log_dir.
"edx_filename" are ignored unless dev_env
is set to true since otherwise logging is handled by rsyslogd.
"""
hostname = platform.node().split(".")[0]
syslog_format = (
"[service_variant=certs]"
"[%(name)s]"
"[env:{logging_env}] "
"%(levelname)s "
"[{hostname} %(process)d] "
"[%(filename)s:%(lineno)d] "
"- %(message)s"
).format(
logging_env=logging_env,
hostname=hostname,
)
handlers = ['console', 'local']
logger_config = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'standard': {
'format': '%(asctime)s %(levelname)s %(process)d '
'[%(name)s] %(filename)s:%(lineno)d - %(message)s',
},
'syslog_format': {'format': syslog_format},
'raw': {'format': '%(message)s'},
},
'handlers': {
'console': {
'level': 'DEBUG' if debug else 'INFO',
'class': 'logging.StreamHandler',
'formatter': 'standard',
'stream': sys.stdout,
},
},
'loggers': {
'': {
'handlers': handlers,
'level': 'DEBUG',
'propagate': False
},
'xserver': {
'handlers': handlers,
'level': 'DEBUG',
'propagate': False
},
}
}
if dev_env:
edx_file_loc = os.path.join(log_dir, edx_filename)
logger_config['handlers'].update({
'local': {
'class': 'logging.handlers.RotatingFileHandler',
'level': LOCAL_LOGLEVEL,
'formatter': 'standard',
'filename': edx_file_loc,
'maxBytes': 1024 * 1024 * 2,
'backupCount': 5,
},
})
else: # pragma: no cover
logger_config['handlers'].update({
'local': {
'level': LOCAL_LOGLEVEL,
'class': 'logging.handlers.SysLogHandler',
'address': '/dev/log',
'formatter': 'syslog_format',
'facility': SysLogHandler.LOG_LOCAL0,
},
})
return logger_config