Skip to content

Commit a3720c0

Browse files
committed
Add logging handler.
1 parent 4ed9120 commit a3720c0

5 files changed

Lines changed: 60 additions & 14 deletions

File tree

pre_commit/color.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
RED = '\033[41m'
55
GREEN = '\033[42m'
6+
YELLOW = '\033[43;30m'
7+
TURQUOISE = '\033[46;30m'
68
NORMAL = '\033[0m'
79

810

pre_commit/logging_handler.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
from __future__ import print_function
3+
4+
import logging
5+
6+
from pre_commit import color
7+
8+
9+
LOG_LEVEL_COLORS = {
10+
'DEBUG': '',
11+
'INFO': '',
12+
'WARNING': color.YELLOW,
13+
'ERROR': color.RED,
14+
}
15+
16+
17+
class LoggingHandler(logging.Handler):
18+
def __init__(self, use_color):
19+
super(LoggingHandler, self).__init__()
20+
self.use_color = use_color
21+
22+
def emit(self, record):
23+
print(
24+
u'{0}{1}'.format(
25+
color.format_color(
26+
'[{0}]'.format(record.levelname),
27+
LOG_LEVEL_COLORS[record.levelname],
28+
self.use_color,
29+
) + ' ' if record.levelno >= logging.WARNING else '',
30+
record.getMessage(),
31+
)
32+
)

pre_commit/repository.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11

2-
from __future__ import print_function
3-
42
import contextlib
3+
import logging
54
from plumbum import local
65

76
import pre_commit.constants as C
@@ -14,6 +13,9 @@
1413
from pre_commit.util import clean_path_on_failure
1514

1615

16+
logger = logging.getLogger('pre_commit')
17+
18+
1719
class Repository(object):
1820
def __init__(self, repo_config):
1921
self.repo_config = repo_config
@@ -66,9 +68,9 @@ def create(self):
6668
return
6769

6870
# Checking out environment for the first time
69-
print('Installing environment for {0}.'.format(self.repo_url))
70-
print('Once installed this environment will be reused.')
71-
print('This may take a few minutes...')
71+
logger.info('Installing environment for {0}.'.format(self.repo_url))
72+
logger.info('Once installed this environment will be reused.')
73+
logger.info('This may take a few minutes...')
7274
with clean_path_on_failure(unicode(local.path(self.sha))):
7375
local['git']['clone', '--no-checkout', self.repo_url, self.sha]()
7476
with self.in_checkout():

pre_commit/run.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@
22
from __future__ import print_function
33

44
import argparse
5+
import logging
56
import subprocess
67
import sys
78

89
from pre_commit import color
910
from pre_commit import commands
1011
from pre_commit import git
12+
from pre_commit.logging_handler import LoggingHandler
1113
from pre_commit.runner import Runner
1214
from pre_commit.util import entry
1315

1416

17+
logger = logging.getLogger('pre_commit')
18+
1519
COLS = int(subprocess.Popen(['tput', 'cols'], stdout=subprocess.PIPE).communicate()[0])
1620

1721
PASS_FAIL_LENGTH = 6
@@ -81,6 +85,10 @@ def run_single_hook(runner, hook_id, args):
8185

8286

8387
def _run(runner, args):
88+
# Set up our logging handler
89+
logger.addHandler(LoggingHandler(args.color))
90+
logger.setLevel(logging.INFO)
91+
8492
if args.hook:
8593
return run_single_hook(runner, args.hook, args)
8694
else:

tests/repository_test.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import __builtin__
21
import mock
32
import os
43
import pytest
54

65
import pre_commit.constants as C
76
from pre_commit import git
7+
from pre_commit import repository
88
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
99
from pre_commit.clientlib.validate_config import validate_config_extra
1010
from pre_commit.jsonschema_extensions import apply_defaults
@@ -135,23 +135,25 @@ def test_languages(config_for_python_hooks_repo):
135135

136136

137137
@pytest.yield_fixture
138-
def print_mock():
139-
with mock.patch.object(__builtin__, 'print', autospec=True) as print_mock:
140-
yield print_mock
138+
def logger_mock():
139+
with mock.patch.object(
140+
repository.logger, 'info', autospec=True,
141+
) as info_mock:
142+
yield info_mock
141143

142144

143-
def test_prints_while_creating(config_for_python_hooks_repo, print_mock):
145+
def test_prints_while_creating(config_for_python_hooks_repo, logger_mock):
144146
repo = Repository(config_for_python_hooks_repo)
145147
repo.require_created()
146-
print_mock.assert_called_with('This may take a few minutes...')
147-
print_mock.reset_mock()
148+
logger_mock.assert_called_with('This may take a few minutes...')
149+
logger_mock.reset_mock()
148150
# Reinstall with same repo should not trigger another install
149151
repo.require_created()
150-
assert print_mock.call_count == 0
152+
assert logger_mock.call_count == 0
151153
# Reinstall on another run should not trigger another install
152154
repo = Repository(config_for_python_hooks_repo)
153155
repo.require_created()
154-
assert print_mock.call_count == 0
156+
assert logger_mock.call_count == 0
155157

156158

157159
def test_reinstall(config_for_python_hooks_repo):

0 commit comments

Comments
 (0)