From 596bddbb6214dfe4910bed82404925e9d7097f8d Mon Sep 17 00:00:00 2001 From: kernelmethod <17100608+kernelmethod@users.noreply.github.com> Date: Mon, 11 Nov 2024 14:28:08 -0700 Subject: [PATCH] Fix logging nuisances that can arise when importing flaml Previously, FLAML would automatically set its own loglevel when imported, overriding any other logging settings that may previously have been set by users of the library. FLAML will now check whether a loglevel has been explicitly set before setting its own. In addition, the `flaml.automl` warning is now emitted using `warnings` rather than `logging.warning`. This falls more in line with other FLAML submodules (e.g. `flaml.ml`). Taken in conjunction, these two changes make it a lot easier for downstream users to suppress warnings emitted by FLAML upon importing the library, either by configuring logging prior to importing `flaml` or by using the somewhat more idiomatic ``` with warnings.catch_warnings(action="ignore"): import flaml ``` This change also helps to ensure that FLAML's defaults won't override users' explicit logging settings. --- flaml/__init__.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/flaml/__init__.py b/flaml/__init__.py index 8664127e3a..2d7bfadfdc 100644 --- a/flaml/__init__.py +++ b/flaml/__init__.py @@ -1,4 +1,5 @@ import logging +import warnings try: from flaml.automl import AutoML, logger_formatter @@ -12,7 +13,8 @@ # Set the root logger. logger = logging.getLogger(__name__) -logger.setLevel(logging.INFO) +if logger.level == logging.NOTSET: + logger.setLevel(logging.INFO) if not has_automl: - logger.warning("flaml.automl is not available. Please install flaml[automl] to enable AutoML functionalities.") + warnings.warn("flaml.automl is not available. Please install flaml[automl] to enable AutoML functionalities.")