I
I
Ilyas2017-12-13 00:42:48
Journaling
Ilyas, 2017-12-13 00:42:48

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

so
# app/__init__.py

import logging

def create_app():
    ....
    logger = logging.getLogger("app.module_one")
    logger.setLevel(logging.ERROR)
    ....
    return app

In this I get info from werkzeug, mine are not there
INFO:werkzeug:127.0.0.1 - - [13/Dec/2017 00:26:15] "GET /index/ HTTP/1.1" 200 -

Mine work only if you specify directly in the view
# app/module_one/controllers.py

@module.route('/index/')
def index():
    ....
    logging.basicConfig(level=logging.INFO)
    logging.info('My message')
    ....


Can this be done somehow for the entire module?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pcdesign, 2017-12-13
@id2669099

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)

And a short hello, if you need to throw the log into the console:
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 question

Ask a Question

731 491 924 answers to any question