Add logging handler.

This commit is contained in:
Anthony Sottile 2014-04-05 21:50:20 -07:00
parent 4ed9120ae9
commit a3720c0645
5 changed files with 60 additions and 14 deletions

View file

@ -3,6 +3,8 @@ import sys
RED = '\033[41m'
GREEN = '\033[42m'
YELLOW = '\033[43;30m'
TURQUOISE = '\033[46;30m'
NORMAL = '\033[0m'

View file

@ -0,0 +1,32 @@
from __future__ import print_function
import logging
from pre_commit import color
LOG_LEVEL_COLORS = {
'DEBUG': '',
'INFO': '',
'WARNING': color.YELLOW,
'ERROR': color.RED,
}
class LoggingHandler(logging.Handler):
def __init__(self, use_color):
super(LoggingHandler, self).__init__()
self.use_color = use_color
def emit(self, record):
print(
u'{0}{1}'.format(
color.format_color(
'[{0}]'.format(record.levelname),
LOG_LEVEL_COLORS[record.levelname],
self.use_color,
) + ' ' if record.levelno >= logging.WARNING else '',
record.getMessage(),
)
)

View file

@ -1,7 +1,6 @@
from __future__ import print_function
import contextlib
import logging
from plumbum import local
import pre_commit.constants as C
@ -14,6 +13,9 @@ from pre_commit.util import cached_property
from pre_commit.util import clean_path_on_failure
logger = logging.getLogger('pre_commit')
class Repository(object):
def __init__(self, repo_config):
self.repo_config = repo_config
@ -66,9 +68,9 @@ class Repository(object):
return
# Checking out environment for the first time
print('Installing environment for {0}.'.format(self.repo_url))
print('Once installed this environment will be reused.')
print('This may take a few minutes...')
logger.info('Installing environment for {0}.'.format(self.repo_url))
logger.info('Once installed this environment will be reused.')
logger.info('This may take a few minutes...')
with clean_path_on_failure(unicode(local.path(self.sha))):
local['git']['clone', '--no-checkout', self.repo_url, self.sha]()
with self.in_checkout():

View file

@ -2,16 +2,20 @@
from __future__ import print_function
import argparse
import logging
import subprocess
import sys
from pre_commit import color
from pre_commit import commands
from pre_commit import git
from pre_commit.logging_handler import LoggingHandler
from pre_commit.runner import Runner
from pre_commit.util import entry
logger = logging.getLogger('pre_commit')
COLS = int(subprocess.Popen(['tput', 'cols'], stdout=subprocess.PIPE).communicate()[0])
PASS_FAIL_LENGTH = 6
@ -81,6 +85,10 @@ def run_single_hook(runner, hook_id, args):
def _run(runner, args):
# Set up our logging handler
logger.addHandler(LoggingHandler(args.color))
logger.setLevel(logging.INFO)
if args.hook:
return run_single_hook(runner, args.hook, args)
else: