Skip to content

Commit v0.6

kwmccabe edited this page Apr 10, 2018 · 12 revisions

Commit v0.6

app.init_logging(), LOG_FILE, LOG_LEVEL, logging.info()

Files changed (4)

File web/Dockerfile MODIFIED

-# copy code from current directory into working directory
+# copy project files into working directory

File web/app/init.py MODIFIED

Create method init_logging(). Set basic logging options using LOG_FILE and LOG_LEVEL from config. Call logging.debug() to test.

 from flask import Flask
 from config import config
 
     app.config.from_object(config[config_name])
     config[config_name].init_app(app)
 
+    init_logging(app)

     return app

+def init_logging(app):
+    import logging
+    logging.basicConfig( filename = app.config['LOG_FILE'], level = app.config['LOG_LEVEL'] )
+    logging.debug("init_logging( filename = %s, level = %i )" % (app.config['LOG_FILE'],app.config['LOG_LEVEL']))

File web/config.py MODIFIED

Set LOG_FILE and LOG_LEVEL in common AppConfig Override LOG_LEVEL per deployment stage

+import logging
+_basedir = os.path.abspath(os.path.dirname(__file__))
 
 class AppConfig(object):
     SECRET_KEY = os.environ.get('SECRET_KEY') or 'kp-cUHRAYH1n-sPaJICcEE2kOpU62mCk'
+    LOG_FILE = _basedir+'/logs/flaskapp.log'
+    LOG_LEVEL = logging.ERROR  # CRITICAL,ERROR,WARNING,INFO,DEBUG,NOTSET
 
     @staticmethod
     def init_app(app):
 
 class DevelopmentConfig(AppConfig):
     DEBUG = True
+    LOG_LEVEL = logging.DEBUG
 
 class TestingConfig(AppConfig):
     TESTING = True
+    LOG_LEVEL = logging.INFO
 
 
 class ProductionConfig(AppConfig):
     DEBUG = False
     TESTING = False
+    LOG_LEVEL = logging.WARNING
 
     @classmethod
     def init_app(cls, app):

File web/flaskapp.py MODIFIED

Add some logging.info() statements

+import logging
  
 @app.route('/')
 def hello_flaskapp():
+    logging.info("hello_flaskapp()")
     return 'Hello FlaskApp'
 
 
 @app.route('/debug')
 def hello_debug():
     import pdb; pdb.set_trace()
+    logging.info("hello_debug()")
     return "Hello Python Debugger"
Clone this wiki locally