From 04eb04404ff586e9dc268c351574d0cc4a74f67b Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Mon, 29 Mar 2021 21:38:12 +0300 Subject: move setup_logging to utils.py --- cgitize/main.py | 16 +--------------- cgitize/utils.py | 21 +++++++++++++++++++-- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/cgitize/main.py b/cgitize/main.py index 39f370e..4aebda1 100644 --- a/cgitize/main.py +++ b/cgitize/main.py @@ -5,7 +5,6 @@ from argparse import ArgumentParser import configparser -from contextlib import contextmanager import importlib import logging import os.path @@ -20,19 +19,6 @@ DEFAULT_CONFIG_PATH = '/etc/cgitize/cgitize.conf' DEFAULT_MY_REPOS_PATH = '/etc/cgitize/my_repos.py' -@contextmanager -def setup_logging(): - logging.basicConfig( - level=logging.DEBUG, - datefmt='%Y-%m-%d %H:%M:%S', - format='%(asctime)s | %(levelname)s | %(message)s') - try: - yield - except Exception as e: - logging.exception(e) - raise - - def parse_args(argv=None): if argv is None: argv = sys.argv[1:] @@ -109,7 +95,7 @@ class Config: def main(args=None): - with setup_logging(): + with utils.setup_logging(): args = parse_args(args) config = Config.read(args.config) my_repos = config.import_my_repos() diff --git a/cgitize/utils.py b/cgitize/utils.py index 478a35d..0bfa855 100644 --- a/cgitize/utils.py +++ b/cgitize/utils.py @@ -3,10 +3,27 @@ # For details, see https://github.com/egor-tensin/cgitize. # Distributed under the MIT License. -import contextlib +from contextlib import contextmanager import logging import os import subprocess +import sys + + +@contextmanager +def setup_logging(): + logging.basicConfig( + level=logging.DEBUG, + datefmt='%Y-%m-%d %H:%M:%S', + format='%(asctime)s | %(levelname)s | %(message)s', + # Log to stdout, because that's where subprocess's output goes (so that + # the don't get interleaved). + stream=sys.stdout) + try: + yield + except Exception as e: + logging.exception(e) + raise def run(*args, capture_output=False, **kwargs): @@ -44,7 +61,7 @@ def try_run_capture(*args, **kwargs): return e.returncode == 0, e.output -@contextlib.contextmanager +@contextmanager def chdir(new_cwd): old_cwd = os.getcwd() os.chdir(new_cwd) -- cgit v1.2.3