Skip to content

Commit 1b2e5cd

Browse files
authored
feat: support configure logging fmt of setup_logger (#5846)
## Changes Made 1. support configure log format and date format 2. enable all logger by default 3. modify documentation to easy setup. ## Related Issues <!-- Link to related GitHub issues, e.g., "Closes #123" -->
1 parent 273372f commit 1b2e5cd

File tree

2 files changed

+22
-27
lines changed

2 files changed

+22
-27
lines changed

daft/logging.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,18 @@ def setup_debug_logger() -> None:
3636
def setup_logger(
3737
level: str = "debug",
3838
exclude_prefix: typing.Iterable[str] | None = None,
39-
daft_only: bool = True,
39+
daft_only: bool = False,
40+
logformat: str = "%(asctime)s - %(name)s - %(levelname)s - %(message)s",
41+
datefmt: str = "%Y-%m-%d %H:%M:%S.%s".format(),
4042
) -> None:
4143
"""Setup Daft logger with a specific log level, optional prefix filtering, and Rust sync.
4244
4345
Args:
4446
level (str, optional): The log level to use. Valid options are `DEBUG`, `INFO`, `WARNING`, `ERROR`. Defaults to `DEBUG`.
4547
exclude_prefix (typing.Iterable[str] | None, optional): A list of prefixes to exclude from logging. Defaults to None.
46-
daft_only (bool, optional): Whether to only log messages from the Daft module. Defaults to True.
48+
daft_only (bool, optional): Whether to only log messages from the Daft module. Defaults to False.
49+
logformat (str, optional): The log format to use. Defaults to "%(asctime)s - %(name)s - %(levelname)s - %(message)s".
50+
datefmt (str, optional): The date format to use. Defaults to '%Y-%m-%d %H:%M:%S.%s'.format().
4751
4852
Raises:
4953
ValueError: If the log level is not valid.
@@ -55,7 +59,7 @@ def setup_logger(
5559
if level == "WARN":
5660
level = "WARNING"
5761

58-
logging.basicConfig(level=level)
62+
logging.basicConfig(level=level, format=logformat, datefmt=datefmt)
5963

6064
root_logger: logging.Logger = logging.getLogger()
6165

docs/optimization/logging.md

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@ setup_logger()
3434
The `setup_logger()` function accepts three optional parameters:
3535

3636
- `level` (str, optional): The log level to use. Valid options are `DEBUG`, `INFO`, `WARNING`, `ERROR`. Defaults to `DEBUG`.
37-
- `daft_only` (bool, optional): When True, only logs from Daft modules will be shown. Defaults to True.
37+
- `daft_only` (bool, optional): When True, only logs from Daft modules will be shown. Defaults to False.
3838
- `exclude_prefix` (list[str], optional): A list of module prefixes to exclude from logging. Defaults to [].
39+
- `logformat` (str, optional): The log format to use. Defaults to
40+
`"%(asctime)s - %(name)s - %(levelname)s - %(message)s"`.
41+
- `datefmt` (str, optional): The date format to use in the log format. Defaults to `"%Y-%m-%d %H:%M:%S.%s"`.
3942

4043
By default, `setup_logger()` sets the root logger to `DEBUG` level and applies a filter to only show logs from Daft modules. This makes it easier to focus on Daft-specific logs while debugging.
4144

@@ -44,7 +47,7 @@ For most use cases, simply using `logging.getLogger(__name__)` is the common fir
4447
!!! note
4548
- It's important to set the logging level before importing Daft, as the underlying Rust components will not pick up level changes that occur after the import.
4649

47-
- After each logger setup,` refresh_logger()` is called to synchronize the configuration with the Rust backend.
50+
- The `setup_logger()` doesn't have the above mentioned side effects. The ` refresh_logger()` will been called to synchronize the configuration with the Rust backend if use `setup_logger()`.
4851
## Remote Execution
4952

5053
When running Daft on a Ray cluster, logging is more complex due to the distributed nature of the system. Logs can be emitted from the driver process (where you call `ray.init()`) or from the worker processes (where the actual data processing happens).
@@ -60,15 +63,15 @@ Here are a few ways to configure logging when using Daft with Ray:
6063

6164
1. **Suppressing Worker Logs on the Driver**: By default, Ray forwards logs from all worker processes to the driver, which can be overwhelming. If you only want to see driver logs, you can disable this behavior by setting `log_to_driver=False` in `ray.init()`:
6265

63-
```python
64-
import ray
66+
```python
67+
import ray
6568

66-
ray.init(log_to_driver=False)
67-
```
69+
ray.init(log_to_driver=False)
70+
```
6871

6972
2. **Per-Worker Logging Configuration**: If you need to set different logging levels for different modules on each worker, you can use a `worker_process_setup_hook`. This is a function that runs at the start of each worker process, allowing you to configure logging before any Daft code is executed.
7073

71-
Here is an example of how to define a setup hook to configure logging on each worker:
74+
Here is an example of how to define a setup hook to configure logging on each worker, and also you can use `setup_logger()` to configure logging.
7275

7376
```python
7477
def configure_logging():
@@ -103,28 +106,16 @@ Here is a complete example of how to initialize Ray with a combination of these
103106
import logging
104107
import ray
105108
from ray.job_config import LoggingConfig
106-
107-
# Configure logging BEFORE importing daft
108-
logging.basicConfig(
109-
level=logging.INFO,
110-
format="%(asctime)s %(levelname)s %(name)s: %(message)s"
111-
)
112-
# Example of setting a specific Daft module to DEBUG
113-
logging.getLogger("daft.distributed").setLevel(logging.DEBUG)
114-
115-
# Import daft AFTER setting up logging
116109
import daft
117110

118111
# A setup hook to configure logging on each worker
119112
def configure_logging():
120-
import logging
121-
logging.basicConfig(
122-
level=logging.INFO,
123-
format="%(asctime)s %(levelname)s %(name)s: %(message)s"
124-
)
125-
# Example of setting a specific Daft module to DEBUG
126-
logging.getLogger("daft.distributed").setLevel(logging.DEBUG)
113+
from daft.logging import setup_logger
114+
# Example of setting Daft module to INFO level except for daft_distributed module
115+
setup_logger(level="INFO", exclude_prefix=["daft_distributed"])
116+
127117

118+
configure_logging()
128119

129120
# Initialize Ray with advanced logging and environment settings
130121
ray.init(

0 commit comments

Comments
 (0)