Skip to content

Commit

Permalink
Don't depend on vdirsyncer logging code
Browse files Browse the repository at this point in the history
And avoid random breakage if user has a "wrong" version of vdirsyncer
installed.
  • Loading branch information
untitaker committed Aug 21, 2014
1 parent 1987023 commit 1cca305
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion khal/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,39 @@
import logging
import sys

import click

from khal import __productname__
from vdirsyncer.log import stdout_handler


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')
}

def format(self, record):
if not record.exc_info:
level = record.levelname.lower()
if level in self.colors:
prefix = click.style('{}: '.format(level),
**self.colors[level])
record.msg = '\n'.join(prefix + x
for x in str(record.msg).splitlines())

return logging.Formatter.format(self, record)


class ClickStream(object):
def write(self, string):
click.echo(string, file=sys.stderr, nl=False)


stdout_handler = logging.StreamHandler(ClickStream())
stdout_handler.formatter = ColorFormatter()

logger = logging.getLogger(__productname__)
logger.setLevel(logging.INFO)
Expand Down

0 comments on commit 1cca305

Please sign in to comment.