Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 105 characters in body
Source Link
Ben Yitzhaki
  • 1.4k
  • 17
  • 31

There are several answers. i ended up with a similar yet different solution that makes sense to me, maybe it will make sense to you as well. My main objective was to be able to pass logs to handlers by their level (debug level logs to the console, warnings and above to files):

from flask import Flask import logging from logging.handlers import RotatingFileHandler app = Flask(__name__) # make default logger output everything to the console logging.basicConfig(level=logging.DEBUG) rotating_file_handler = RotatingFileHandler(filename="logs.log") rotating_file_handler.setLevel(logging.INFO) app.logger.addHandler(rotating_file_handler) 

created a nice util file named logger.py:

import logging def get_logger(name): return logging.getLogger("flask.app." + name) 

the flask.app is a hardcoded value in flask. the application logger is always starting with flask.app as its the module's name.

now, in each module, i'm able to use it in the following mode:

from logger import get_logger logger = get_logger(__name__) logger.info("new log") 

This will create a new log for "app.flask.MODULE_NAME" with minimum effort.

There are several answers. i ended up with a similar yet different solution that makes sense to me, maybe it will make sense to you as well. My main objective was to be able to pass logs to handlers by their level (debug level logs to the console, warnings and above to files):

app = Flask(__name__) # make default logger output everything to the console logging.basicConfig(level=logging.DEBUG) rotating_file_handler = RotatingFileHandler(filename="logs.log") rotating_file_handler.setLevel(logging.INFO) app.logger.addHandler(rotating_file_handler) 

created a nice util file named logger.py:

import logging def get_logger(name): return logging.getLogger("flask.app." + name) 

the flask.app is a hardcoded value in flask. the application logger is always starting with flask.app as its the module's name.

now, in each module, i'm able to use it in the following mode:

from logger import get_logger logger = get_logger(__name__) logger.info("new log") 

This will create a new log for "app.flask.MODULE_NAME" with minimum effort.

There are several answers. i ended up with a similar yet different solution that makes sense to me, maybe it will make sense to you as well. My main objective was to be able to pass logs to handlers by their level (debug level logs to the console, warnings and above to files):

from flask import Flask import logging from logging.handlers import RotatingFileHandler app = Flask(__name__) # make default logger output everything to the console logging.basicConfig(level=logging.DEBUG) rotating_file_handler = RotatingFileHandler(filename="logs.log") rotating_file_handler.setLevel(logging.INFO) app.logger.addHandler(rotating_file_handler) 

created a nice util file named logger.py:

import logging def get_logger(name): return logging.getLogger("flask.app." + name) 

the flask.app is a hardcoded value in flask. the application logger is always starting with flask.app as its the module's name.

now, in each module, i'm able to use it in the following mode:

from logger import get_logger logger = get_logger(__name__) logger.info("new log") 

This will create a new log for "app.flask.MODULE_NAME" with minimum effort.

Source Link
Ben Yitzhaki
  • 1.4k
  • 17
  • 31

There are several answers. i ended up with a similar yet different solution that makes sense to me, maybe it will make sense to you as well. My main objective was to be able to pass logs to handlers by their level (debug level logs to the console, warnings and above to files):

app = Flask(__name__) # make default logger output everything to the console logging.basicConfig(level=logging.DEBUG) rotating_file_handler = RotatingFileHandler(filename="logs.log") rotating_file_handler.setLevel(logging.INFO) app.logger.addHandler(rotating_file_handler) 

created a nice util file named logger.py:

import logging def get_logger(name): return logging.getLogger("flask.app." + name) 

the flask.app is a hardcoded value in flask. the application logger is always starting with flask.app as its the module's name.

now, in each module, i'm able to use it in the following mode:

from logger import get_logger logger = get_logger(__name__) logger.info("new log") 

This will create a new log for "app.flask.MODULE_NAME" with minimum effort.