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

update statshandler logging message #6051

Merged
merged 10 commits into from
Feb 25, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
27 changes: 19 additions & 8 deletions monai/handlers/stats_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import torch

from monai.config import IgniteInfo
from monai.utils import is_scalar, min_version, optional_import
from monai.utils import deprecated_arg_default, is_scalar, min_version, optional_import

Events, _ = optional_import("ignite.engine", IgniteInfo.OPT_IMPORT_VERSION, min_version, "Events")
if TYPE_CHECKING:
Expand All @@ -41,9 +41,8 @@ class StatsHandler:

Note that if `name` arg is None, will leverage `engine.logger` as default logger directly, otherwise,
get logger from `logging.getLogger(name)`, we can setup a logger outside first with the same `name`.
As the default log level of `RootLogger` is `WARNING`, may need to call
`logging.basicConfig(stream=sys.stdout, level=logging.INFO)` before running this handler to enable
the stats logging.
As the default log level is `WARNING`, it's recommended to call
`logging.getLogger(name).setLevel(logging.INFO)` before running this handler to enable the stats logging.
wyli marked this conversation as resolved.
Show resolved Hide resolved

Default behaviors:
- When EPOCH_COMPLETED, logs ``engine.state.metrics`` using ``self.logger``.
Expand All @@ -52,7 +51,7 @@ class StatsHandler:

Usage example::

logging.basicConfig(stream=sys.stdout, level=logging.INFO)
logging.getLogger("train_stats").setLevel(logging.INFO)

trainer = SupervisedTrainer(...)
StatsHandler(name="train_stats").attach(trainer)
Expand All @@ -64,6 +63,14 @@ class StatsHandler:

"""

@deprecated_arg_default(
"name",
old_default=None,
new_default="StatsHandler",
since="1.1",
replaced="1.3",
msg_suffix="the default logger is 'StatsHandler'.",
)
def __init__(
self,
iteration_log: bool | Callable[[Engine, int], bool] = True,
Expand Down Expand Up @@ -135,10 +142,14 @@ def attach(self, engine: Engine) -> None:
"""
if self.name is None:
self.logger = engine.logger
if self.logger.getEffectiveLevel() > logging.INFO or logging.root.getEffectiveLevel() > logging.INFO:
if self.logger.getEffectiveLevel() > logging.INFO:
suggested = f"\n\nimport logging\nlogging.getLogger('{self.logger.name}').setLevel(logging.INFO)"
if self.logger.name != engine.logger.name:
suggested += f"\nlogging.getLogger('{engine.logger.name}').setLevel(logging.INFO)"
suggested += "\n\n"
warnings.warn(
"the effective log level of engine logger or RootLogger is higher than INFO, may not record log,"
" please call `logging.basicConfig(stream=sys.stdout, level=logging.INFO)` to enable it."
f"the effective log level of {self.logger.name} higher than INFO, StatsHandler may not generate logs,"
f"\nplease use the following code before running the engine to enable it: {suggested}"
)
if self.iteration_log and not engine.has_event_handler(self.iteration_completed, Events.ITERATION_COMPLETED):
event = Events.ITERATION_COMPLETED
Expand Down
3 changes: 2 additions & 1 deletion tests/test_handler_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ def _train_func(engine, batch):

# set up testing handler
stats_handler = StatsHandler(name=None, tag_name=key_to_print)
stats_handler.attach(engine)
with self.assertWarns(Warning): # engine logging level warn
stats_handler.attach(engine)
# leverage `engine.logger` to print info
engine.logger.setLevel(logging.INFO)
level = logging.root.getEffectiveLevel()
Expand Down