21

Flask by default will log things like GET and POST requests directly with an INFO tag. When implementing a custom logger, these are posted to the same logger and clutter my INFO layer. Is there a way to downgrade them to another layer like DEBUG?

This is the logger I use:

# create logger
FORMAT = '%(asctime)s - %(module)s - %(levelname)s - Thread_name: %(threadName)s - %(message)s'
logging.basicConfig(
    format=FORMAT, datefmt='%m/%d/%Y %I:%M:%S %p',
    filename='wizard/logs/example.log', level=logging.DEBUG)
ZekeDroid
  • 7,089
  • 5
  • 33
  • 59

3 Answers3

29

I'm not sure about a way to downgrade log level of requests (as it usually is stated explicitly in the code like logging.info("...")) but the following may help you to reduce the verbosity of Flask itself.

Python allows you to have multiple loggers, each with its own log level. You can modify any existing logger if you know the module name it is in or the name it was registered with as described here.

For example:

import logging
logger = logging.getLogger("mypackage.mymodule")  # or __name__ for current module
logger.setLevel(logging.ERROR)

The above can be done for any python module. Flask provides a logger per app. You can get a reference to it like this:

import logging
from flask import Flask
app = Flask(__name__)  # or instead of __name__ provide the name of the module
app.logger.setLevel(logging.ERROR)
foobarto
  • 544
  • 5
  • 5
  • Right the thing is that by setting the level in the app to ERROR, although it removes the INFO logs from Flask, it doesn't allow for me to have DEBUG level logs without seeing the INFO, right? I would have to turn all my DEBUG logs to ERROR, at least, to get it working. Even if I use the getLogger function, it still seems like Flask logs the INFO logs to my logger... – ZekeDroid May 07 '15 at 01:45
  • Did you create a custom logger as describe by @foobarto? And that is reporting both your custom logs and flask logs? – amccormack May 07 '15 at 21:04
10

In fact, there is a way to downgrade a log record from INFO to DEBUG (even if it was already emitted using a call such as info()). This can be achieved using a filter attached to a logger. According to the docs a filter checks:

Is the specified record to be logged? Returns zero for no, nonzero for yes. If deemed appropriate, the record may be modified in-place by this method.

So a filter may change the level of a log record (levelno and levelname attributes). Later on, a handler may then allow or drop this record based on the new level.

Piotr Ćwiek
  • 1,580
  • 13
  • 19
1

Just in case anyone visits this place,

I'm facing the same issue as well. It seems that print calls have the tendency to 'not run'. But if you use the logger instead of print you will find that codes are actually still running.

Not quite sure what the reason is, but when other sections of my code starts running suddenly all the old prints will appear.

tldr; don't use print, use logger

Elixander Chen
  • 119
  • 1
  • 6
  • 1
    Python buffers the `print` function, probably that is why you see it that way. You can get around it a few ways, see here in more detail: https://stackoverflow.com/questions/107705/disable-output-buffering – fbence Apr 22 '19 at 12:23