Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suppress TensorFlow info messages to debug level #721

Merged
merged 8 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions annif/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ def create_flask_app(config_name: str | None = None) -> Flask:
"""Create a Flask app to be used by the CLI."""
from flask import Flask

_set_tensorflow_loglevel()

app = Flask(__name__)
config_name = _get_config_name(config_name)
logger.debug(f"creating flask app with configuration {config_name}")
Expand Down Expand Up @@ -75,3 +77,20 @@ def _get_config_name(config_name: str | None) -> str:
else:
config_name = "annif.default_config.ProductionConfig" # pragma: no cover
return config_name


def _set_tensorflow_loglevel():
"""Set TensorFlow log level based on Annif log level (--verbosity/-v
option) using an environment variable. INFO messages by TF are shown only on
DEBUG (or NOTSET) level of Annif."""
annif_loglevel = logger.getEffectiveLevel()
tf_loglevel_mapping = {
0: "0", # NOTSET
10: "0", # DEBUG
20: "1", # INFO
30: "1", # WARNING
40: "2", # ERROR
50: "3", # CRITICAL
}
tf_loglevel = tf_loglevel_mapping[annif_loglevel]
os.environ.setdefault("TF_CPP_MIN_LOG_LEVEL", tf_loglevel)
21 changes: 21 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,27 @@
PROJECTS_CONFIG_PATH = "tests/projects_for_config_path_option.cfg"


@mock.patch.dict(os.environ, clear=True)
def test_tensorflow_loglevel():
tf_env = "TF_CPP_MIN_LOG_LEVEL"

runner.invoke(annif.cli.cli, ["list-projects", "-v", "DEBUG"])
assert os.environ[tf_env] == "0" # Show INFO, WARNING and ERROR messages by TF
os.environ.pop(tf_env)
runner.invoke(annif.cli.cli, ["list-projects"]) # INFO level by default
assert os.environ[tf_env] == "1" # Show WARNING and ERROR messages by TF
os.environ.pop(tf_env)
runner.invoke(annif.cli.cli, ["list-projects", "-v", "WARN"])
assert os.environ[tf_env] == "1" # Show WARNING and ERROR messages by TF
os.environ.pop(tf_env)
runner.invoke(annif.cli.cli, ["list-projects", "-v", "ERROR"])
assert os.environ[tf_env] == "2" # Show ERROR messages by TF
os.environ.pop(tf_env)
runner.invoke(annif.cli.cli, ["list-projects", "-v", "CRITICAL"])
assert os.environ[tf_env] == "3" # Show no messages by TF
os.environ.pop(tf_env)


def test_list_projects():
result = runner.invoke(annif.cli.cli, ["list-projects"])
assert not result.exception
Expand Down