harbor run -t openthoughts/tasktrove-exp-flat25-stackoverflow-v2-5k-stackoverflow-4262harbor download openthoughts/tasktrove-exp-flat25-stackoverflow-v2-5k-stackoverflow-4262Title: Implement a lightweight global logger with context manager overrides in nnabla_rl/logger.py
Body: I'm building a small utility within the nnabla_rl package and need a minimal global logger module that supports temporary overrides via context managers.
Requirements to implement (in a new file nnabla_rl/logger.py):
Expose a module-level object named logger with a mutable boolean attribute disabled. The default value must be False (logging enabled).
Provide a function log(message, *args, **kwargs) that prints or formats a message only when logging is not disabled. The function should be safe to call regardless of the current state (i.e., it should not raise even if disabled).
Provide two context managers:
Nesting behavior: The context managers must support nesting correctly so that inner contexts restore to the immediately previous state (i.e., each enter/exit should push/pop the disabled state).
Specific tests to satisfy:
After importing, logger.logger.disabled exists and defaults to False.
Test sequence:
API usage should be accessible as:
Implementation notes:
Use only the standard library (no external dependencies).
Implement the context managers using @contextlib.contextmanager or simple classes with enter/exit.
Ensure log() respects the disabled flag (no output when disabled) and remains a no-op when disabled.
Keep the implementation small and self-contained to satisfy tests without additional configuration.
Deliverable:
Example usage (optional, for clarity):
import nnabla_rl.logger as logger print(logger.logger.disabled) # False logger.log("hello") # prints because enabled with logger.enable_logging(): print(logger.logger.disabled) # False with logger.disable_logging(): print(logger.logger.disabled) # True print(logger.logger.disabled) # False if outer state was False
If anyone has a compact, dependency-free implementation that satisfies all of the above, I’d appreciate it.