-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload.py
55 lines (40 loc) · 1.41 KB
/
load.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
import datetime
import logging
import os
from flask import render_template, has_request_context, request
from postcoderoonie import create_app
from logging.handlers import SMTPHandler, RotatingFileHandler
app = create_app()
@app.errorhandler(404)
def page_not_found(e):
# note that we set the 404 status explicitly
return render_template('404.html'), 404
@app.errorhandler(500)
def server_error(e):
# note that we set the 500 status explicitly
app.logger.error(e)
return render_template('500.html', error=e), 500
class RequestFormatter(logging.Formatter):
def format(self, record):
if has_request_context():
record.url = request.url
record.remote_addr = request.remote_addr
else:
record.url = None
record.remote_addr = None
return super().format(record)
def set_logger():
formatter = RequestFormatter(
'%(asctime)s||%(remote_addr)s||requested %(url)s||%(levelname)s||in %(module)s||%(message)s'
)
# Create handlers
f_handler = RotatingFileHandler(os.getcwd() + '/file.log', maxBytes=100000, backupCount=10)
f_handler.setLevel(logging.DEBUG)
# Create formatters and add it to handlers
f_handler.setFormatter(formatter)
# Add handlers to the logger
app.logger.addHandler(f_handler)
set_logger()
if __name__ == '__main__':
host = "0.0.0.0"
app.run(debug=True, host=host, port=5006)