abseil / abseil-py

Abseil Common Libraries (Python)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Initializing root logger

adamgayoso opened this issue · comments

I feel that the root logger should not be set by a single python package. This is creating double logging for our package that depends on Flax, Jax, etc, as we have our own handler on the logger in our namespace.

if not logging.root.handlers:
logging.basicConfig()

I could be wrong but in this code:

original_logger_class = logging.getLoggerClass()
logging.setLoggerClass(ABSLLogger)
_absl_logger = logging.getLogger('absl')
logging.setLoggerClass(original_logger_class)
python_logging_formatter = PythonFormatter()
_absl_handler = ABSLHandler(python_logging_formatter)

The handler should be added with _absl_logger.addHandler(_absl_handler) while logging.setLoggerClass(ABSLLogger) should be _absl_logger.setLoggerClass(ABSLLogger) with the surrounding lines removed

(1) Re the first reference of calling logging.basicConfig(): this is the same behavior in Python's own std logging library:

https://github.com/python/cpython/blob/8f31bf46980956c735dd18f9914f3e7144e87c77/Lib/logging/__init__.py#L2140-L2141

(2) Re the second reference, the _absl_handler is created at absl.logging import time but not added to any logger, to avoid exactly the double logging issue. The handler is only added to the root logger when use_absl_handler() is explicitly called, e.g. if you use absl.app.run() (see

logging.use_absl_handler()
)

So if you are seeing double logging:

  1. because of the logging.basicConfig() call from (1), then it's the same behavior if use you std logging statements.
  2. because of half of the logs are from the _absl_handler, then somewhere use_absl_handler() is called (or much less likely that you explicitly added _absl_handler to a logger).

Thank you, our problem was actually found elsewhere! Thanks for the info!