/
logsettings.py
137 lines (124 loc) · 5.06 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""Get log settings."""
import logging
import platform
import sys
import warnings
from logging.handlers import SysLogHandler
LOG_LEVELS = ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL']
def get_logger_config(log_dir, # lint-amnesty, pylint: disable=unused-argument
logging_env="no_env",
local_loglevel='INFO',
service_variant=""):
"""
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.
"""
# Revert to INFO if an invalid string is passed in
if local_loglevel not in LOG_LEVELS:
local_loglevel = 'INFO'
hostname = platform.node().split(".")[0]
syslog_format = ("[service_variant={service_variant}]"
"[%(name)s][env:{logging_env}] %(levelname)s "
"[{hostname} %(process)d] [user %(userid)s] [ip %(remoteip)s] [%(filename)s:%(lineno)d] "
"- %(message)s").format(service_variant=service_variant,
logging_env=logging_env,
hostname=hostname)
logger_config = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'standard': {
'format': '%(asctime)s %(levelname)s %(process)d '
'[%(name)s] [user %(userid)s] [ip %(remoteip)s] %(filename)s:%(lineno)d - %(message)s',
},
'syslog_format': {'format': syslog_format},
'raw': {'format': '%(message)s'},
},
'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse',
},
'userid_context': {
'()': 'edx_django_utils.logging.UserIdFilter',
},
'remoteip_context': {
'()': 'edx_django_utils.logging.RemoteIpFilter',
}
},
'handlers': {
'console': {
'level': 'INFO',
'class': 'logging.StreamHandler',
'formatter': 'standard',
'filters': ['userid_context', 'remoteip_context'],
'stream': sys.stderr,
},
'mail_admins': {
'level': 'ERROR',
'filters': ['require_debug_false'],
'class': 'django.utils.log.AdminEmailHandler'
},
'local': {
'level': local_loglevel,
'class': 'logging.handlers.SysLogHandler',
'address': '/dev/log',
'formatter': 'syslog_format',
'filters': ['userid_context', 'remoteip_context'],
'facility': SysLogHandler.LOG_LOCAL0,
},
'tracking': {
'level': 'DEBUG',
'class': 'logging.handlers.SysLogHandler',
'address': '/dev/log',
'facility': SysLogHandler.LOG_LOCAL1,
'formatter': 'raw',
},
},
'loggers': {
'tracking': {
'handlers': ['tracking'],
'level': 'DEBUG',
'propagate': False,
},
'': {
'handlers': ['console', 'local'],
'level': 'INFO',
'propagate': False
},
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},
# requests is so loud at INFO (logs every connection) that we
# force it to warn by default.
'requests.packages.urllib3': {
'level': 'WARN'
}
}
}
return logger_config
def log_python_warnings():
"""
Stop ignoring DeprecationWarning, ImportWarning, and PendingDeprecationWarning;
log all Python warnings to the main log file.
Not used in test runs, so pytest can collect the warnings triggered for
each test case.
"""
warnings.simplefilter('default')
warnings.filterwarnings('ignore', 'Not importing directory ')
warnings.filterwarnings('ignore', 'Setting _field_data is deprecated')
warnings.filterwarnings('ignore', 'Setting _field_data via the constructor is deprecated')
warnings.filterwarnings('ignore', '.*unclosed.*', category=ResourceWarning)
try:
# There are far too many of these deprecation warnings in startup to output for every management command;
# suppress them until we've fixed at least the most common ones as reported by the test suite
from django.utils.deprecation import RemovedInDjango40Warning, RemovedInDjango41Warning
warnings.simplefilter('ignore', RemovedInDjango40Warning)
warnings.simplefilter('ignore', RemovedInDjango41Warning)
except ImportError:
pass
logging.captureWarnings(True)