Color codes not stripped from records with colorize=False
See original GitHub issueWhen adding a function sink with colorize=False
the message
attribute of the record does not have the color codes stripped. Here’s a short example showing the issue I mean:
from loguru import logger
def test_sink(message):
assert "<red>" not in message # This is fine
assert "<red>" not in message.record["message"] # This fails
logger.add(test_sink, colorize=False)
logger.opt(ansi=True).info("<red>something</>")
This is a problem for example when sending log records over the network, as in this recipe. https://loguru.readthedocs.io/en/stable/resources/recipes.html#sending-and-receiving-log-messages-across-network-or-processes
On the other side of the network, the record message still contains color codes, but we have no way of transmitting that the message should be processed with the .opt(ansi=True
anymore. Perhaps rather than (or maybe in addition to) .opt()
modifying the logger, the options should be attached to the log record?
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
Removing ANSI color codes from text stream - Super User
How would one remove the ANSI color codes from the output file? I suppose the best way would be to pipe the output...
Read more >How to strip color codes used by mIRC users? - Stack Overflow
The issue is that some mIRC users and some Bots write using color codes. Any idea on how i could strip those parts...
Read more >Record Coloring by Conditions - Airtable Support
Adding a condition to color-code your records. Click the "Color" option in the view bar to color your records by conditions, ...
Read more >What color codes can I use in my Bash PS1 prompt?
Not all of them work on (e.g.) a normal Linux console. This is incorrect: \033]00m\] # white. 0 resets the terminal to its...
Read more >Color regions in Logic Pro - Apple Support
Change the color of regions in the Logic Pro Tracks area. ... Newly recorded or added regions use the color of the track...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You’re right, it will not work well with multiple sinks on both server and client side. 😕
In any case, it doesn’t seem possible to correct this considering #197: both color tags and logging
args
are required as is during the formatting process (to handle coloring or stripping correctly), but it’s complicated for the client to propagate them separately to the server (meaning themessage
must not have been formatted).I don’t think there is any proper solution I could implement. In my opinion, we have to accept the fact that color tags in the message cannot be transmitted through the network.
Here is another possible workaround, though: you could force
colorize=True
on the client side, and manually strip ansi codes in your sink (instead of relying oncolorize
) if needed in your server sinks (usingansistrip
for example).Fixed in
v0.4.1
: therecord["message"]
will now always contain the stripped message.