Source code for medical_image.utils.logging

# SPDX-License-Identifier: AGPL-3.0-only
#
# Copyright (C) 2024-2026 [YOUR NAME / ORGANIZATION]
#
# This file is part of medical-image-std.
#
# medical-image-std is free software: you can redistribute it and/or
# modify it under the terms of the GNU Affero General Public License
# as published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# medical-image-std is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with medical-image-std. If not, see
# <https://www.gnu.org/licenses/>.

import logging

logger = logging.getLogger("medical_image")
logger.addHandler(logging.NullHandler())


[docs] def configure_logging(level=logging.DEBUG, log_file=None): """ Optional convenience function for users who want console/file logging. Args: level: Logging level (default DEBUG). log_file: Path to a log file. If None, only console output. """ logger.setLevel(level) formatter = logging.Formatter( "%(asctime)s | %(levelname)s | %(message)s", datefmt="%Y%m%d-%H:%M:%S", ) if not any(isinstance(h, logging.StreamHandler) for h in logger.handlers): console = logging.StreamHandler() console.setFormatter(formatter) logger.addHandler(console) if log_file and not any( isinstance(h, logging.FileHandler) for h in logger.handlers ): fh = logging.FileHandler(log_file, mode="a") fh.setFormatter(formatter) logger.addHandler(fh)