From e6a8a40b0bff5d344f030d084746826cf071e003 Mon Sep 17 00:00:00 2001 From: Fede Raimondo Date: Thu, 5 Dec 2024 10:19:59 +0100 Subject: [PATCH 1/4] Configure only the bst logger instead of the root logger --- tools/schemacode/src/bidsschematools/render/tables.py | 3 --- tools/schemacode/src/bidsschematools/render/text.py | 3 --- tools/schemacode/src/bidsschematools/schema.py | 4 ---- tools/schemacode/src/bidsschematools/utils.py | 11 ++++++++++- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/tools/schemacode/src/bidsschematools/render/tables.py b/tools/schemacode/src/bidsschematools/render/tables.py index 8eb6b386a4..87af49b04e 100644 --- a/tools/schemacode/src/bidsschematools/render/tables.py +++ b/tools/schemacode/src/bidsschematools/render/tables.py @@ -14,9 +14,6 @@ from bidsschematools.utils import get_logger, set_logger_level 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..c2939310de 100644 --- a/tools/schemacode/src/bidsschematools/render/text.py +++ b/tools/schemacode/src/bidsschematools/render/text.py @@ -11,9 +11,6 @@ from bidsschematools.utils import get_logger, set_logger_level 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..0cefc725aa 100644 --- a/tools/schemacode/src/bidsschematools/schema.py +++ b/tools/schemacode/src/bidsschematools/schema.py @@ -21,10 +21,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): """Errors indicating invalid values in the schema itself""" diff --git a/tools/schemacode/src/bidsschematools/utils.py b/tools/schemacode/src/bidsschematools/utils.py index 6f9850edb0..c518a88bb5 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,15 @@ 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" + for lh in logger.handlers: + lh.setFormatter(logging.Formatter(format)) + + return logger + def set_logger_level(lgr, level): From c2f20d744fce94f55d94e6d320f6258cb98224f4 Mon Sep 17 00:00:00 2001 From: Fede Raimondo Date: Thu, 5 Dec 2024 10:26:10 +0100 Subject: [PATCH 2/4] Add a logging handler if there are none --- tools/schemacode/src/bidsschematools/utils.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tools/schemacode/src/bidsschematools/utils.py b/tools/schemacode/src/bidsschematools/utils.py index c518a88bb5..0aa6d71f45 100644 --- a/tools/schemacode/src/bidsschematools/utils.py +++ b/tools/schemacode/src/bidsschematools/utils.py @@ -34,6 +34,11 @@ def get_logger(name=None): # 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)) From 7286483112d0d6db07c60622913e55498123a2b8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 5 Dec 2024 09:30:16 +0000 Subject: [PATCH 3/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tools/schemacode/src/bidsschematools/schema.py | 1 + tools/schemacode/src/bidsschematools/utils.py | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/schemacode/src/bidsschematools/schema.py b/tools/schemacode/src/bidsschematools/schema.py index 0cefc725aa..55bb501e0f 100644 --- a/tools/schemacode/src/bidsschematools/schema.py +++ b/tools/schemacode/src/bidsschematools/schema.py @@ -22,6 +22,7 @@ lgr = utils.get_logger() + class BIDSSchemaError(Exception): """Errors indicating invalid values in the schema itself""" diff --git a/tools/schemacode/src/bidsschematools/utils.py b/tools/schemacode/src/bidsschematools/utils.py index 0aa6d71f45..441e8986eb 100644 --- a/tools/schemacode/src/bidsschematools/utils.py +++ b/tools/schemacode/src/bidsschematools/utils.py @@ -33,7 +33,7 @@ def get_logger(name=None): 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" + format = "%(asctime)-15s [%(levelname)8s] %(message)s" if len(logger.handlers) == 0: # add a handler if there isn't one ch = logging.StreamHandler() @@ -45,7 +45,6 @@ def get_logger(name=None): return logger - def set_logger_level(lgr, level): """Set the logger level. From 25d501ad36c75953f2cc8b9288e0f4e61b1cd129 Mon Sep 17 00:00:00 2001 From: Fede Raimondo Date: Thu, 5 Dec 2024 10:42:49 +0100 Subject: [PATCH 4/4] Fix flake8 --- tools/schemacode/src/bidsschematools/render/tables.py | 4 +--- tools/schemacode/src/bidsschematools/render/text.py | 5 +---- tools/schemacode/src/bidsschematools/schema.py | 1 - 3 files changed, 2 insertions(+), 8 deletions(-) diff --git a/tools/schemacode/src/bidsschematools/render/tables.py b/tools/schemacode/src/bidsschematools/render/tables.py index 87af49b04e..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,7 +9,7 @@ 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() diff --git a/tools/schemacode/src/bidsschematools/render/text.py b/tools/schemacode/src/bidsschematools/render/text.py index c2939310de..226b1d408b 100644 --- a/tools/schemacode/src/bidsschematools/render/text.py +++ b/tools/schemacode/src/bidsschematools/render/text.py @@ -1,14 +1,11 @@ """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() diff --git a/tools/schemacode/src/bidsschematools/schema.py b/tools/schemacode/src/bidsschematools/schema.py index 55bb501e0f..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