|
| 1 | +Logging Configuration |
| 2 | +===================== |
| 3 | + |
| 4 | +mloptimizer follows the standard Python library logging pattern, giving you full control over log output. By default, the library uses a `NullHandler`, meaning no log messages are displayed unless you explicitly configure logging. |
| 5 | + |
| 6 | +Using the verbose Parameter (Recommended) |
| 7 | +----------------------------------------- |
| 8 | + |
| 9 | +The easiest way to enable logging is using the ``verbose`` parameter in ``GeneticSearch``: |
| 10 | + |
| 11 | +.. code-block:: python |
| 12 | +
|
| 13 | + from mloptimizer.interfaces import GeneticSearch |
| 14 | +
|
| 15 | + # Silent (default) |
| 16 | + opt = GeneticSearch(estimator_class=..., hyperparam_space=..., verbose=0) |
| 17 | +
|
| 18 | + # Info level - shows optimization lifecycle |
| 19 | + opt = GeneticSearch(estimator_class=..., hyperparam_space=..., verbose=1) |
| 20 | +
|
| 21 | + # Debug level - shows detailed evaluation info |
| 22 | + opt = GeneticSearch(estimator_class=..., hyperparam_space=..., verbose=2) |
| 23 | +
|
| 24 | +This is similar to how scikit-learn and XGBoost handle verbosity. |
| 25 | + |
| 26 | +Basic Configuration |
| 27 | +------------------- |
| 28 | + |
| 29 | +To enable logging output, configure Python's logging module before running your optimization: |
| 30 | + |
| 31 | +.. code-block:: python |
| 32 | +
|
| 33 | + import logging |
| 34 | +
|
| 35 | + # Enable INFO level logging globally |
| 36 | + logging.basicConfig(level=logging.INFO) |
| 37 | +
|
| 38 | + # Now use mloptimizer |
| 39 | + from mloptimizer.interfaces import GeneticSearch |
| 40 | + # ... your optimization code |
| 41 | +
|
| 42 | +This will display informative messages about the optimization process, including start/end summaries and progress updates. |
| 43 | + |
| 44 | +Configuring mloptimizer's Logger Only |
| 45 | +------------------------------------- |
| 46 | + |
| 47 | +To enable logging specifically for mloptimizer without affecting other libraries: |
| 48 | + |
| 49 | +.. code-block:: python |
| 50 | +
|
| 51 | + import logging |
| 52 | +
|
| 53 | + # Get mloptimizer's logger |
| 54 | + mlopt_logger = logging.getLogger("mloptimizer") |
| 55 | + mlopt_logger.setLevel(logging.INFO) |
| 56 | +
|
| 57 | + # Add a handler to output to console |
| 58 | + handler = logging.StreamHandler() |
| 59 | + handler.setFormatter(logging.Formatter("%(asctime)s [%(levelname)s]: %(message)s")) |
| 60 | + mlopt_logger.addHandler(handler) |
| 61 | +
|
| 62 | +Logging to a File |
| 63 | +----------------- |
| 64 | + |
| 65 | +To save logs to a file for later analysis: |
| 66 | + |
| 67 | +.. code-block:: python |
| 68 | +
|
| 69 | + import logging |
| 70 | +
|
| 71 | + # Configure logging to file |
| 72 | + logging.basicConfig( |
| 73 | + filename='optimization.log', |
| 74 | + level=logging.INFO, |
| 75 | + format="%(asctime)s [%(levelname)s]: %(message)s" |
| 76 | + ) |
| 77 | +
|
| 78 | +Or configure both console and file output: |
| 79 | + |
| 80 | +.. code-block:: python |
| 81 | +
|
| 82 | + import logging |
| 83 | +
|
| 84 | + # Create logger |
| 85 | + logger = logging.getLogger("mloptimizer") |
| 86 | + logger.setLevel(logging.INFO) |
| 87 | +
|
| 88 | + # Console handler |
| 89 | + console_handler = logging.StreamHandler() |
| 90 | + console_handler.setFormatter(logging.Formatter("%(asctime)s [%(levelname)s]: %(message)s")) |
| 91 | + logger.addHandler(console_handler) |
| 92 | +
|
| 93 | + # File handler |
| 94 | + file_handler = logging.FileHandler("optimization.log") |
| 95 | + file_handler.setFormatter(logging.Formatter("%(asctime)s [%(levelname)s]: %(message)s")) |
| 96 | + logger.addHandler(file_handler) |
| 97 | +
|
| 98 | +Log Levels |
| 99 | +---------- |
| 100 | + |
| 101 | +mloptimizer uses standard Python logging levels: |
| 102 | + |
| 103 | +- **DEBUG**: Detailed information for diagnosing problems (individual evaluations, internal state) |
| 104 | +- **INFO**: Confirmation that things are working as expected (optimization start/end, generation summaries) |
| 105 | +- **WARNING**: Indication of potential issues (deprecated parameters, suboptimal configurations) |
| 106 | +- **ERROR**: Serious problems that prevent operation |
| 107 | + |
| 108 | +Example with DEBUG level: |
| 109 | + |
| 110 | +.. code-block:: python |
| 111 | +
|
| 112 | + import logging |
| 113 | + logging.getLogger("mloptimizer").setLevel(logging.DEBUG) |
| 114 | +
|
| 115 | +Silencing Logs |
| 116 | +-------------- |
| 117 | + |
| 118 | +To completely suppress mloptimizer's log output: |
| 119 | + |
| 120 | +.. code-block:: python |
| 121 | +
|
| 122 | + import logging |
| 123 | + logging.getLogger("mloptimizer").setLevel(logging.CRITICAL) |
| 124 | +
|
| 125 | +Or to only see warnings and errors: |
| 126 | + |
| 127 | +.. code-block:: python |
| 128 | +
|
| 129 | + import logging |
| 130 | + logging.getLogger("mloptimizer").setLevel(logging.WARNING) |
| 131 | +
|
| 132 | +Integration with scikit-learn |
| 133 | +----------------------------- |
| 134 | + |
| 135 | +Since mloptimizer follows the same logging pattern as scikit-learn and other major Python libraries, you can configure logging for your entire ML pipeline consistently: |
| 136 | + |
| 137 | +.. code-block:: python |
| 138 | +
|
| 139 | + import logging |
| 140 | +
|
| 141 | + # Configure logging for all ML libraries |
| 142 | + logging.basicConfig( |
| 143 | + level=logging.INFO, |
| 144 | + format="%(asctime)s [%(name)s] %(levelname)s: %(message)s" |
| 145 | + ) |
| 146 | +
|
| 147 | + # Now all libraries (mloptimizer, sklearn, etc.) will log consistently |
0 commit comments