method to globally set logging to output nicely to the console

This will make all of the direct calls to logging level functions output
in a format that looks appropriate for the console. Previously, the default
output looked like it should be written to a log file.
This commit is contained in:
Hans-Christoph Steiner 2024-02-27 16:39:53 +01:00 committed by Michael Pöhn
parent 091fe9b260
commit 76d9eddb3a
5 changed files with 42 additions and 4 deletions

View File

@ -192,6 +192,31 @@ def setup_global_opts(parser):
help=_("Restrict output to warnings and errors"))
def set_console_logging(verbose=False):
"""Globally set logging to output nicely to the console."""
class _StdOutFilter(logging.Filter):
def filter(self, record):
return record.levelno < logging.ERROR
if verbose:
level = logging.DEBUG
else:
level = logging.ERROR
stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.addFilter(_StdOutFilter())
stdout_handler.setFormatter(logging.Formatter('%(message)s'))
stderr_handler = logging.StreamHandler(sys.stderr)
stderr_handler.setLevel(logging.ERROR)
stderr_handler.setFormatter(logging.Formatter(_('ERROR: %(message)s')))
logging.basicConfig(
force=True, level=level, handlers=[stdout_handler, stderr_handler]
)
def _add_java_paths_to_config(pathlist, thisconfig):
def path_version_key(s):
versionlist = []

View File

@ -83,6 +83,8 @@ def main():
)
options = parser.parse_args()
common.set_console_logging(options.verbose)
fdroiddir = os.getcwd()
test_config = dict()
examplesdir = common.get_examples_dir()
@ -290,4 +292,9 @@ and https://f-droid.org/docs/Signing_Process'''
)
% os.path.join(fdroiddir, 'repo')
)
logging.info(msg)
if not options.quiet:
# normally, INFO is only shown with --verbose, but show this unless --quiet
logger = logging.getLogger()
logger.setLevel(logging.INFO)
logger.info(msg)
logging.shutdown()

View File

@ -54,6 +54,8 @@ def main():
help=_("Install all signed applications available"))
options = parser.parse_args()
common.set_console_logging(options.verbose)
if not options.appid and not options.all:
parser.error(_("option %s: If you really want to install all the signed apps, use --all") % "all")

View File

@ -95,6 +95,8 @@ def main():
)
options = parser.parse_args()
common.set_console_logging(options.verbose)
if options.all:
options.archive = True
options.build_logs = True
@ -152,10 +154,10 @@ def main():
if hostname == 'f-droid.org' or (
ip is not None and hostname in socket.gethostbyname_ex('f-droid.org')[2]
):
print(
logging.error(
_(
'ERROR: this command should never be used to mirror f-droid.org!\n'
'A full mirror of f-droid.org requires more than 200GB.'
'This command should never be used to mirror f-droid.org! '
'A full copy requires more than 600GB.'
)
)
sys.exit(1)

View File

@ -105,6 +105,8 @@ def main():
parser.add_argument("--no-check-https", action="store_true", default=False)
options = parser.parse_args()
common.set_console_logging(options.verbose)
# Read config.py...
common.read_config(options)