Skip to content

Commit e4f0b4c

Browse files
committed
Only configure logging inside the context
1 parent bdc58cc commit e4f0b4c

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

pre_commit/logging_handler.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import unicode_literals
22

3+
import contextlib
34
import logging
45

56
from pre_commit import color
@@ -34,6 +35,12 @@ def emit(self, record):
3435
)
3536

3637

37-
def add_logging_handler(*args, **kwargs):
38-
logger.addHandler(LoggingHandler(*args, **kwargs))
38+
@contextlib.contextmanager
39+
def logging_handler(*args, **kwargs):
40+
handler = LoggingHandler(*args, **kwargs)
41+
logger.addHandler(handler)
3942
logger.setLevel(logging.INFO)
43+
try:
44+
yield
45+
finally:
46+
logger.removeHandler(handler)

pre_commit/main.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from pre_commit.commands.try_repo import try_repo
2121
from pre_commit.error_handler import error_handler
2222
from pre_commit.error_handler import FatalError
23-
from pre_commit.logging_handler import add_logging_handler
23+
from pre_commit.logging_handler import logging_handler
2424
from pre_commit.store import Store
2525
from pre_commit.util import CalledProcessError
2626

@@ -248,9 +248,7 @@ def main(argv=None):
248248
elif args.command == 'help':
249249
parser.parse_args(['--help'])
250250

251-
with error_handler():
252-
add_logging_handler(args.color)
253-
251+
with error_handler(), logging_handler(args.color):
254252
_adjust_args_and_chdir(args)
255253

256254
store = Store()

tests/conftest.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import six
1212

1313
from pre_commit import output
14-
from pre_commit.logging_handler import add_logging_handler
14+
from pre_commit.logging_handler import logging_handler
1515
from pre_commit.store import Store
1616
from pre_commit.util import cmd_output
1717
from testing.fixtures import git_dir
@@ -155,7 +155,8 @@ class YouForgotToExplicitlyChooseAStoreDirectory(AssertionError):
155155

156156
@pytest.fixture(autouse=True, scope='session')
157157
def configure_logging():
158-
add_logging_handler(use_color=False)
158+
with logging_handler(use_color=False):
159+
yield
159160

160161

161162
@pytest.fixture

0 commit comments

Comments
 (0)