You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi I'm trying to configure color output for formatting, but I'm loosing this battle, I have no idea what you did with logger, but everything I try works on "clear" logging, but not on logger coming with flask. Can you configure logs like this please ? Like, why python ["INFO"] log is red in console ?
This code works with logging, but not with flask.
class CustomFormatter(logging.Formatter):
grey = "\x1b[38;20m"
yellow = "\x1b[33;20m"
red = "\x1b[31;20m"
bold_red = "\x1b[31;1m"
reset = "\x1b[0m"
format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s (%(filename)s:%(lineno)d)"
FORMATS = {
logging.DEBUG: grey + format + reset,
logging.INFO: grey + format + reset,
logging.WARNING: yellow + format + reset,
logging.ERROR: red + format + reset,
logging.CRITICAL: bold_red + format + reset
}
def format(self, record):
print(record)
log_fmt = self.FORMATS.get(record.levelno)
formatter = logging.Formatter(log_fmt)
return formatter.format(record)
logger = logging.getLogger("tmp")
logger.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(CustomFormatter())
logger.addHandler(ch)
logger.info("Hello")
logger.error("Hello")
logger.debug("Hello")
logger.warning("Hello")
This discussion was converted from issue #5310 on October 27, 2023 13:52.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi I'm trying to configure color output for formatting, but I'm loosing this battle, I have no idea what you did with logger, but everything I try works on "clear" logging, but not on logger coming with flask. Can you configure logs like this please ? Like, why python ["INFO"] log is red in console ?
This code works with logging, but not with flask.
Beta Was this translation helpful? Give feedback.
All reactions