From f4b83e1a1e9548e9c152a56a5e9c28712480460a Mon Sep 17 00:00:00 2001 From: thanhthtran Date: Wed, 18 Sep 2024 20:02:34 -0700 Subject: [PATCH] add logg function --- FlaskTemplate/__init__.py | 16 ++++++++------ FlaskTemplate/views.py | 45 +++++++++++++-------------------------- application.py | 12 ++++------- 3 files changed, 29 insertions(+), 44 deletions(-) diff --git a/FlaskTemplate/__init__.py b/FlaskTemplate/__init__.py index 7738d74..cdb33ac 100644 --- a/FlaskTemplate/__init__.py +++ b/FlaskTemplate/__init__.py @@ -1,8 +1,12 @@ -""" -The flask application package. -""" - +import logging from flask import Flask -app = Flask(__name__) -import FlaskTemplate.views +app = Flask(__name__) +wsgi_app = app.wsgi_app +# TODO: Set the app's logger level to "warning" +# and any other necessary changes +app.logger.setLevel(logging.WARNING) +streamHandler = logging.StreamHandler() +streamHandler.setLevel(logging.WARNING) +app.logger.addHandler(streamHandler) +import FlaskExercise.views \ No newline at end of file diff --git a/FlaskTemplate/views.py b/FlaskTemplate/views.py index 4265fb1..90070a9 100644 --- a/FlaskTemplate/views.py +++ b/FlaskTemplate/views.py @@ -1,37 +1,22 @@ -""" -Routes and views for the flask application. -""" +from flask import flash, render_template, redirect, request +from FlaskExercise import app -from datetime import datetime -from flask import render_template -from FlaskTemplate import app @app.route('/') -@app.route('/home') def home(): - """Renders the home page.""" + log = request.values.get('log_button') + # TODO: Appropriately log the different button presses + # with the appropriate log level. + if log: + if log == 'info': + app.logger.info('No issue.') + elif log == 'warning': + app.logger.warning('Warning occurred.') + elif log == 'error': + app.logger.error('Error occurred.') + elif log == 'critical': + app.logger.critical('Critical error occurred.') # noqa: E501 return render_template( 'index.html', - title='Home Page', - year=datetime.now().year, - ) - -@app.route('/contact') -def contact(): - """Renders the contact page.""" - return render_template( - 'contact.html', - title='Contact', - year=datetime.now().year, - message='Your contact page.' - ) - -@app.route('/about') -def about(): - """Renders the about page.""" - return render_template( - 'about.html', - title='About', - year=datetime.now().year, - message='Your application description page.' + log=log ) diff --git a/application.py b/application.py index 5933866..741c384 100644 --- a/application.py +++ b/application.py @@ -1,14 +1,10 @@ -""" -This script runs the FlaskTemplate application using a development server. -""" - from os import environ -from FlaskTemplate import app +from FlaskExercise import app if __name__ == '__main__': HOST = environ.get('SERVER_HOST', 'localhost') try: - PORT = int(environ.get('SERVER_PORT', '3000')) + PORT = int(environ.get('SERVER_PORT', '5555')) except ValueError: - PORT = 3000 - app.run(HOST, PORT) + PORT = 5555 + app.run(HOST, PORT, debug=True)