Comments (6)
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.
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_config
s 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.
from click-log.
@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.
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.
👋 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 findlevelname
. 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 outputslevelname
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)
- Provide a way to customise the formatter HOT 2
- Breaking change in API HOT 4
- stderr HOT 4
- Unformatted log messages in 0.3.0 HOT 1
- Not clear on setting file handler and format HOT 1
- Color on info level HOT 2
- How can I use click_log in a project with multiple modules? HOT 1
- How can I write log data to a file?
- Document the default log level
- Log level is leaking between invokations: hack to force-reset it HOT 2
- Add a `no-color` option, method or parameter to disable colouring globally HOT 2
- The short option `-v` as `--verbosity` is not documented. HOT 2
- How to use click-log with multi-file program? HOT 2
- this should be incoroprated to click rather than being a standalone repo HOT 1
- use with structlog documentation
- click_log only applies to the initial module's logger. HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from click-log.