forked from williamdlees/digby_backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_logging.py
55 lines (43 loc) · 1.78 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
# Copyright William Lees
import logging.handlers
from flask import request
from flask import has_request_context
import sys
from mail_log_handler import FlaskMailLogHandler
from rabbit_log import FlaskRabbitLogHandler
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(
'--------------------------------\n'
'[%(asctime)s] %(levelname)s (%(module)s) :\n'
'%(remote_addr)s %(url)s\n%(message)s\n'
)
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['RABBIT_LOG']:
mq_handler = FlaskRabbitLogHandler(app.config['RABBIT_URL'], app.config['INSTANCE_NAME'])
mq_handler.setLevel(logging.ERROR)
mq_handler.setFormatter(formatter)
root.addHandler(mq_handler)
if app.config['MAIL_LOG']:
mail_handler = FlaskMailLogHandler(mail, app.config['MAIL_USERNAME'], ['[email protected]'], 'Error from %s' % app.config['INSTANCE'])
mail_handler.setLevel(logging.ERROR)
mail_handler.setFormatter(formatter)
root.addHandler(mail_handler)
handler = logging.handlers.RotatingFileHandler(app.config['LOGPATH'], maxBytes=1024 * 1024)
handler.setLevel(int(app.config['LOGLEVEL']))
handler.setFormatter(formatter)
app.logger.addHandler(handler)