Skip to content

Commit

Permalink
[FEAT] Add function to refresh logger state for rust (#2323)
Browse files Browse the repository at this point in the history
When daft is imported it cached the current python log level to avoid
grabbing the GIL for every log statement. See [this
page](https://docs.rs/pyo3-log/latest/pyo3_log/#performance-filtering-and-caching)
for more details.

This PR exposes a method to "refresh" the cache. This is useful when the
user updates the log level later in the code, like when they configure
logging.

Here is a example showing this. Note prior to refreshing the logger, no
logs were coming out!
<img width="521" alt="image"
src="https://github.com/Eventual-Inc/Daft/assets/2550285/de7ad2d8-8335-4dd0-b7d5-8b36ef28d7ad">
  • Loading branch information
samster25 committed May 29, 2024
1 parent 51451f9 commit 8a0d844
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ daft-plan = {path = "src/daft-plan", default-features = false}
daft-scan = {path = "src/daft-scan", default-features = false}
daft-stats = {path = "src/daft-stats", default-features = false}
daft-table = {path = "src/daft-table", default-features = false}
lazy_static = {workspace = true}
lzma-sys = {version = "*", features = ["static"]}
pyo3 = {workspace = true, optional = true}
pyo3-log = {workspace = true, optional = true}
Expand Down
7 changes: 7 additions & 0 deletions daft/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

from daft.daft import build_type as _build_type
from daft.daft import version as _version
from daft.daft import refresh_logger as _refresh_logger


def get_version() -> str:
Expand All @@ -35,6 +36,11 @@ def get_build_type() -> str:
return _build_type()


def refresh_logger() -> None:
"""Refreshes Daft's internal rust logging to the current python log level"""
_refresh_logger()


__version__ = get_version()


Expand Down Expand Up @@ -112,6 +118,7 @@ def get_build_type() -> str:
"Series",
"TimeUnit",
"register_viz_hook",
"refresh_logger",
"udf",
"ResourceRequest",
"set_planning_config",
Expand Down
1 change: 1 addition & 0 deletions daft/daft.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1498,6 +1498,7 @@ class PyDaftPlanningConfig:

def build_type() -> str: ...
def version() -> str: ...
def refresh_logger() -> None: ...
def __getattr__(name) -> Any: ...
def io_glob(
path: str,
Expand Down
14 changes: 12 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ pub static malloc_conf: Option<&'static libc::c_char> = Some(unsafe {

#[cfg(feature = "python")]
pub mod pylib {
use lazy_static::lazy_static;
use pyo3::prelude::*;

lazy_static! {
static ref LOG_RESET_HANDLE: pyo3_log::ResetHandle = pyo3_log::init();
}
#[pyfunction]
pub fn version() -> &'static str {
daft_core::VERSION
Expand All @@ -45,9 +48,15 @@ pub mod pylib {
daft_core::DAFT_BUILD_TYPE
}

#[pyfunction]
pub fn refresh_logger() {
LOG_RESET_HANDLE.reset();
}

#[pymodule]
fn daft(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
pyo3_log::init();
refresh_logger();

common_daft_config::register_modules(_py, m)?;
common_system_info::register_modules(_py, m)?;
daft_core::register_modules(_py, m)?;
Expand All @@ -63,6 +72,7 @@ pub mod pylib {
daft_scan::register_modules(_py, m)?;
m.add_wrapped(wrap_pyfunction!(version))?;
m.add_wrapped(wrap_pyfunction!(build_type))?;
m.add_wrapped(wrap_pyfunction!(refresh_logger))?;
Ok(())
}
}

0 comments on commit 8a0d844

Please sign in to comment.