Git Product home page Git Product logo

Comments (6)

untitaker avatar untitaker commented on June 17, 2024

click_log.basic_config works the same. You call it with the root logger or the logger you want to use at import time. The only thing that it does not do is to set the level, since that might be dependent on a CLI option. You can use simple_verbosity_option for that.

What are you missing from the docs? They contain an example where it's used after all.

from click-log.

flokli avatar flokli commented on June 17, 2024

This was more related to the format parameter that logging.basic_config(format=…) exposes, but click_log.basic_config() currently doesn't.

In my usecase I'd like to keep the coloring depending on log level as prefix, but change the rest of the printed text to include the loggers name, date etc, without having to duplicate most of the color handling in ColorFormatter.

What's your opinion on changing click_log.basic_configs signature to basic_config(logger=None, format='%(message)s'), and adding needed wiring to ColorFormatter? If somebody doesn't want the color formatting, he could simply use another Formatter, but configuring the rest of the format string inside ColorFormatter should be made possible.

from click-log.

untitaker avatar untitaker commented on June 17, 2024

from click-log.

flokli avatar flokli commented on June 17, 2024

@untitaker I had a look into it.

We already subclass logging.Formatter, to which we could pass a custom format parameter, and simply use super().format(record) and click.style inside of our format(self, record) implementation - with getting support for all LogRecord parameters for free as a bonus :-)

However, honoring a passed format string would also mean and not treating messages differently depending on a log level, which is currently intended behaviour.

In addition, we'd only be able to color the message as a whole (by calling click.style on the string returned from super().format()). Working around that by passing a copy of the original record with color escape strings on the loglevel attribute to super().format() looks really hacky for me.

So what's your opinion on it? I'd really like to see support for the format parameter and friends in click-log, rather than having users subclassing on their own…

from click-log.

untitaker avatar untitaker commented on June 17, 2024

You're suggesting this:

basic_config(format='...')

I would suggest:

basic_config(formatter=MyFormatter(format='...'))

Feel free to include a formatter you'd like (separate from ColorFormatter) in the PR too, but also with a default format string so I can understand what you're doing with it.

My usecase for the current logger is: simple command line applications where the output is usually not written to a file.

from click-log.

jamestwebber avatar jamestwebber commented on June 17, 2024

👋 hello all, happened across this issue because I wanted my nicely-formatted logs to have fancy colors.

This is a bit of a hack but it does work:

class ColorFormatter(logging.Formatter):
    colors = {
        'ERROR': dict(fg='red'),
        'EXCEPTION': dict(fg='red'),
        'CRITICAL': dict(fg='red'),
        'DEBUG': dict(fg='blue'),
        'WARNING': dict(fg='yellow'),
        'INFO': dict(fg='green'),
    }

    def format(self, record):
        formatted_msg = super().format(record)

        if self._fmt.find("levelname") > -1:
            formatted_msg = formatted_msg.replace(
                record.levelname,
                click.style(record.levelname, **self.colors[record.levelname])
            )

        return formatted_msg 

A couple notes:

  • the main hackiness is using replace to find levelname. If your message happens to contain the same string, that will get the color formatting too. This was a heck of a lot easier than supporting all three of the logging formats, including potential formatting like %(levelname)5s, but it feels a bit gross
  • click_log usually outputs levelname in lowercase, the above code does not. This could easily be accomplished with a little tweaking. Ideally it'd be an option when setting up a formatter.
  • there was no color for INFO and rather than special-case it I just made it green (might as well hit #19 :) )

from click-log.

Related Issues (17)

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.