Skip to content

Commit

Permalink
CLI: log to stderr instead of stdout
Browse files Browse the repository at this point in the history
Since the `ls` command produces machine-readable output, we need to ensure
that any log messages produced do not corrupt that output.

Logging to stderr is also conventional; Python's `StreamHandler` even
defaults to it.

This breaks the `import` command tests, because previously they were looking
at the last log message. I don't think we should be testing log messages, so
add a proper `print` to this command. This also makes it consistent with the
`create` command.
  • Loading branch information
SpecLad committed Dec 6, 2024
1 parent b5c0971 commit 44d6397
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
4 changes: 4 additions & 0 deletions changelog.d/20241206_135902_roman_cli_logging.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
### Changed

- \[CLI\] Log messages are now printed on stderr rather than stdout
(<https://github.com/cvat-ai/cvat/pull/8784>)
3 changes: 2 additions & 1 deletion cvat-cli/src/cvat_cli/_internal/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,11 +418,12 @@ def configure_parser(self, parser: argparse.ArgumentParser) -> None:
)

def execute(self, client: Client, *, filename: str, status_check_period: int) -> None:
client.tasks.create_from_backup(
task = client.tasks.create_from_backup(
filename=filename,
status_check_period=status_check_period,
pbar=DeferredTqdmProgressReporter(),
)
print(f"Created task ID", task.id)


@COMMANDS.command_class("auto-annotate")
Expand Down
2 changes: 1 addition & 1 deletion cvat-cli/src/cvat_cli/_internal/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def configure_logger(logger: logging.Logger, parsed_args: argparse.Namespace) ->
formatter = logging.Formatter(
"[%(asctime)s] %(levelname)s: %(message)s", datefmt="%Y-%m-%d %H:%M:%S", style="%"
)
handler = logging.StreamHandler(sys.stdout)
handler = logging.StreamHandler(sys.stderr)
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(level)
Expand Down

0 comments on commit 44d6397

Please sign in to comment.