Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to process message before it's logged in specific handler? #903

Closed
changchiyou opened this issue Jun 27, 2023 · 3 comments
Closed

How to process message before it's logged in specific handler? #903

changchiyou opened this issue Jun 27, 2023 · 3 comments
Labels
question Further information is requested

Comments

@changchiyou
Copy link

I want to solve the problem about message consists with color-syntax #901.

Goal

  1. keep the color-syntax in consoles:

    image
  2. remove the color-syntax in logs:

    image

My (temporary) solution:

Design a new function to clean the color-syntax from record["message"]:

def remove_color_syntax(record) -> bool:
    """
    Remove the color-syntax from `record["message"]` string.

    ### Reference
    - https://chat.openai.com/share/26ff7e69-5f1b-4a71-a8bd-613270a03ac1
    """

    # https://zhuanlan.zhihu.com/p/70680488
    # `re.compile` is no need in python but it's a good development habits
    ansi_escape = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])")
    # remove color-syntax from message
    record["message"] = ansi_escape.sub("", record["message"])

    return True

Then pass the function remove_color_syntax as the filter parameter of logger.add:

    logger.add(
        console_sink,
        level=console_level,
        colorize=True,
        format=VARS.CONSOLE_FORMAT if console_format is None else console_format,
        enqueue=enqueue,
    )
    logger.add(
        f"{ARGS.LOG_PATH}/{VARS.BASE_LOG_FILE_NAME}" if log_path is None else log_path,
        level=log_level,
        rotation=VARS.ROTATION if rotation is None else rotation,
        compression=rename if compression is None else compression,
        format=VARS.LOG_FORMAT if log_format is None else log_format,
        enqueue=enqueue,
        colorize=False,
        filter=remove_color_syntax,
    )

❓ The problem is, my temporary solution adjust the value inside record directly, which is very unstable in my opinion. Because the order of these two logger.add() is fixed(first one is for consoles, and the second one is for logs).

@Delgan
Copy link
Owner

Delgan commented Jun 27, 2023

Hi.

Instead of modifying record["message"], I would suggest instead to add the processed message to a new entry in record["extra"] dict, then use it in your format.

def remove_color_syntax(record) -> bool:
    ansi_escape = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])")
    record["extra"]["message_no_colors"] = ansi_escape.sub("", record["message"])
    return True

logger.add("file.log", filter=remove_color_syntax, format="{time} {level} {extra[message_no_colors]}")

@Delgan Delgan added the question Further information is requested label Jun 27, 2023
@changchiyou
Copy link
Author

@Delgan Thanks! Your response helps me a lot 😄

@drjmcauliffe
Copy link

@Delgan thanks a lot for this. I was trying for sometime to "simply" patch the record["message"] via .replace() and couldn't get it to work... using the extra[updated_message] with a modified format worked first time.

Awesome library! I'm only starting to discover the power of it...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants