Centralize logging initialization

This commit is contained in:
Anthony Sottile 2016-11-26 14:18:13 -08:00
parent 323507c9e3
commit a7169905dc
18 changed files with 26 additions and 43 deletions

View file

@ -1,8 +1,6 @@
from __future__ import print_function from __future__ import print_function
from __future__ import unicode_literals from __future__ import unicode_literals
import logging
from aspy.yaml import ordered_dump from aspy.yaml import ordered_dump
from aspy.yaml import ordered_load from aspy.yaml import ordered_load
@ -12,7 +10,6 @@ from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
from pre_commit.clientlib.validate_config import is_local_hooks from pre_commit.clientlib.validate_config import is_local_hooks
from pre_commit.clientlib.validate_config import load_config from pre_commit.clientlib.validate_config import load_config
from pre_commit.jsonschema_extensions import remove_defaults from pre_commit.jsonschema_extensions import remove_defaults
from pre_commit.logging_handler import LoggingHandler
from pre_commit.ordereddict import OrderedDict from pre_commit.ordereddict import OrderedDict
from pre_commit.repository import Repository from pre_commit.repository import Repository
from pre_commit.util import CalledProcessError from pre_commit.util import CalledProcessError
@ -20,9 +17,6 @@ from pre_commit.util import cmd_output
from pre_commit.util import cwd from pre_commit.util import cwd
logger = logging.getLogger('pre_commit')
class RepositoryCannotBeUpdatedError(RuntimeError): class RepositoryCannotBeUpdatedError(RuntimeError):
pass pass
@ -69,10 +63,6 @@ def _update_repository(repo_config, runner):
def autoupdate(runner): def autoupdate(runner):
"""Auto-update the pre-commit config to the latest versions of repos.""" """Auto-update the pre-commit config to the latest versions of repos."""
# Set up our logging handler
logger.addHandler(LoggingHandler(False))
logger.setLevel(logging.WARNING)
retv = 0 retv = 0
output_configs = [] output_configs = []
changed = False changed = False

View file

@ -2,21 +2,15 @@ from __future__ import print_function
from __future__ import unicode_literals from __future__ import unicode_literals
import io import io
import logging
import os
import os.path import os.path
import sys import sys
from pre_commit import output from pre_commit import output
from pre_commit.logging_handler import LoggingHandler
from pre_commit.util import make_executable from pre_commit.util import make_executable
from pre_commit.util import mkdirp from pre_commit.util import mkdirp
from pre_commit.util import resource_filename from pre_commit.util import resource_filename
logger = logging.getLogger('pre_commit')
# This is used to identify the hook file we install # This is used to identify the hook file we install
PREVIOUS_IDENTIFYING_HASHES = ( PREVIOUS_IDENTIFYING_HASHES = (
'4d9958c90bc262f47553e2c073f14cfe', '4d9958c90bc262f47553e2c073f14cfe',
@ -88,9 +82,6 @@ def install(runner, overwrite=False, hooks=False, hook_type='pre-commit'):
# If they requested we install all of the hooks, do so. # If they requested we install all of the hooks, do so.
if hooks: if hooks:
# Set up our logging handler
logger.addHandler(LoggingHandler(False))
logger.setLevel(logging.INFO)
for repository in runner.repositories: for repository in runner.repositories:
repository.require_installed() repository.require_installed()

View file

@ -8,7 +8,6 @@ import sys
from pre_commit import color from pre_commit import color
from pre_commit import git from pre_commit import git
from pre_commit import output from pre_commit import output
from pre_commit.logging_handler import LoggingHandler
from pre_commit.output import get_hook_message from pre_commit.output import get_hook_message
from pre_commit.staged_files_only import staged_files_only from pre_commit.staged_files_only import staged_files_only
from pre_commit.util import cmd_output from pre_commit.util import cmd_output
@ -178,9 +177,6 @@ def _has_unstaged_config(runner):
def run(runner, args, environ=os.environ): def run(runner, args, environ=os.environ):
no_stash = args.no_stash or args.all_files or bool(args.files) no_stash = args.no_stash or args.all_files or bool(args.files)
# Set up our logging handler
logger.addHandler(LoggingHandler(args.color))
logger.setLevel(logging.INFO)
# Check if we have unresolved merge conflict files and fail fast. # Check if we have unresolved merge conflict files and fail fast.
if _has_unmerged_paths(runner): if _has_unmerged_paths(runner):

View file

@ -2,7 +2,6 @@ from __future__ import unicode_literals
import functools import functools
import logging import logging
import os
import os.path import os.path
import re import re

View file

@ -6,6 +6,8 @@ from pre_commit import color
from pre_commit import output from pre_commit import output
logger = logging.getLogger('pre_commit')
LOG_LEVEL_COLORS = { LOG_LEVEL_COLORS = {
'DEBUG': '', 'DEBUG': '',
'INFO': '', 'INFO': '',
@ -30,3 +32,8 @@ class LoggingHandler(logging.Handler):
record.getMessage(), record.getMessage(),
) )
) )
def add_logging_handler(*args, **kwargs):
logger.addHandler(LoggingHandler(*args, **kwargs))
logger.setLevel(logging.INFO)

View file

@ -15,6 +15,7 @@ from pre_commit.commands.install_uninstall import install
from pre_commit.commands.install_uninstall import uninstall from pre_commit.commands.install_uninstall import uninstall
from pre_commit.commands.run import run from pre_commit.commands.run import run
from pre_commit.error_handler import error_handler from pre_commit.error_handler import error_handler
from pre_commit.logging_handler import add_logging_handler
from pre_commit.runner import Runner from pre_commit.runner import Runner
@ -25,6 +26,14 @@ from pre_commit.runner import Runner
os.environ.pop('__PYVENV_LAUNCHER__', None) os.environ.pop('__PYVENV_LAUNCHER__', None)
def _add_color_option(parser):
parser.add_argument(
'--color', default='auto', type=color.use_color,
metavar='{' + ','.join(color.COLOR_CHOICES) + '}',
help='Whether to use color in output. Defaults to `%(default)s`.',
)
def main(argv=None): def main(argv=None):
argv = argv if argv is not None else sys.argv[1:] argv = argv if argv is not None else sys.argv[1:]
argv = [five.to_text(arg) for arg in argv] argv = [five.to_text(arg) for arg in argv]
@ -44,6 +53,7 @@ def main(argv=None):
install_parser = subparsers.add_parser( install_parser = subparsers.add_parser(
'install', help='Install the pre-commit script.', 'install', help='Install the pre-commit script.',
) )
_add_color_option(install_parser)
install_parser.add_argument( install_parser.add_argument(
'-f', '--overwrite', action='store_true', '-f', '--overwrite', action='store_true',
help='Overwrite existing hooks / remove migration mode.', help='Overwrite existing hooks / remove migration mode.',
@ -63,25 +73,26 @@ def main(argv=None):
uninstall_parser = subparsers.add_parser( uninstall_parser = subparsers.add_parser(
'uninstall', help='Uninstall the pre-commit script.', 'uninstall', help='Uninstall the pre-commit script.',
) )
_add_color_option(uninstall_parser)
uninstall_parser.add_argument( uninstall_parser.add_argument(
'-t', '--hook-type', choices=('pre-commit', 'pre-push'), '-t', '--hook-type', choices=('pre-commit', 'pre-push'),
default='pre-commit', default='pre-commit',
) )
subparsers.add_parser('clean', help='Clean out pre-commit files.') clean_parser = subparsers.add_parser(
'clean', help='Clean out pre-commit files.',
)
_add_color_option(clean_parser)
subparsers.add_parser( autoupdate_parser = subparsers.add_parser(
'autoupdate', 'autoupdate',
help="Auto-update pre-commit config to the latest repos' versions.", help="Auto-update pre-commit config to the latest repos' versions.",
) )
_add_color_option(autoupdate_parser)
run_parser = subparsers.add_parser('run', help='Run hooks.') run_parser = subparsers.add_parser('run', help='Run hooks.')
_add_color_option(run_parser)
run_parser.add_argument('hook', nargs='?', help='A single hook-id to run') run_parser.add_argument('hook', nargs='?', help='A single hook-id to run')
run_parser.add_argument(
'--color', default='auto', type=color.use_color,
metavar='{' + ','.join(color.COLOR_CHOICES) + '}',
help='Whether to use color in output. Defaults to `%(default)s`.',
)
run_parser.add_argument( run_parser.add_argument(
'--no-stash', default=False, action='store_true', '--no-stash', default=False, action='store_true',
help='Use this option to prevent auto stashing of unstaged files.', help='Use this option to prevent auto stashing of unstaged files.',
@ -140,6 +151,7 @@ def main(argv=None):
parser.parse_args(['--help']) parser.parse_args(['--help'])
with error_handler(): with error_handler():
add_logging_handler(args.color)
runner = Runner.create() runner = Runner.create()
if args.command == 'install': if args.command == 'install':

View file

@ -1,6 +1,5 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import os
import os.path import os.path
import subprocess import subprocess

View file

@ -1,6 +1,5 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import os
import os.path import os.path
from cached_property import cached_property from cached_property import cached_property

View file

@ -3,7 +3,6 @@ from __future__ import unicode_literals
import contextlib import contextlib
import io import io
import logging import logging
import os
import os.path import os.path
import sqlite3 import sqlite3
import tempfile import tempfile

View file

@ -3,7 +3,6 @@ from __future__ import unicode_literals
import contextlib import contextlib
import errno import errno
import functools import functools
import os
import os.path import os.path
import shutil import shutil
import stat import stat

View file

@ -1,6 +1,5 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import os
import os.path import os.path
import shutil import shutil

View file

@ -2,7 +2,6 @@ from __future__ import absolute_import
from __future__ import unicode_literals from __future__ import unicode_literals
import io import io
import os
import os.path import os.path
import re import re
import shutil import shutil

View file

@ -2,7 +2,6 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import io import io
import os
import os.path import os.path
import subprocess import subprocess
import sys import sys

View file

@ -4,7 +4,6 @@ from __future__ import unicode_literals
import functools import functools
import io import io
import logging import logging
import os
import os.path import os.path
import mock import mock

View file

@ -3,7 +3,6 @@ from __future__ import unicode_literals
import io import io
import logging import logging
import os
import os.path import os.path
import re import re
import shutil import shutil

View file

@ -1,7 +1,6 @@
from __future__ import absolute_import from __future__ import absolute_import
from __future__ import unicode_literals from __future__ import unicode_literals
import os
import os.path import os.path
import pre_commit.constants as C import pre_commit.constants as C

View file

@ -2,7 +2,6 @@ from __future__ import absolute_import
from __future__ import unicode_literals from __future__ import unicode_literals
import io import io
import os
import os.path import os.path
import sqlite3 import sqlite3

View file

@ -1,6 +1,5 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import os
import os.path import os.path
import random import random