-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcustom_logging.py
61 lines (48 loc) · 2 KB
/
custom_logging.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
# Copyright William Lees
#
# This source code, and any executable file compiled or derived from it, is governed by the European Union Public License v. 1.2,
# the English version of which is available here: https://perma.cc/DK5U-NDVE
#
import logging.handlers
from flask import request
from flask import has_request_context
from flask_mail import email_dispatched
import sys
from mail_log_handler import FlaskMailLogHandler
from mail import log_mail
from security_log_handler import init_security_logging
class RequestFormatter(logging.Formatter):
def format(self, record):
if has_request_context():
record.url = request.url
record.remote_addr = request.remote_addr
return super().format(record)
else:
record.url = ''
record.remote_addr = ''
return super().format(record)
formatter = RequestFormatter(
'[%(asctime)s] %(levelname)s (%(module)s) : %(remote_addr)s %(url)s %(message)s'
)
def init_logging(app, mail):
root = logging.getLogger()
root.setLevel(logging.INFO)
if app.config['REMOTE_DEBUG']:
sys.path.append("pycharm-debug-py3k.egg")
import pydevd
pydevd.settrace('127.0.0.1', port=30000, stdoutToServer=True, stderrToServer=True)
if app.config['MAIL_LOG']:
mail_handler = FlaskMailLogHandler(mail, 'william@lees.org.uk', ['william@lees.org.uk'], 'Error from OGRDB')
mail_handler.setLevel(logging.ERROR)
mail_handler.setFormatter(formatter)
root.addHandler(mail_handler)
email_dispatched.connect(log_mail)
init_security_logging()
if 'CONSOLE_LOG' in app.config and app.config['CONSOLE_LOG']:
handler = logging.StreamHandler(stream=sys.stdout)
handler.setLevel(int(app.config['LOGLEVEL']))
root.addHandler(handler)
handler = logging.handlers.RotatingFileHandler(app.config['LOGPATH'], maxBytes=1024 * 1024)
handler.setLevel(int(app.config['LOGLEVEL']))
handler.setFormatter(formatter)
root.addHandler(handler)