Skip to content

Instantly share code, notes, and snippets.

@rizplate
Forked from jakubczaplicki/app.py
Created June 4, 2018 23:49
Show Gist options
  • Save rizplate/cdbcf2703c42f22f8d42a22d859ab848 to your computer and use it in GitHub Desktop.
Save rizplate/cdbcf2703c42f22f8d42a22d859ab848 to your computer and use it in GitHub Desktop.
Flask Logging Logger Configuration
import sys
import logging
import logging.config
from flask import Flask
class LoggerConfig:
dictConfig = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'standard': { 'format': '%(asctime)s - %(name)s - %(levelname)s - '
'%(message)s - [in %(pathname)s:%(lineno)d]'},
'short': { 'format': '%(message)s' }
},
'handlers': {
'default': {
'level': 'DEBUG',
'formatter': 'standard',
'class': 'logging.handlers.RotatingFileHandler',
'filename': 'femm-api.log',
'maxBytes': 5000000,
'backupCount': 10
},
'debug': {
'level': 'DEBUG',
'formatter': 'standard',
'class': 'logging.StreamHandler'
},
'console': {
'class': 'logging.StreamHandler',
'level': 'DEBUG'
},
},
'loggers': {
'femmapi': {
'handlers': ['default'],
'level': 'DEBUG',
'propagate': True},
'werkzeug': { 'propagate': True },
},
# 'root': { 'level': 'DEBUG', 'handlers': ['console'] }
}
app = Flask(__name__)
app.config['LOGGER_HANDLER_POLICY'] = 'always' # 'always' (default), 'never', 'production', 'debug'
app.config['LOGGER_NAME'] = 'femmapi' # define which logger to use for Flask
app.logger # initialise logger
if len(sys.argv) > 1 and sys.argv[1] == 'debug':
app.debug = True
logging.config.dictConfig(LoggerConfig.dictConfig)
print(app.logger.name)
app.logger.debug('debug message')
app.logger.info('info message')
app.logger.warn('warn message')
app.logger.error('error message')
app.logger.critical('critical message')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment