Giter Site home page Giter Site logo

Comments (5)

xolox avatar xolox commented on May 20, 2024 1

Hi again @con-f-use and sorry for the long silence here. I've just released coloredlogs 10.0 which should fix the issue you reported: coloredlogs is now finally able to properly distinguish formatting directives from their surrounding text, dropping the dependency on whitespace delimiters.

from python-coloredlogs.

xolox avatar xolox commented on May 20, 2024

Yes this is intended behavior. The current approach is documented under the colorize_format() method. Basically I decided to implement it like this (the "simple" approach) until someone came up with a use case not covered by the simple approach. And here you are! :-)

So consider this a documented implementation detail. If we can find a way to improve this that doesn't break backwards compatibility (at least not in a surprising way) I'm open to changing the implementation.

from python-coloredlogs.

xolox avatar xolox commented on May 20, 2024

An advantage of the current simple approach (from my perspective) is that it's possible to highlight things like %(name)s[%(process)d] as a single logical unit with a single color that encompasses the [ and ] characters. It also helps that coloredlogs doesn't need to fully understand and (properly) parse the format of logging format strings.

Making the current behavior more flexible introduces more decisions to make. For example if the format string contains %(name)s[%(process)d] and styling has been specified for both name and process, what should the color of the [ and ] tokens be? Just leave them at the default color? That looks kind of weird, but maybe that's just me :-).

The following approach might work:

  1. We split the format string on whitespace resulting in a list of whitespace separated tokens.
  2. For each whitespace separated token the following decision process is followed:
    • If the token contains only one %(...) field with a color/style defined then the complete token is highlighted in that color/style (this provides compatibility with the current behavior).
    • If the token contains multiple %(...) fields with a color/style defined then each %(...) field is highlighted according to the configured color/style but any text in between is left as is.

This change in behavior requires real/proper parsing of logging format strings. This isn't impossible to do of course, but it might be a bit tricky to get it right :-). So one the one hand this complicates the library but on the other hand it can guarantee correct behavior (provided that the format string parsing and rewriting is correct of course). In contrast the current approach is kind of dumb but works fine most of the time (it's possible to break it though, I guess).

from python-coloredlogs.

con-f-use avatar con-f-use commented on May 20, 2024

Sounds good. Another approach would be to introduce a reset string, that ends the current colouring, or colour parenthesis strings respectively.

from python-coloredlogs.

 avatar commented on May 20, 2024

I'm still seeing a similar issue with coloredlogs 15.0.1. With fmt="{asctime} [{levelname[0]}] {name}: {message}", the trailing colon after the name field is colored as well. If I change it to fmt="{asctime} [{levelname[0]}] {name} : {message}", the colon is not colored.

from python-coloredlogs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.