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:

View File

@@ -1,10 +1,10 @@
import __builtin__
import mock
import os
import pytest
import pre_commit.constants as C
from pre_commit import git
from pre_commit import repository
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
from pre_commit.clientlib.validate_config import validate_config_extra
from pre_commit.jsonschema_extensions import apply_defaults
@@ -135,23 +135,25 @@ def test_languages(config_for_python_hooks_repo):
@pytest.yield_fixture
def print_mock():
with mock.patch.object(__builtin__, 'print', autospec=True) as print_mock:
yield print_mock
def logger_mock():
with mock.patch.object(
repository.logger, 'info', autospec=True,
) as info_mock:
yield info_mock
def test_prints_while_creating(config_for_python_hooks_repo, print_mock):
def test_prints_while_creating(config_for_python_hooks_repo, logger_mock):
repo = Repository(config_for_python_hooks_repo)
repo.require_created()
print_mock.assert_called_with('This may take a few minutes...')
print_mock.reset_mock()
logger_mock.assert_called_with('This may take a few minutes...')
logger_mock.reset_mock()
# Reinstall with same repo should not trigger another install
repo.require_created()
assert print_mock.call_count == 0
assert logger_mock.call_count == 0
# Reinstall on another run should not trigger another install
repo = Repository(config_for_python_hooks_repo)
repo.require_created()
assert print_mock.call_count == 0
assert logger_mock.call_count == 0
def test_reinstall(config_for_python_hooks_repo):