Created
February 2, 2017 18:20
-
-
Save jakubczaplicki/c9077a482c2b195b7f3bcbee19dde441 to your computer and use it in GitHub Desktop.
Flask Logging Logger Configuration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This is no longer valid for Flask 1.0. see docs: http://flask.pocoo.org/docs/1.0/api/#flask.Flask.logger
the Logger for Flask should be named
flask.app
no need for