Logging#

Loguru-based logging with console and rotating file handlers. Use get_logger() in every module.

thesis.core.logging#

Logging system for the thesis framework.

This module provides a centralized logging configuration using loguru. It supports both console and file output with rotation, colored output, and structured logging for medical imaging processing pipelines.

Example

>>> from thesis.core.logging import get_logger
>>> logger = get_logger(__name__)
>>> logger.info("Processing started")
>>> logger.debug(f"Image shape: {shape}")
thesis.core.logging.get_logger(name=None)[source]#

Get a logger instance for a module.

This function returns the global loguru logger with context about the calling module. The logger is automatically configured on first use.

Parameters:

name (Optional[str]) – Name of the module/component (typically __name__)

Return type:

Any

Returns:

Logger instance with module context

Example

>>> logger = get_logger(__name__)
>>> logger.info("Processing image")
thesis.core.logging.setup_logging(log_dir=None, log_level='INFO', console_output=True, file_output=True, rotation='10 MB', retention='7 days', compression='zip')[source]#

Configure the logging system for the application.

Parameters:
  • log_dir (Union[str, Path, None]) – Directory for log files. If None, uses ‘./logs’

  • log_level (str) – Minimum log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)

  • console_output (bool) – Whether to output logs to console

  • file_output (bool) – Whether to output logs to file

  • rotation (str) – When to rotate log files (size or time-based)

  • retention (str) – How long to keep old log files

  • compression (str) – Compression format for rotated logs

Return type:

None

Example

>>> setup_logging(log_dir="./logs", log_level="DEBUG")
>>> logger.info("Logging configured")
thesis.core.logging.reset_logging()[source]#

Reset the logging system (primarily for testing).

Removes all handlers and resets the initialization flag.

Return type:

None

thesis.core.logging.set_console_level(level)[source]#

Change the console handler’s minimum log level at runtime.

This is used by the CLI output system to suppress verbose loguru messages when the user has not requested -v / --verbose. File handlers are unaffected so the full log is always available.

Parameters:

level (str) – New minimum level (e.g. "WARNING", "DEBUG").

Return type:

None

class thesis.core.logging.InterceptHandler[source]#

Bases: Handler

Intercepts stdlib logging records and forwards them to loguru.

Install once via logging.basicConfig(handlers=[InterceptHandler()], level=0, force=True) to route all third-party library logs (e.g. Nipype, FSL wrappers) through loguru’s formatting.

emit(record)[source]#

Do whatever it takes to actually log the specified logging record.

This version is intended to be implemented by subclasses and so raises a NotImplementedError.

Parameters:

record (LogRecord)

Return type:

None

thesis.core.logging.suppress_nipype_native_logging()[source]#

Remove nipype’s own StreamHandlers to prevent duplicate log output.

Nipype attaches StreamHandlers directly to named loggers (nipype.workflow, nipype.pipeline.*, etc.) that output in nipype’s native format. Since the root InterceptHandler already routes those records to loguru, the native handlers produce duplicate lines. Call this once after nipype is imported.

Return type:

None

thesis.core.logging.suspend_console_logging()[source]#

Temporarily disable console logging while progress UI is active.

Return type:

Iterator[None]

thesis.core.logging.handlers#

Custom log handlers for specialized logging needs.

This module provides custom handlers for specific logging scenarios in medical imaging pipelines, such as patient-specific logs, step logs, and performance tracking.

class thesis.core.logging.handlers.InterceptHandler[source]#

Bases: Handler

Intercepts stdlib logging records and forwards them to loguru.

Install once via logging.basicConfig(handlers=[InterceptHandler()], level=0, force=True) to route all third-party library logs (e.g. Nipype, FSL wrappers) through loguru’s formatting.

emit(record)[source]#

Do whatever it takes to actually log the specified logging record.

This version is intended to be implemented by subclasses and so raises a NotImplementedError.

Parameters:

record (LogRecord)

Return type:

None

thesis.core.logging.formatters#

Log formatting utilities for the thesis framework.

Provides consistent formatting for console and file output with appropriate detail levels and coloring.

thesis.core.logging.formatters.get_console_format(verbose=False)[source]#

Get the console log format string.

Parameters:

verbose (bool) – If True, returns more detailed format

Return type:

str

Returns:

Format string for console output

thesis.core.logging.formatters.get_file_format(minimal=False)[source]#

Get the file log format string.

Parameters:

minimal (bool) – If True, returns minimal format

Return type:

str

Returns:

Format string for file output