Skip to content

Commit

Permalink
Merge pull request #1030 from reef-technologies/more-env-logging
Browse files Browse the repository at this point in the history
Escape invalid unicode in logging, log more locale info
  • Loading branch information
mjurbanski-reef authored May 3, 2024
2 parents dac0505 + 0165468 commit 160e9d1
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
3 changes: 3 additions & 0 deletions b2/_internal/_cli/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
10 changes: 8 additions & 2 deletions b2/_internal/console_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -5374,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:
Expand All @@ -5394,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
Expand All @@ -5406,13 +5408,17 @@ 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', ' ')
)
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
Expand Down
1 change: 1 addition & 0 deletions changelog.d/+logging-encoding.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Escape invalid unicode characters in log messages.
1 change: 1 addition & 0 deletions docker/Dockerfile.template
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 160e9d1

Please sign in to comment.