Skip to content

Commit

Permalink
works now
Browse files Browse the repository at this point in the history
  • Loading branch information
Colin Ho authored and Colin Ho committed Aug 23, 2024
1 parent abfa672 commit 7ac9b5d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
1 change: 1 addition & 0 deletions daft/daft.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1787,6 +1787,7 @@ class PyDaftPlanningConfig:
def build_type() -> str: ...
def version() -> str: ...
def refresh_logger() -> None: ...
def test_logging() -> None: ...
def __getattr__(name) -> Any: ...
def io_glob(
path: str,
Expand Down
4 changes: 4 additions & 0 deletions daft/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import logging

from daft import refresh_logger


def setup_debug_logger(
exclude_prefix: list[str] = [],
Expand All @@ -18,3 +20,5 @@ def setup_debug_logger(
for prefix in exclude_prefix:
for handler in root_logger.handlers:
handler.addFilter(lambda record: not record.name.startswith(prefix))

refresh_logger()
26 changes: 16 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ fn should_enable_chrome_trace() -> bool {
#[cfg(feature = "python")]
pub mod pylib {
use common_tracing::init_tracing;
use lazy_static::lazy_static;
use pyo3::prelude::*;
static LOG_RESET_HANDLE: std::sync::OnceLock<pyo3_log::ResetHandle> =
std::sync::OnceLock::new();
lazy_static! {
static ref LOG_RESET_HANDLE: pyo3_log::ResetHandle = pyo3_log::init();
}

#[pyfunction]
pub fn version() -> &'static str {
Expand All @@ -61,6 +63,14 @@ pub mod pylib {
daft_core::DAFT_BUILD_TYPE
}

#[pyfunction]
pub fn test_logging() {
log::debug!("DEBUG from rust");
log::info!("INFO from rust");
log::warn!("WARN from rust");
log::error!("ERROR from rust");
}

#[pyfunction]
pub fn refresh_logger(py: Python) -> PyResult<()> {
use log::LevelFilter;
Expand All @@ -75,19 +85,14 @@ pub mod pylib {
// https://docs.python.org/3/library/logging.html#logging-levels
let level_filter = match python_log_level {
0 => LevelFilter::Off,
1..=5 => LevelFilter::Trace,
6..=10 => LevelFilter::Debug,
1..=10 => LevelFilter::Debug,
11..=20 => LevelFilter::Info,
21..=30 => LevelFilter::Warn,
_ => LevelFilter::Error,
};

let install_handle = || {
let logger = pyo3_log::Logger::default().filter(level_filter);
logger.install().unwrap()
};

LOG_RESET_HANDLE.get_or_init(install_handle).reset();
LOG_RESET_HANDLE.reset();
log::set_max_level(level_filter);
Ok(())
}

Expand Down Expand Up @@ -117,6 +122,7 @@ pub mod pylib {
m.add_wrapped(wrap_pyfunction!(version))?;
m.add_wrapped(wrap_pyfunction!(build_type))?;
m.add_wrapped(wrap_pyfunction!(refresh_logger))?;
m.add_wrapped(wrap_pyfunction!(test_logging))?;
Ok(())
}
}

0 comments on commit 7ac9b5d

Please sign in to comment.