Skip to content

Commit v0.6

kwmccabe edited this page Apr 17, 2018 · 12 revisions

v0.6 - app.init_logging(), LOG_FILE, LOG_LEVEL, logging.info()


Files changed (3)

File web/app/init.py MODIFIED

  • Create method init_logging().
  • Set basic logging options using LOG_FILE and LOG_LEVEL from app.config.
  • Call logging.debug() to test.
     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
 
...
 
 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"

Commit-v0.5 | Commit-v0.6 | Commit-v0.7

Clone this wiki locally