Skip to content

Commit

Permalink
Only set log level for the root logger
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hashhar committed Dec 22, 2023
1 parent f99ef24 commit 3b1eb3d
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions trino/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,20 @@
# 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]) -> 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)

0 comments on commit 3b1eb3d

Please sign in to comment.