mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 00:04:42 +04:00
Convert LoggingHandler to use sys.stdout.write
This commit is contained in:
parent
3d5a360df4
commit
374ef75835
2 changed files with 7 additions and 8 deletions
|
|
@ -1,6 +1,5 @@
|
|||
from __future__ import print_function
|
||||
|
||||
import logging
|
||||
import sys
|
||||
|
||||
from pre_commit import color
|
||||
|
||||
|
|
@ -14,14 +13,14 @@ LOG_LEVEL_COLORS = {
|
|||
|
||||
|
||||
class LoggingHandler(logging.Handler):
|
||||
def __init__(self, use_color, print_fn=print):
|
||||
def __init__(self, use_color, write=sys.stdout.write):
|
||||
logging.Handler.__init__(self)
|
||||
self.use_color = use_color
|
||||
self.__print_fn = print_fn
|
||||
self.__write = write
|
||||
|
||||
def emit(self, record):
|
||||
self.__print_fn(
|
||||
u'{0}{1}'.format(
|
||||
self.__write(
|
||||
u'{0}{1}\n'.format(
|
||||
color.format_color(
|
||||
'[{0}]'.format(record.levelname),
|
||||
LOG_LEVEL_COLORS[record.levelname],
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ def test_logging_handler_color():
|
|||
handler = LoggingHandler(True, print_mock)
|
||||
handler.emit(FakeLogRecord('hi', 'WARNING', 30))
|
||||
print_mock.assert_called_once_with(
|
||||
color.YELLOW + '[WARNING]' + color.NORMAL + ' hi',
|
||||
color.YELLOW + '[WARNING]' + color.NORMAL + ' hi\n',
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -28,5 +28,5 @@ def test_logging_handler_no_color():
|
|||
handler = LoggingHandler(False, print_mock)
|
||||
handler.emit(FakeLogRecord('hi', 'WARNING', 30))
|
||||
print_mock.assert_called_once_with(
|
||||
'[WARNING] hi',
|
||||
'[WARNING] hi\n',
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue