Answer the question
In order to leave comments, you need to log in
How can I display info level logs in flask?
Hello, I want to generate INFO level logs to the console for certain events in views, I use Flask.
I tried in create_app () to do this:
# app/__init__.py
import logging
def create_app():
app = Flask(__name__)
app.logger.setLevel(logging.INFO)
....
return app
# app/__init__.py
import logging
def create_app():
....
logger = logging.getLogger("app.module_one")
logger.setLevel(logging.ERROR)
....
return app
INFO:werkzeug:127.0.0.1 - - [13/Dec/2017 00:26:15] "GET /index/ HTTP/1.1" 200 -
# app/module_one/controllers.py
@module.route('/index/')
def index():
....
logging.basicConfig(level=logging.INFO)
logging.info('My message')
....
Answer the question
In order to leave comments, you need to log in
This is how I log and rotate
# config.py
class Config(object):
TESTING = False
# И еще конфиги разные
class ProductionConfig(Config):
DEBUG = False
LOGFILE = 'logs/Production.log'
class DevelopmentConfig(Config):
DEBUG = True
LOGFILE = 'logs/Development.log'
# app.py
from logging.handlers import RotatingFileHandler
from logging import Formatter
import logging
app = Flask(__name__)
app.config.from_object('config.DevelopmentConfig')
# -----------------------------------------------------------------------------
# Включение, отключение и ротация логов.
# -----------------------------------------------------------------------------
handler = RotatingFileHandler(app.config['LOGFILE'],
maxBytes=1000000, backupCount=1)
handler.setLevel(logging.DEBUG)
handler.setFormatter(Formatter('%(asctime)s %(levelname)s: %(message)s '
'[in %(pathname)s:%(lineno)d]'))
# logging.disable(logging.CRITICAL) # Расскоментарь это для прекращения логов
app.logger.addHandler(handler)
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
app.logger.info("My message")
return 'Hello, World!'
if __name__ == "__main__":
app.run(debug=True)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question