From 57074aa1ea61e81c56a05ee57cb96671ec914f45 Mon Sep 17 00:00:00 2001 From: Ashhar Hasan Date: Fri, 22 Dec 2023 14:36:36 +0530 Subject: [PATCH] Only set log level for the root logger Before this change we called `setLevel` on each logger with level set to INFO. This meant that it was not possible to change log level for all child modules by doing something like `logging.getLogger('trino').setLevel(logging.DEBUG)` because the child loggers had explicit levels set already. It instead required us to change log levels for each module (`trino.client`, `trino.dbapi`, `trino.auth` etc.) separately. After this change only the root logger `trino` has a default level set. Other child loggers inherit from it. So now the default log level for all modules can be changed by doing `logging.getLogger('trino').setLevel(logging.DEBUG)` for example. --- trino/logging.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/trino/logging.py b/trino/logging.py index 866c3e92..87e879fe 100644 --- a/trino/logging.py +++ b/trino/logging.py @@ -11,12 +11,21 @@ # limitations under the License. import logging +from typing import Optional LEVEL = logging.INFO # TODO: provide interface to use ``logging.dictConfig`` -def get_logger(name: str, log_level: int = LEVEL) -> logging.Logger: +def get_logger(name: str, log_level: Optional[int] = None) -> logging.Logger: logger = logging.getLogger(name) - logger.setLevel(log_level) + # We must not call setLevel by default except on the root logger otherwise + # we cannot change log levels for all modules by changing level of the root + # logger + if log_level is not None: + logger.setLevel(log_level) return logger + + +# set default log level to LEVEL +trino_root_logger = get_logger('trino', LEVEL)