-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_factory.py
97 lines (78 loc) · 3.83 KB
/
app_factory.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
# app_factory.py
import traceback
import logging
from flask import Flask, render_template, request
from utils.extensions import db, bcrypt, login_manager, create_extensions
from logging.config import fileConfig
import os
def create_app():
app = Flask(__name__)
#Load the logging configuration
fileConfig(os.path.join('configuration', 'logging.conf'))
# Set up a separate logger
logger = logging.getLogger('root')
logger.setLevel(logging.INFO)
handler = logging.FileHandler('app.log')
handler.setLevel(logging.INFO)
logger.addHandler(handler)
# Write a test log message
logger.info('App started')
app.static_folder = "static"
# Configuration settings
app.config["SECRET_KEY"] = "5791628bb0b13ce0c676dfde280ba245"
app.config["SQLALCHEMY_DATABASE_URI"] = "mysql+pymysql://ikokalovic:Mikrovela1!@localhost:3306/banking_app"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
create_extensions(app)
login_manager.login_view = "user_routes.login"
login_manager.login_message_category = "info"
# Register Blueprints
from routes.transaction_routes import transaction_routes
from routes.account_routes import account_routes
from routes.user_routes import user_routes
from routes.search_routes.search import search_routes # Import the search_routes blueprint
from routes.logger_routes.log_routes import log_routes
app.register_blueprint(transaction_routes)
app.register_blueprint(account_routes)
app.register_blueprint(user_routes)
app.register_blueprint(search_routes) # Register the search_routes blueprint
app.register_blueprint(log_routes)
# Log rules for each Blueprint
logger.info("\nMain Application Rules:")
for rule in app.url_map.iter_rules():
logger.info(rule)
# Set logging level to WARNING (or higher) to reduce log messages
log_handler = logging.StreamHandler()
log_handler.setLevel(logging.DEBUG) # Change to WARNING if needed
logger.addHandler(log_handler)
#Set logging from werkzeug to app.log file
werkzeug_logger = logging.getLogger('werkzeug')
for handler in logger.handlers:
handler.setFormatter(logging.Formatter('%(asctime)s [%(levelname)s] - %(name)s - %(message)s'))
werkzeug_logger.addHandler(handler)
# Set logging from DatabaseHandling.connection to app.log file
database_logger = logging.getLogger('DatabaseHandling.connection')
database_logger.setLevel(logging.INFO)
for handler in logger.handlers:
handler.setFormatter(logging.Formatter('%(asctime)s [%(levelname)s] - %(name)s - %(message)s'))
database_logger.addHandler(handler)
# Set logging from routes.account_routes.dashboard to app.log file
account_logger = logging.getLogger('routes.account_routes.dashboard')
account_logger.setLevel(logging.INFO)
for handler in logger.handlers:
handler.setFormatter(logging.Formatter('%(asctime)s [%(levelname)s] - %(name)s - %(message)s'))
account_logger.addHandler(handler)
# Custom 404 error handler
@app.errorhandler(404)
def page_not_found(e):
# Log available URLs
available_urls = [rule.rule for rule in app.url_map.iter_rules()]
logging.error(f"404 Not Found: {request.url}. The requested URL was not found on the server. Available URLs: {available_urls}")
# Render the error page
return render_template("error.html", error_message=f"404 Not Found: {request.url}. The requested URL was not found on the server. If you entered the URL manually, please check your spelling and try again."), 404
@app.errorhandler(Exception)
def log_error(exception):
# Get the exception details
error_message = str(exception)
# Render the error template with the error message
return render_template("error.html", error_message=error_message), 500
return app