From 7309536dd8546d9d3337dad980d6bc977e1faac7 Mon Sep 17 00:00:00 2001 From: Fede Raimondo Date: Thu, 5 Dec 2024 13:47:19 +0100 Subject: [PATCH] [SCHEMA] Configure only the bst logger instead of the root logger (#1996) * Configure only the bst logger instead of the root logger * Add a logging handler if there are none * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix flake8 --- .../src/bidsschematools/render/tables.py | 7 +------ .../schemacode/src/bidsschematools/render/text.py | 8 +------- tools/schemacode/src/bidsschematools/schema.py | 4 ---- tools/schemacode/src/bidsschematools/utils.py | 15 ++++++++++++++- 4 files changed, 16 insertions(+), 18 deletions(-) diff --git a/tools/schemacode/src/bidsschematools/render/tables.py b/tools/schemacode/src/bidsschematools/render/tables.py index 8eb6b386a4..4b91b223e5 100644 --- a/tools/schemacode/src/bidsschematools/render/tables.py +++ b/tools/schemacode/src/bidsschematools/render/tables.py @@ -2,8 +2,6 @@ from __future__ import annotations -import logging -import os import typing as ty import pandas as pd @@ -11,12 +9,9 @@ from bidsschematools.render import utils from bidsschematools.schema import BIDSSchemaError, Namespace, filter_schema -from bidsschematools.utils import get_logger, set_logger_level +from bidsschematools.utils import get_logger lgr = get_logger() -# Basic settings for output, for now just basic -set_logger_level(lgr, os.environ.get("BIDS_SCHEMA_LOG_LEVEL", logging.INFO)) -logging.basicConfig(format="%(asctime)-15s [%(levelname)8s] %(message)s") # Remember to add extension (.html or .md) to the paths when using them. ENTITIES_PATH = "SPEC_ROOT/appendices/entities" diff --git a/tools/schemacode/src/bidsschematools/render/text.py b/tools/schemacode/src/bidsschematools/render/text.py index ce70ae0ded..226b1d408b 100644 --- a/tools/schemacode/src/bidsschematools/render/text.py +++ b/tools/schemacode/src/bidsschematools/render/text.py @@ -1,19 +1,13 @@ """Functions for rendering portions of the schema as text.""" -import logging -import os - import yaml from markdown_it import MarkdownIt from bidsschematools.render import utils from bidsschematools.schema import Namespace, filter_schema, load_schema -from bidsschematools.utils import get_logger, set_logger_level +from bidsschematools.utils import get_logger lgr = get_logger() -# Basic settings for output, for now just basic -set_logger_level(lgr, os.environ.get("BIDS_SCHEMA_LOG_LEVEL", logging.INFO)) -logging.basicConfig(format="%(asctime)-15s [%(levelname)8s] %(message)s") # Remember to add extension (.html or .md) to the paths when using them. ENTITIES_PATH = "SPEC_ROOT/appendices/entities" diff --git a/tools/schemacode/src/bidsschematools/schema.py b/tools/schemacode/src/bidsschematools/schema.py index a00eaddac9..ad6462f32c 100644 --- a/tools/schemacode/src/bidsschematools/schema.py +++ b/tools/schemacode/src/bidsschematools/schema.py @@ -1,7 +1,6 @@ """Schema loading- and processing-related functions.""" import json -import logging import os import re import sys @@ -21,9 +20,6 @@ from .types import Namespace lgr = utils.get_logger() -# Basic settings for output, for now just basic -utils.set_logger_level(lgr, os.environ.get("BIDS_SCHEMA_LOG_LEVEL", logging.INFO)) -logging.basicConfig(format="%(asctime)-15s [%(levelname)8s] %(message)s") class BIDSSchemaError(Exception): diff --git a/tools/schemacode/src/bidsschematools/utils.py b/tools/schemacode/src/bidsschematools/utils.py index 6f9850edb0..441e8986eb 100644 --- a/tools/schemacode/src/bidsschematools/utils.py +++ b/tools/schemacode/src/bidsschematools/utils.py @@ -1,6 +1,7 @@ """Utility functions for the bids-specification schema.""" import logging +import os from . import data @@ -29,7 +30,19 @@ def get_logger(name=None): logging.Logger logger object. """ - return logging.getLogger("bidsschematools" + (".%s" % name if name else "")) + logger = logging.getLogger("bidsschematools" + (".%s" % name if name else "")) + # Basic settings for output, for now just basic + set_logger_level(logger, os.environ.get("BIDS_SCHEMA_LOG_LEVEL", logging.INFO)) + format = "%(asctime)-15s [%(levelname)8s] %(message)s" + if len(logger.handlers) == 0: + # add a handler if there isn't one + ch = logging.StreamHandler() + logger.addHandler(ch) + # Set the formatter for the handlers + for lh in logger.handlers: + lh.setFormatter(logging.Formatter(format)) + + return logger def set_logger_level(lgr, level):