Skip to content

Commit

Permalink
Fix logging nuisances that can arise when importing flaml
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kernelmethod committed Nov 11, 2024
1 parent 5a74227 commit 596bddb
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions flaml/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import warnings

try:
from flaml.automl import AutoML, logger_formatter
Expand All @@ -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.")

0 comments on commit 596bddb

Please sign in to comment.