Skip to content

Commit aef7502

Browse files
authored
Adding logging to hf_xet (#24)
1 parent 5df5ca2 commit aef7502

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

hf_xet/Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hf_xet/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@ pyo3 = { version = "0.20.2", features = [
1717
data = { path = "../data" }
1818
tokio = { version = "1.36", features = ["full"] }
1919
parutils = { path = "../parutils" }
20+
tracing = "0.1.*"
21+
tracing-subscriber = { version = "0.3", features = ["tracing-log"] }
2022

hf_xet/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod data_client;
22
mod config;
3+
mod log;
34

45
use pyo3::{pyfunction, PyResult};
56
use pyo3::exceptions::PyException;
@@ -82,6 +83,7 @@ impl PyPointerFile {
8283

8384
#[pymodule]
8485
pub fn hf_xet(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
86+
log::initialize_logging();
8587
m.add_function(wrap_pyfunction!(upload_files, m)?)?;
8688
m.add_function(wrap_pyfunction!(download_files, m)?)?;
8789
m.add_class::<PyPointerFile>()?;

hf_xet/src/log.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};
2+
3+
/// Default log level for the library to use. Override using `RUST_LOG` env variable.
4+
/// TODO: probably change default to warn or error before shipping.
5+
const DEFAULT_LOG_LEVEL: &str = "info";
6+
7+
pub fn initialize_logging() {
8+
// TODO: maybe have an env variable for writing to a log file instead of stderr
9+
let fmt_layer = tracing_subscriber::fmt::layer()
10+
.with_line_number(true)
11+
.with_file(true)
12+
.with_target(false)
13+
.json();
14+
15+
let filter_layer = EnvFilter::try_from_default_env()
16+
.or_else(|_| EnvFilter::try_new(DEFAULT_LOG_LEVEL))
17+
.unwrap_or_default();
18+
tracing_subscriber::registry()
19+
.with(fmt_layer)
20+
.with(filter_layer)
21+
.init();
22+
}

0 commit comments

Comments
 (0)