Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logging infrastructure #27

Merged
merged 8 commits into from
Apr 1, 2016
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions bikeshare_app/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Module containing configurations for the flask server"""
from os import getenv
import logging
# TODO: Setup a logging environment

# pylint: disable=too-few-public-methods
Expand All @@ -8,29 +9,38 @@ class BaseConfig(object):
DEBUG = False
TESTING = False
MONGODB_SETTINGS = {'db': 'bikeshare'}
LOGGING_FORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
LOGGING_LOCATION = 'bikeshare.log'
LOGGING_LEVEL = logging.INFO

class TestingConfig(BaseConfig):
"""Configuration for running unit tests"""
DEBUG = False
TESTING = True
MONGODB_SETTINGS = {'db': 'bikeshare-test'}
LOGGING_LEVEL = logging.DEBUG

class DevelopmentConfig(BaseConfig):
"""The configuration that should be run during development"""
DEBUG = True
TESTING = True
MONGODB_SETTINGS = {'db': 'bikeshare-test'}
LOGGING_LEVEL = logging.DEBUG

CONFIG = {
"development": "bikeshare_app.config.DevelopmentConfig",
"testing": "bikeshare_app.config.TestingConfig",
"default": "bikeshare_app.config.BaseConfig"
'development': 'bikeshare_app.config.DevelopmentConfig',
'testing': 'bikeshare_app.config.TestingConfig',
'default': 'bikeshare_app.config.BaseConfig',
}

def configure_app(app):
"""Configure the app based on where it is deployed. First check for the
FLASK_ENVIRONMENT environment variable, fallback to the default
BaseConfig"""
config_name = getenv("FLASK_ENVIRONMENT", "default")
print(config_name)
config_name = getenv('FLASK_ENVIRONMENT', 'default')
app.config.from_object(CONFIG[config_name])
handler = logging.FileHandler(app.config['LOGGING_LOCATION'])
handler.setLevel(app.config['LOGGING_LEVEL'])
logging.getLogger().setLevel(app.config['LOGGING_LEVEL'])
formatter = logging.Formatter(app.config['LOGGING_FORMAT'])
handler.setFormatter(formatter)
app.logger.addHandler(handler)
5 changes: 4 additions & 1 deletion bikeshare_app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@
@app.route("/")
def index():
"""Return the index page for the app"""
app.logger.debug('Rendering index page')
return render_template('bikes.html', bikes=ActiveShare.objects(available=True))

# Admin page
# Display bike availabilities again. Ability to alter database info
@app.route("/admin")
def admin():
"""The admin page"""
app.logger.debug('Rendering admin page')
#TODO Check user credentials for admin role
# Allow user through if check passed, else log attempt and return home
return render_template('admin.html')

# Bike request endpoint
@app.route("/request_bike/<user_email>/<int:bike_id>")
def request_bike():
def request_bike(user_email, bike_id):
"""This endpoint is for requesting a bike"""
app.logger.debug('User %s requesting bike %d' % (user_email, bike_id))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

%-formatting is "old style formatting." The Python community is still trying to oust this in favor of the .format() method, eg 'User {} requesting bike {}'.format(user_email, bike_id). The {} is where the parameters will fall into and you can do lots of things with it, in fact the CFG that describes this new format string can be found here.

return "Bike request"