-
Notifications
You must be signed in to change notification settings - Fork 704
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
Option to not raise on unknown color tags #197
Comments
Hum... Actually, wouldn't the most desirable default behavior be to totally ignore tags present in additional arguments? In For now I'm ignoring the technical feasibility of such solution, I'm just looking for the most convenient / less surprising behavior. Current approach seems like being completely wrong. 😕 |
Actually, that sounds great. I hadn't thought of it. |
This could be implemented by using a custom Formatter class when the ansi option was on. This would prevent it from running on unused arguments, as well as prevent it from running whenever ansi option wasn't on. Something like: class MarkupEscapingFormatter(string.Formatter):
def format_field(self, value, format_spec):
value = super().format_field(value, format_spec)
if isinstance(value, str):
value = re.sub(r"(\\?</?((?:[fb]g\s)?[^<>\s]*)>)", r"\\\1", value)
return value |
I think it's possible to implement a workaround without explicitly escaping arguments to be formatted. Basically, I will need to use Lines 242 to 268 in d4851c4
It's a bit tricky. Give me a few days to get my head around this. 😄 |
Oh interesting, nice. I had a peek to see if I could send over a PR, but it will probably take me longer to get my head around what's already going on there than for you to come up with a solution. |
First, ansi markup in "*args" and "**kwargs" are ignored when used while "opt(ansi=True)". This caused errors, as such arguments can contains tags which are not intended to be used as color markups. Secondly, it strips the color markups present in the "record[message]" so it can be used somewhere else where the "ansi" context is lost (like when logs are emitted through socket). Actually, the colors are part of the handler so it makes sense to strip them from the record. These change required a whole refactoring of the coloration process. It has been implemented with optimization in mind: trying to parse the ansi message only once, then using the generated tokens to colorize the message appropriately in each handler. It could certainly be improved, though.
Ok, I just released It has been quite a headache to refactor color handling between format, message and arguments while also optimizing it for performances. Fortunately, I think I ended up with something quite correct, although the code is rather complicated. Also, note that I deprecated Anyway, I think it will be much more convenient now that the arguments are no longer taken into account for coloring. It should have been like this from the beginning. |
I think it would be a nicer behavior to not raisie on unknown tags (or an option that could turn this off.) We ran into logging crashes with the first log lines we tried to color, because some reprs that ended up in the message were with angle brackets. The angle brackets were via a variable in the message, so escaping ends up being a bit cumbersome.
logger.opt(ansi=True).info('<green>colored message</> other stuff {here}', here=dobracketescaping(somevar))
I see this used to be the behavior, but got changed to help people get the color tags right in #57 .If you don't want to change this behavior back, maybe include a function, or formatter that does the escaping? I see in the docs there is a regex if the user wants to do it, but it might be nice to include. Potential methods:
The text was updated successfully, but these errors were encountered: