question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Color codes not stripped from records with colorize=False

See original GitHub issue

When 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:closed
  • Created 4 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
Delgancommented, Jan 10, 2020

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 the message 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 on colorize) if needed in your server sinks (using ansistrip for example).

# server.py
from ansistrip import ansi_strip

logger.add(sink_supporting_colors)
logger.add(lambda m: sink_not_supporting_colors(ansi_strip(m)))
0reactions
Delgancommented, Jan 19, 2020

Fixed in v0.4.1: the record["message"] will now always contain the stripped message.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found