lolozen
lolozen

Reputation: 426

remove bash colors when logging to file in flask

I'm trying to modify the flask request formater so I get some logs on the console but I want the logs in my file to be colorless since those basically add ANSI characters that make the log reading harder.

Here is what I have done until now, I made a class to setup my logs:

from logging.config import dictConfig
from logging import Formatter
import re
from flask import has_request_context, request

def configure_logging():
    """
    Configure the logging system
    """
    dictConfig({
        'version': 1,
        'formatters': {
            'common': {
                'format': '%(asctime)s - %(levelname)s - %(message)s',
                'datefmt': '%Y-%m-%d %H:%M:%S',
                '()': 'utils.log_utils.CustomFormatter'
            },
            'error': {
                'format': '%(asctime)s - %(levelname)s - %(module)s - %(lineno)d - %(message)s',
                'datefmt': '%Y-%m-%d %H:%M:%S',
                '()': 'utils.log_utils.CustomFormatter'
            }
        },
        'filters': {
            'common': {
                '()': 'utils.log_utils.CommonFilter'
            }
        },
        'handlers': {
            'console': {
                'class': 'logging.StreamHandler',
                'level': 'INFO',
            },
            'common': {
                'class': 'logging.handlers.TimedRotatingFileHandler',
                'formatter': 'common',
                'level': 'DEBUG',
                'filters': ['common'],
                'filename': 'logs/app.log',
                'encoding': 'utf-8',
                'when': 'W0',
                'backupCount': 3
            },
            'error': {
                'class': 'logging.handlers.TimedRotatingFileHandler',
                'formatter': 'error',
                'level': 'ERROR',
                'filename': 'logs/app.log',
                'encoding': 'utf-8',
                'when': 'W0',
                'backupCount': 3
            }
        },
        'root': {
            'handlers': ['console','common', 'error'],
            'level': 'DEBUG'
        }
    })

class CustomFormatter(Formatter):
    """
    Custom formatter to remove ANSI escape sequences from log messages
    """
    def format(self, record):
        # Remove ANSI escape sequences
        record.msg = re.sub(r'\x1b\[[0-?]*[ -/]*[@-~]', '', record.msg)
        if has_request_context():
            record.url = re.sub(r'\x1b\[[0-?]*[ -/]*[@-~]', '', record.url)
            record.remote_addr = re.sub(r'\x1b\[[0-?]*[ -/]*[@-~]', '', record.remote_addr)
            record.method = re.sub(r'\x1b\[[0-?]*[ -/]*[@-~]', '', record.method)
            record.user_agent = re.sub(r'\x1b\[[0-?]*[ -/]*[@-~]', '', record.user_agent)
        return super().format(record)


class CommonFilter:
    """
    Filter to exclude log errors warning and critical
    """
    def filter(self, record):
        return record.levelno < 30

When I check my log file, I see that most of the color characters disappeared but when I get a request log like this:

enter image description here

I have a log input looking like:

2024-10-31 12:00:34 - INFO - 172.18.0.1 - - [31/Oct/2024 12:00:34] "[33mPOST /webhook/2 HTTP/1.1[0m" 404 -

It feels like some already formated log is being put into my log by the look of the two timestamp, but I can't figure out how to fix that and get two distinct logging formatting.

Upvotes: 0

Views: 60

Answers (1)

mpivet-p
mpivet-p

Reputation: 244

If you just want to remove the formatting and don't necessarily need to modify the logs, you can just use the following snippet after your imports. Not so much configurable, but it does the job.

import werkzeug

werkzeug.serving._log_add_style = False

Upvotes: 0

Related Questions