From d7ea47387f13f13e6586cc8d897bcf25dfb45827 Mon Sep 17 00:00:00 2001 From: Siddharth Gupta Date: Sun, 1 May 2016 07:59:08 -0700 Subject: [PATCH] enable timerotateloghandler (configurable) (#311) * attempt to enchance logging * clean up logging * clean up logging * reset to logger * clean up imports * add comments in config.py * remove redundant declaration of logging.config. Already exists in caravel/__init__.py * replace RotatingFileHandler with TimedRotatingFileHandler * revert back running web server in debug mode * fix debug in bin/caravel * resolve build errors - formatting * need to test * enable time rotateloghandler * revert back print statements - add feature for rotatetimelog which is needed and make it configurable * revert back to default in master * fix build issues * remove extra print statement * change log location to default * configure console log level and format --- caravel/__init__.py | 17 +++++++++++++---- caravel/config.py | 23 +++++++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/caravel/__init__.py b/caravel/__init__.py index 74dcbd558..72999097b 100644 --- a/caravel/__init__.py +++ b/caravel/__init__.py @@ -6,6 +6,7 @@ from __future__ import unicode_literals import logging import os +from logging.handlers import TimedRotatingFileHandler from flask import Flask, redirect from flask.ext.appbuilder import SQLA, AppBuilder, IndexView @@ -20,10 +21,6 @@ VERSION = version.VERSION_STRING APP_DIR = os.path.dirname(__file__) CONFIG_MODULE = os.environ.get('CARAVEL_CONFIG', 'caravel.config') -# Logging configuration -logging.basicConfig(format='%(asctime)s:%(levelname)s:%(name)s:%(message)s') -logging.getLogger().setLevel(logging.DEBUG) - app = Flask(__name__) app.config.from_object(CONFIG_MODULE) if not app.debug: @@ -37,6 +34,18 @@ cache = Cache(app, config=app.config.get('CACHE_CONFIG')) migrate = Migrate(app, db, directory=APP_DIR + "/migrations") +# Logging configuration +logging.basicConfig(format=app.config.get('LOG_FORMAT')) +logging.getLogger().setLevel(app.config.get('LOG_LEVEL')) + +if app.config.get('ENABLE_TIME_ROTATE'): + logging.getLogger().setLevel(app.config.get('TIME_ROTATE_LOG_LEVEL')) + handler = TimedRotatingFileHandler(app.config.get('FILENAME'), + when=app.config.get('ROLLOVER'), + interval=app.config.get('INTERVAL'), + backupCount=app.config.get('BACKUP_COUNT')) + logging.getLogger().addHandler(handler) + class MyIndexView(IndexView): @expose('/') diff --git a/caravel/config.py b/caravel/config.py index 134127f37..07df0b5bf 100644 --- a/caravel/config.py +++ b/caravel/config.py @@ -130,6 +130,29 @@ CACHE_CONFIG = {'CACHE_TYPE': 'null'} VIZ_TYPE_BLACKLIST = [] +""" +1) http://docs.python-guide.org/en/latest/writing/logging/ +2) https://docs.python.org/2/library/logging.config.html +""" + +# Console Log Settings + +LOG_FORMAT = '%(asctime)s:%(levelname)s:%(name)s:%(message)s' +LOG_LEVEL = 'DEBUG' + +# --------------------------------------------------- +# Enable Time Rotate Log Handler +# --------------------------------------------------- +# LOG_LEVEL = DEBUG, INFO, WARNING, ERROR, CRITICAL + +ENABLE_TIME_ROTATE = False +TIME_ROTATE_LOG_LEVEL = 'DEBUG' +FILENAME = '/tmp/caravel.log' +ROLLOVER = 'midnight' +INTERVAL = 1 +BACKUP_COUNT = 30 + + try: from caravel_config import * # noqa except Exception: