Skip to content

Commit

Permalink
add logg function
Browse files Browse the repository at this point in the history
  • Loading branch information
thanhthtran committed Sep 19, 2024
1 parent dda7bfd commit f4b83e1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 44 deletions.
16 changes: 10 additions & 6 deletions FlaskTemplate/__init__.py
Original file line number Diff line number Diff line change
@@ -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
45 changes: 15 additions & 30 deletions FlaskTemplate/views.py
Original file line number Diff line number Diff line change
@@ -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
)
12 changes: 4 additions & 8 deletions application.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit f4b83e1

Please sign in to comment.