def __init__( self, cfg ): super( Logger, self ).__init__( cfg ) access_handler = MongoHandler( level = INFO, host = MONGOLAB.hostname, port = MONGOLAB.port, database_name = MONGOLAB.path[ 1: ], collection = 'access-logs', username = MONGOLAB.username, password = MONGOLAB.password ) error_handler = MongoHandler( level = INFO, host = MONGOLAB.hostname, port = MONGOLAB.port, database_name = MONGOLAB.path[ 1: ], collection = 'error-logs', username = MONGOLAB.username, password = MONGOLAB.password ) access_handler.setFormatter( MongoFormatter() ) error_handler.setFormatter( MongoFormatter() ) self.access_log.addHandler( access_handler ) self.access_log.setLevel( INFO ) self.error_log.addHandler( error_handler ) self.error_log.setLevel( INFO )
def __init__( self, cfg ): super( Logger, self ).__init__( cfg ) access_handler = MongoHandler( level = INFO, collection = 'access-logs', **DB( 'MONGOLAB_URI' ).URI ) error_handler = MongoHandler( level = INFO, collection = 'error-logs', **DB( 'MONGOLAB_URI' ).URI ) access_handler.setFormatter( MongoFormatter() ) error_handler.setFormatter( MongoFormatter() ) self.access_log.addHandler( access_handler ) self.access_log.setLevel( INFO ) self.error_log.addHandler( error_handler ) self.error_log.setLevel( INFO )
def setup_logging( name ): root_logger = getLogger( name ) if PRODUCTION: handler = MongoHandler( level = DEBUG, collection = 'application-log', **MONGOLAB_CONF_DICT ) handler.setFormatter( MongoFormatter() ) else: handler = StreamHandler() handler.setLevel( DEBUG ) handler.setFormatter( Formatter( '%(asctime)s [%(process)d] [%(levelname)s/APPLICATION] %(message)s', '%Y.%m:%d %H:%M:%S' ) ) root_logger.setLevel( DEBUG ) root_logger.addHandler( handler )
def __init__(self, cfg): super(Logger, self).__init__(cfg) access_handler = MongoHandler(level=INFO, collection='access-logs', **DB('MONGOLAB_URI').URI) error_handler = MongoHandler(level=INFO, collection='error-logs', **DB('MONGOLAB_URI').URI) access_handler.setFormatter(MongoFormatter()) error_handler.setFormatter(MongoFormatter()) self.access_log.addHandler(access_handler) self.access_log.setLevel(INFO) self.error_log.addHandler(error_handler) self.error_log.setLevel(INFO)
def setup_logging(name): root_logger = getLogger(name) if PRODUCTION: handler = MongoHandler(level=DEBUG, collection='application-log', **MONGOLAB_CONF_DICT) handler.setFormatter(MongoFormatter()) else: handler = StreamHandler() handler.setLevel(DEBUG) handler.setFormatter( Formatter( '%(asctime)s [%(process)d] [%(levelname)s/APPLICATION] %(message)s', '%Y.%m:%d %H:%M:%S')) root_logger.setLevel(DEBUG) root_logger.addHandler(handler)
def __init__(self, cfg): super(Logger, self).__init__(cfg) access_handler = MongoHandler(level=INFO, host=MONGOLAB.hostname, port=MONGOLAB.port, database_name=MONGOLAB.path[1:], collection='access-logs', username=MONGOLAB.username, password=MONGOLAB.password) error_handler = MongoHandler(level=INFO, host=MONGOLAB.hostname, port=MONGOLAB.port, database_name=MONGOLAB.path[1:], collection='error-logs', username=MONGOLAB.username, password=MONGOLAB.password) access_handler.setFormatter(MongoFormatter()) error_handler.setFormatter(MongoFormatter()) self.access_log.addHandler(access_handler) self.access_log.setLevel(INFO) self.error_log.addHandler(error_handler) self.error_log.setLevel(INFO)
def __init__( self, cfg ): super( GunicornLogger, self ).__init__( cfg ) if PRODUCTION: access_handler = MongoHandler( level = INFO, collection = 'access-log', **MONGOLAB_CONF_DICT ) error_handler = MongoHandler( level = INFO, collection = 'error-log', **MONGOLAB_CONF_DICT ) access_handler.setFormatter( MongoFormatter() ) error_handler.setFormatter( MongoFormatter() ) self.error_log.addHandler( error_handler ) self.error_log.setLevel( INFO ) else: access_handler = StreamHandler() access_handler.setFormatter( Formatter( '%(asctime)s [%(process)d] [%(levelname)s/ACCESS] %(message)s', '%Y.%m:%d %H:%M:%S' ) ) self.access_log.addHandler( access_handler ) self.access_log.setLevel( INFO )
def __init__(self, cfg): super(GunicornLogger, self).__init__(cfg) if PRODUCTION: access_handler = MongoHandler(level=INFO, collection='access-log', **MONGOLAB_CONF_DICT) error_handler = MongoHandler(level=INFO, collection='error-log', **MONGOLAB_CONF_DICT) access_handler.setFormatter(MongoFormatter()) error_handler.setFormatter(MongoFormatter()) self.error_log.addHandler(error_handler) self.error_log.setLevel(INFO) else: access_handler = StreamHandler() access_handler.setFormatter( Formatter( '%(asctime)s [%(process)d] [%(levelname)s/ACCESS] %(message)s', '%Y.%m:%d %H:%M:%S')) self.access_log.addHandler(access_handler) self.access_log.setLevel(INFO)
# mongo logger, only logs the tweets, modified from log4Mongo class ExtraOnlyFormatter(logging.Formatter): def format(self, record): """ Formats tweet into python dictionary.""" return record.tweet class InfoOnlyFilter(logging.Filter): def filter(self, record): return record.levelname == "INFO" and hasattr(record, "tweet") from log4mongo.handlers import MongoHandler mongoHandler = MongoHandler(host="localhost", database_name="busfumes", collection="tweets") mongoHandler.setFormatter(ExtraOnlyFormatter()) mongoHandler.addFilter(InfoOnlyFilter()) # add formatter formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") timedHandler.setFormatter(formatter) ch.setFormatter(formatter) # add handlers logger.addHandler(timedHandler) logger.addHandler(ch) logger.addHandler(mongoHandler) ############## start scripts
from web import home from apis.home_api import Home app = Flask(__name__) config = app.config config.from_object('config') api = Api(app) try: app.logger.info('mongodb get_db request complete') environment = config['ACTIVE_ENV'] handler = MongoHandler(host=config['ENVS'][environment]['DB_HOST'], port=config['ENVS'][environment]['DB_PORT'], database_name=config['ENVS'][environment]['DB_NAME']) formatter = RequestFormatter() handler.setFormatter(formatter) logger = logging.getLogger(config["APP_NAME"]) logger.setLevel(logging.INFO) logger.addHandler(handler) app.logger = logger except pymongo.errors.ServerSelectionTimeoutError as e: message = "Could not connect to database" status = 403 app.logger.error('Could not connect to database',e) except Exception as e: message = "database connection error" status = 400 app.logger.error('database error',e) # APIs api.add_resource(Home, '/home')