diff --git a/kallithea/config/application.py b/kallithea/config/application.py --- a/kallithea/config/application.py +++ b/kallithea/config/application.py @@ -20,6 +20,7 @@ from kallithea.config.middleware.simpleg from kallithea.config.middleware.simplehg import SimpleHg from kallithea.config.middleware.wrapper import RequestWrapper from kallithea.lib.utils2 import asbool +from kallithea.lib.vcs.utils import hgcompat __all__ = ['make_app'] @@ -50,6 +51,7 @@ def wrap_app(app): def make_app(global_conf, **app_conf): """Return WSGI app with logging Mercurial stdout/stderr - to be used as Paste or mod_wsgi entry point""" + hgcompat.redirect_stdio_to_logging() return make_app_raw(global_conf, **app_conf) diff --git a/kallithea/lib/vcs/utils/hgcompat.py b/kallithea/lib/vcs/utils/hgcompat.py --- a/kallithea/lib/vcs/utils/hgcompat.py +++ b/kallithea/lib/vcs/utils/hgcompat.py @@ -2,10 +2,25 @@ Mercurial libs compatibility """ +import logging + import mercurial.encoding import mercurial.localrepo +class MercurialStdLogger: + def __init__(self, logger): + self.logger = logger + + def write(self, message): + try: + self.logger(message.decode().rstrip()) + except: + self.logger(message) + + def flush(self): + pass + def monkey_do(): """Apply some Mercurial monkey patching""" # workaround for 3.3 94ac64bcf6fe and not calling largefiles reposetup correctly, and test_archival failing @@ -15,3 +30,18 @@ def monkey_do(): # Minimize potential impact from custom configuration mercurial.encoding.environ[b'HGPLAIN'] = b'1' + + +hglog = logging.getLogger("mercurial") + + +def redirect_stdio_to_logging(): + # Capture Mercurial stdout/stderr and send to a 'mercurial' logger + try: + import mercurial.utils.procutil as procutil + if not isinstance(procutil.stdout, MercurialStdLogger): + procutil.stdout = MercurialStdLogger(hglog.info) + if not isinstance(procutil.stderr, MercurialStdLogger): + procutil.stderr = MercurialStdLogger(hglog.warning) + except Exception as e: + hglog.error("Exception installing procutil stdout/stderr: %s", e)