· 9 years ago · Sep 14, 2016, 01:08 PM
1import logging
2
3from flask import Flask
4
5app = Flask(__name__)
6
7@app.route('/')
8def about():
9 app.logger.warning('A warning message is sent.')
10 app.logger.error('An error message is sent.')
11 app.logger.info('Information: 3 + 2 = %d', 5)
12 return "about"
13
14#config.py
15class BaseConfig(object):
16 DEBUG = False
17 TESTING = False
18 # sqlite :memory: identifier is the default if no filepath is present
19 SQLALCHEMY_DATABASE_URI = 'sqlite://'
20 SECRET_KEY = '1d94e52c-1c89-4515-b87a-f48cf3cb7f0b'
21 LOGGING_FORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
22 LOGGING_LOCATION = 'bookshelf.log'
23 LOGGING_LEVEL = logging.DEBUG
24
25
26def configure_app(app):
27 config_name = os.getenv('FLASK_CONFIGURATION', 'default')
28 app.config.from_object(config[config_name])
29 app.config.from_pyfile('config.cfg', silent=True)
30 # Configure logging
31 handler = logging.FileHandler(app.config['LOGGING_LOCATION'])
32 handler.setLevel(app.config['LOGGING_LEVEL'])
33 formatter = logging.Formatter(app.config['LOGGING_FORMAT'])
34 handler.setFormatter(formatter)
35 app.logger.addHandler(handler)