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
I want to solve the problem about message consists with color-syntax #901.
Goal
keep the color-syntax in consoles:
remove the color-syntax in logs:
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).
The text was updated successfully, but these errors were encountered:
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.
@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...
I want to solve the problem about message consists with color-syntax #901.
Goal
keep the color-syntax in consoles:
remove the color-syntax in logs:
My (temporary) solution:
Design a new function to clean the color-syntax from
record["message"]
:Then pass the function
remove_color_syntax
as thefilter
parameter oflogger.add
:❓ The problem is, my temporary solution adjust the value inside
record
directly, which is very unstable in my opinion. Because the order of these twologger.add()
is fixed(first one is for consoles, and the second one is for logs).The text was updated successfully, but these errors were encountered: