MCPcopy
hub / github.com/huggingface/transformers / init_test_logger

Function init_test_logger

src/transformers/testing_utils.py:4288–4315  ·  view source on GitHub ↗

Initialize a test-specific logger with colored stderr handler and INFO level for tests. Uses a named logger instead of root logger to avoid conflicts with pytest-xdist parallel execution. Uses stderr instead of stdout to avoid deadlocks with pytest-xdist output capture.

()

Source from the content-addressed store, hash-verified

4286
4287
4288def init_test_logger() -> logging.Logger:
4289 """Initialize a test-specific logger with colored stderr handler and INFO level for tests.
4290
4291 Uses a named logger instead of root logger to avoid conflicts with pytest-xdist parallel execution.
4292 Uses stderr instead of stdout to avoid deadlocks with pytest-xdist output capture.
4293 """
4294 logger = logging.getLogger("transformers.training_test")
4295 logger.setLevel(logging.INFO)
4296
4297 # Only add handler if not already present (avoid duplicate handlers on repeated calls)
4298 if not logger.handlers:
4299 # Use stderr instead of stdout - pytest-xdist captures stdout which can cause deadlocks
4300 ch = logging.StreamHandler(sys.stderr)
4301 ch.setLevel(logging.INFO)
4302
4303 # Use colored formatter if terminal supports it, plain otherwise
4304 if sys.stderr.isatty():
4305 formatter = ColoredFormatter(datefmt="%Y-%m-%d %H:%M:%S")
4306 else:
4307 formatter = logging.Formatter(
4308 "%(asctime)s - %(name)s - %(levelname)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S"
4309 )
4310
4311 ch.setFormatter(formatter)
4312 logger.addHandler(ch)
4313
4314 logger.propagate = False # Don't propagate to root logger to avoid duplicate output
4315 return logger
4316
4317
4318def warn_once(logger_instance: logging.Logger, msg: str) -> None:

Callers 1

test_training_overfitMethod · 0.90

Calls 5

ColoredFormatterClass · 0.85
setLevelMethod · 0.80
isattyMethod · 0.80
setFormatterMethod · 0.80
addHandlerMethod · 0.80

Tested by

no test coverage detected