Skip to content
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

Read logging format #7

Closed
boompig opened this issue Feb 12, 2015 · 2 comments
Closed

Read logging format #7

boompig opened this issue Feb 12, 2015 · 2 comments

Comments

@boompig
Copy link

boompig commented Feb 12, 2015

This module should output in same format as specified by logging.basicConfig.

@htgoebel
Copy link

My approach would be:
Implement a new class ColoredFormatter(logging.Formatter) which does the following:

  1. In __init__: Replace the formatting sting like %(name)s with an ansi-wraped version. Something like
if istty:
  for s, kw in (
          ('name', {color: 'green'}),
          ('pathname', ...)
          ('loggername', ...), ...):
     s = '%(' + s + ')s'
     fmt = fmt.replace(s, ansi_wrap(s, *kw)

Later this can be enhanced by using regepx to detect more formating instructions.

  1. In format(): Instead of s = self._fmt % record.__dict__ use something like this:
is istty and '%(levelname)' in fmt:
  data = record.__dict__.copy()
  data['levelname'] =  ansi_warptext(data['levelname'], **self.severity_to_style[data['levelname'])
  ... same for levelno
else:
    data = record.__dict__.copy()

This allows to specify the logformat as usual. User of your module only need to plug-in the ColoredFormatter. This should be easy, see e.g. https://docs.python.org/2/library/logging.config.html#dictionary-schema-details.

NB: You could do something like this for your formatter writing HTML.

@xolox xolox closed this as completed in 52f6dd0 Oct 23, 2015
@xolox
Copy link
Owner

xolox commented Oct 23, 2015

Hi @boompig and @htgoebel and thanks for the feedback. Please try out version 3.0 (just released). I've changed the ColoredStreamHandler into a ColoredFormatter that supports user defined format strings just like logging.Formatter does. I've also improved coloredlogs.install() so that the log format and date/time format can be set with coloredlogs.install(fmt=..., datefmt=...). Additionally there are now several environment variables that can be used to customize coloredlogs "from the outside" (refer to the online documentation for details and examples).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants