From 0eeb93ee0c830d00a3f95ea376fcbbbf9ecf91c8 Mon Sep 17 00:00:00 2001 From: Dan Streetman Date: Mon, 1 Feb 2021 18:26:13 -0500 Subject: [PATCH] logging: update ubuntutools.getLogger() to output to stdout/stderr correctly Python logging by default sends all output to stderr, but that's not what normal programs usually do and is not expected for these scripts. Change the ubuntutools.getLogger() method to return a logger with handlers correctly set up to send INFO level (or lower) logs to stdout and WARNING level (or higher; technically INFO+1 level or higher) logs to stderr. This results in normally logged output going to stdout and warnings/errors going to stderr, as expected. --- backportpackage | 2 +- bitesize | 2 +- grep-merges | 2 +- hugdaylist | 2 +- import-bug-from-debian | 2 +- merge-changelog | 2 +- pbuilder-dist | 2 +- pull-debian-debdiff | 2 +- requestbackport | 2 +- requestsync | 2 +- reverse-depends | 2 +- seeded-in-ubuntu | 2 +- sponsor-patch | 2 +- submittodebian | 2 +- syncpackage | 2 +- ubuntu-build | 2 +- ubuntu-iso | 2 +- ubuntu-upload-permission | 2 +- ubuntutools/__init__.py | 54 ++++++++++++++++++++++++++++++++-------- ubuntutools/pullpkg.py | 6 +++-- 20 files changed, 65 insertions(+), 31 deletions(-) diff --git a/backportpackage b/backportpackage index 8fe2788..3856e82 100755 --- a/backportpackage +++ b/backportpackage @@ -42,7 +42,7 @@ from ubuntutools.misc import (system_distribution, vendor_to_distroinfo, from ubuntutools.question import YesNoQuestion from ubuntutools import getLogger -Logger = getLogger(__name__) +Logger = getLogger() def error(msg): diff --git a/bitesize b/bitesize index 5a2c132..04d1eef 100755 --- a/bitesize +++ b/bitesize @@ -30,7 +30,7 @@ from launchpadlib.errors import HTTPError from ubuntutools.config import UDTConfig from ubuntutools import getLogger -Logger = getLogger(__name__) +Logger = getLogger() def error_out(msg): diff --git a/grep-merges b/grep-merges index 7c75cd4..6a1b895 100755 --- a/grep-merges +++ b/grep-merges @@ -28,7 +28,7 @@ from httplib2 import Http, HttpLib2Error import ubuntutools.misc from ubuntutools import getLogger -Logger = getLogger(__name__) +Logger = getLogger() def main(): diff --git a/hugdaylist b/hugdaylist index 5140ae1..ef33523 100755 --- a/hugdaylist +++ b/hugdaylist @@ -37,7 +37,7 @@ from launchpadlib.launchpad import Launchpad from ubuntutools.lp.libsupport import translate_web_api from ubuntutools import getLogger -Logger = getLogger(__name__) +Logger = getLogger() def check_args(): diff --git a/import-bug-from-debian b/import-bug-from-debian index b9ab478..9aeac46 100755 --- a/import-bug-from-debian +++ b/import-bug-from-debian @@ -33,7 +33,7 @@ from launchpadlib.launchpad import Launchpad from ubuntutools.config import UDTConfig from ubuntutools import getLogger -Logger = getLogger(__name__) +Logger = getLogger() def main(): diff --git a/merge-changelog b/merge-changelog index f14d4db..d808dbf 100755 --- a/merge-changelog +++ b/merge-changelog @@ -23,7 +23,7 @@ import sys from debian.changelog import Changelog from ubuntutools import getLogger -Logger = getLogger(__name__) +Logger = getLogger() def usage(exit_code=1): diff --git a/pbuilder-dist b/pbuilder-dist index 4e1b47b..9ae4eff 100755 --- a/pbuilder-dist +++ b/pbuilder-dist @@ -44,7 +44,7 @@ from ubuntutools.config import UDTConfig from ubuntutools.question import YesNoQuestion from ubuntutools import getLogger -Logger = getLogger(__name__) +Logger = getLogger() class PbuilderDist(object): diff --git a/pull-debian-debdiff b/pull-debian-debdiff index 50f15ce..365b7f8 100755 --- a/pull-debian-debdiff +++ b/pull-debian-debdiff @@ -27,7 +27,7 @@ from ubuntutools.config import UDTConfig from ubuntutools.version import Version from ubuntutools import getLogger -Logger = getLogger(__name__) +Logger = getLogger() def previous_version(package, version, distance): diff --git a/requestbackport b/requestbackport index a6e30f3..53771f8 100755 --- a/requestbackport +++ b/requestbackport @@ -30,7 +30,7 @@ from ubuntutools.question import (YesNoQuestion, EditBugReport, from ubuntutools.rdepends import query_rdepends, RDependsException from ubuntutools import getLogger -Logger = getLogger(__name__) +Logger = getLogger() class DestinationException(Exception): diff --git a/requestsync b/requestsync index a12a669..781834f 100755 --- a/requestsync +++ b/requestsync @@ -39,7 +39,7 @@ from ubuntutools.question import confirmation_prompt, EditBugReport from ubuntutools.version import Version from ubuntutools import getLogger -Logger = getLogger(__name__) +Logger = getLogger() # # entry point diff --git a/reverse-depends b/reverse-depends index d5ca6b8..15aa669 100755 --- a/reverse-depends +++ b/reverse-depends @@ -24,7 +24,7 @@ from ubuntutools.misc import (system_distribution, vendor_to_distroinfo, from ubuntutools.rdepends import query_rdepends, RDependsException from ubuntutools import getLogger -Logger = getLogger(__name__) +Logger = getLogger() DEFAULT_MAX_DEPTH = 10 # We want avoid any infinite loop... diff --git a/seeded-in-ubuntu b/seeded-in-ubuntu index 846f92e..b3f9d7f 100755 --- a/seeded-in-ubuntu +++ b/seeded-in-ubuntu @@ -26,7 +26,7 @@ from ubuntutools.lp.lpapicache import (Distribution, Launchpad, PackageNotFoundException) from ubuntutools import getLogger -Logger = getLogger(__name__) +Logger = getLogger() DATA_URL = 'http://qa.ubuntuwire.org/ubuntu-seeded-packages/seeded.json.gz' diff --git a/sponsor-patch b/sponsor-patch index 1e77852..4125a44 100755 --- a/sponsor-patch +++ b/sponsor-patch @@ -26,7 +26,7 @@ from ubuntutools.config import UDTConfig from ubuntutools.sponsor_patch.sponsor_patch import sponsor_patch, check_dependencies from ubuntutools import getLogger -Logger = getLogger(__name__) +Logger = getLogger() def parse(script_name): diff --git a/submittodebian b/submittodebian index 13a5bc1..a2cc145 100755 --- a/submittodebian +++ b/submittodebian @@ -40,7 +40,7 @@ from ubuntutools.question import YesNoQuestion, EditFile from ubuntutools.update_maintainer import update_maintainer, restore_maintainer from ubuntutools import getLogger -Logger = getLogger(__name__) +Logger = getLogger() def get_most_recent_debian_version(changelog): diff --git a/syncpackage b/syncpackage index 8282bb3..fefd668 100755 --- a/syncpackage +++ b/syncpackage @@ -46,7 +46,7 @@ from ubuntutools.requestsync.lp import get_debian_srcpkg, get_ubuntu_srcpkg from ubuntutools.version import Version from ubuntutools import getLogger -Logger = getLogger(__name__) +Logger = getLogger() def remove_signature(dscname): diff --git a/ubuntu-build b/ubuntu-build index af5345d..a08a7ef 100755 --- a/ubuntu-build +++ b/ubuntu-build @@ -33,7 +33,7 @@ from ubuntutools.lp.lpapicache import Distribution, PersonTeam from ubuntutools.misc import split_release_pocket from ubuntutools import getLogger -Logger = getLogger(__name__) +Logger = getLogger() def main(): diff --git a/ubuntu-iso b/ubuntu-iso index c07b420..3c5afbb 100755 --- a/ubuntu-iso +++ b/ubuntu-iso @@ -25,7 +25,7 @@ import subprocess import sys from ubuntutools import getLogger -Logger = getLogger(__name__) +Logger = getLogger() def extract(iso, path): diff --git a/ubuntu-upload-permission b/ubuntu-upload-permission index 2742eb2..394b186 100755 --- a/ubuntu-upload-permission +++ b/ubuntu-upload-permission @@ -23,7 +23,7 @@ from ubuntutools.lp.lpapicache import (Launchpad, Distribution, PersonTeam, from ubuntutools.misc import split_release_pocket from ubuntutools import getLogger -Logger = getLogger(__name__) +Logger = getLogger() def parse_arguments(): diff --git a/ubuntutools/__init__.py b/ubuntutools/__init__.py index f886333..eadee07 100644 --- a/ubuntutools/__init__.py +++ b/ubuntutools/__init__.py @@ -4,18 +4,50 @@ # https://launchpad.net/ubuntu-dev-tools import logging +import sys -def _loggingBasicConfig(**kwargs): - '''Set log level to INFO and define log format to use.''' - if 'level' not in kwargs: - kwargs['level'] = logging.INFO - if 'format' not in kwargs: - kwargs['format'] = '%(message)s' - logging.basicConfig(**kwargs) +def getLogger(): + ''' Get the logger instance for this module + Quick guide for using this or not: if you want to call ubuntutools + module code and have its output print to stdout/stderr ONLY, you can + use the logger this creates. You can also log directly to this logger + from your own code to send output to stdout/stderr. -def getLogger(name=None): - '''Get standard Python logging.Logger with some ubuntutools defaults.''' - _loggingBasicConfig() - return logging.getLogger(name) + This creates the ubuntutools module-level logger, and sets some default + values for formatting and levels, and directs INFO-level logs messages to + stdout and logs higher than INFO to stderr. The logger's level may be + adjusted to show more logs (e.g. DEBUG) or less (e.g. WARNING, to suppress + all INFO messages). + + Without calling this module, the ubuntutools logs will propagate up to + higher level loggers (possibly the root logger) and be handled by them. + Note that the default for python logging is to print WARNING and above + logs to stderr. + + Note if any code calls this method, the ubuntutools module-level logger + will no longer propagate ubuntutools log message up to higher level + loggers. + + This should only be used by runnable scripts provided by the + ubuntu-dev-tools package, or other runnable scripts that want the behavior + described above. + ''' + logger = logging.getLogger(__name__) + logger.setLevel(logging.INFO) + logger.propagate = False + + fmt = logging.Formatter('%(message)s') + + stdout_handler = logging.StreamHandler(stream=sys.stdout) + stdout_handler.setFormatter(fmt) + stdout_handler.addFilter(lambda r: r.levelno <= logging.INFO) + logger.addHandler(stdout_handler) + + stderr_handler = logging.StreamHandler(stream=sys.stderr) + stdout_handler.setFormatter(fmt) + stderr_handler.setLevel(logging.INFO+1) + logger.addHandler(stderr_handler) + + return logger diff --git a/ubuntutools/pullpkg.py b/ubuntutools/pullpkg.py index f812d63..4077ed8 100644 --- a/ubuntutools/pullpkg.py +++ b/ubuntutools/pullpkg.py @@ -49,8 +49,10 @@ from ubuntutools.misc import (split_release_pocket, UPLOAD_QUEUE_STATUSES, STATUSES) -from ubuntutools import _loggingBasicConfig +# by default we use standard logging.getLogger() and only use +# ubuntutools.getLogger() in PullPkg().main() +from ubuntutools import getLogger as ubuntutools_getLogger import logging Logger = logging.getLogger(__name__) @@ -98,7 +100,7 @@ class PullPkg(object): unexpected errors will flow up to the caller. On success, this simply returns. """ - _loggingBasicConfig() + Logger = ubuntutools_getLogger() try: cls(*args, **kwargs).pull()