From 923c83f2f9a0c1e16f753f9a89991c41c9d2de85 Mon Sep 17 00:00:00 2001 From: Adal Chiriliuc Date: Thu, 2 May 2024 18:13:11 +0300 Subject: [PATCH 1/2] more environment logging --- b2/_internal/_cli/const.py | 3 +++ b2/_internal/console_tool.py | 5 +++++ docker/Dockerfile.template | 1 + 3 files changed, 9 insertions(+) diff --git a/b2/_internal/_cli/const.py b/b2/_internal/_cli/const.py index 29d40a431..95b751401 100644 --- a/b2/_internal/_cli/const.py +++ b/b2/_internal/_cli/const.py @@ -26,3 +26,6 @@ CREATE_BUCKET_TYPES = ('allPublic', 'allPrivate') B2_ESCAPE_CONTROL_CHARACTERS = 'B2_ESCAPE_CONTROL_CHARACTERS' + +# Set to 1 when running under B2 CLI as a Docker container +B2_CLI_DOCKER_ENV_VAR = 'B2_CLI_DOCKER' diff --git a/b2/_internal/console_tool.py b/b2/_internal/console_tool.py index e011ebe3b..af221cdf1 100644 --- a/b2/_internal/console_tool.py +++ b/b2/_internal/console_tool.py @@ -147,6 +147,7 @@ from b2._internal._cli.const import ( B2_APPLICATION_KEY_ENV_VAR, B2_APPLICATION_KEY_ID_ENV_VAR, + B2_CLI_DOCKER_ENV_VAR, B2_DESTINATION_SSE_C_KEY_B64_ENV_VAR, B2_DESTINATION_SSE_C_KEY_ID_ENV_VAR, B2_ENVIRONMENT_ENV_VAR, @@ -5406,6 +5407,8 @@ def _setup_logging(cls, args, argv): logger.info(r'// %s %s %s \\', SEPARATOR, VERSION.center(8), SEPARATOR) logger.debug('platform is %s', platform.platform()) + if os.environ.get(B2_CLI_DOCKER_ENV_VAR) == "1": + logger.debug('running as a Docker container') logger.debug( 'Python version is %s %s', platform.python_implementation(), sys.version.replace('\n', ' ') @@ -5413,6 +5416,8 @@ def _setup_logging(cls, args, argv): logger.debug('b2sdk version is %s', b2sdk_version) logger.debug('locale is %s', locale.getlocale()) logger.debug('filesystem encoding is %s', sys.getfilesystemencoding()) + logger.debug('default encoding is %s', sys.getdefaultencoding()) + logger.debug('flags.utf8_mode is %s', sys.flags.utf8_mode) # used by Sphinx diff --git a/docker/Dockerfile.template b/docker/Dockerfile.template index f93ae8252..254df4e52 100644 --- a/docker/Dockerfile.template +++ b/docker/Dockerfile.template @@ -23,6 +23,7 @@ LABEL vcs-url="${vcs_url}" LABEL vcs-ref="${vcs_ref}" LABEL build-date-iso8601="${build_date}" +ENV B2_CLI_DOCKER=1 ENV PYTHONPATH=/opt/b2 COPY ./docker/entrypoint.sh /entrypoint.sh COPY --from=builder /b2/__pypackages__/${python_version}/lib /opt/b2 From 01654683eb3bfd6e29f82b9c9d9d3d33615f054b Mon Sep 17 00:00:00 2001 From: Adal Chiriliuc Date: Thu, 2 May 2024 21:23:12 +0300 Subject: [PATCH 2/2] more robust logging setup --- b2/_internal/console_tool.py | 5 +++-- changelog.d/+logging-encoding.fixed.md | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) create mode 100644 changelog.d/+logging-encoding.fixed.md diff --git a/b2/_internal/console_tool.py b/b2/_internal/console_tool.py index af221cdf1..0ecc0601b 100644 --- a/b2/_internal/console_tool.py +++ b/b2/_internal/console_tool.py @@ -5375,12 +5375,13 @@ def _print_stderr(self, *args, **kwargs): def _setup_logging(cls, args, argv): if args.log_config and (args.verbose or args.debug_logs): raise ValueError('Please provide either --log-config or --verbose/--debug-logs') + errors_kwarg = {'errors': 'backslashreplace'} if sys.version_info >= (3, 9) else {} if args.log_config: logging.config.fileConfig(args.log_config) elif args.verbose or args.debug_logs: # set log level to DEBUG for ALL loggers (even those not belonging to B2), but without any handlers, # those will added as needed (file and/or stderr) - logging.basicConfig(level=logging.DEBUG, handlers=[]) + logging.basicConfig(level=logging.DEBUG, handlers=[], **errors_kwarg) else: logger.setLevel(logging.CRITICAL + 1) # No logs! if args.verbose: @@ -5395,7 +5396,7 @@ def _setup_logging(cls, args, argv): '%(asctime)s\t%(process)d\t%(thread)d\t%(name)s\t%(levelname)s\t%(message)s' ) formatter.converter = time.gmtime - handler = logging.FileHandler('b2_cli.log') + handler = logging.FileHandler('b2_cli.log', **errors_kwarg) handler.setFormatter(formatter) # logs from ALL loggers sent to the log file should be formatted this way diff --git a/changelog.d/+logging-encoding.fixed.md b/changelog.d/+logging-encoding.fixed.md new file mode 100644 index 000000000..000090ff2 --- /dev/null +++ b/changelog.d/+logging-encoding.fixed.md @@ -0,0 +1 @@ +Escape invalid unicode characters in log messages. \ No newline at end of file