Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding logging to hf_xet #24

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions hf_xet/Cargo.lock

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

2 changes: 2 additions & 0 deletions hf_xet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ pyo3 = { version = "0.20.2", features = [
data = { path = "../data" }
tokio = { version = "1.36", features = ["full"] }
parutils = { path = "../parutils" }
tracing = "0.1.*"
tracing-subscriber = { version = "0.3", features = ["tracing-log"] }

2 changes: 2 additions & 0 deletions hf_xet/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod data_client;
mod config;
mod log;

use pyo3::{pyfunction, PyResult};
use pyo3::exceptions::PyException;
Expand Down Expand Up @@ -82,6 +83,7 @@ impl PyPointerFile {

#[pymodule]
pub fn hf_xet(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
log::initialize_logging();
m.add_function(wrap_pyfunction!(upload_files, m)?)?;
m.add_function(wrap_pyfunction!(download_files, m)?)?;
m.add_class::<PyPointerFile>()?;
Expand Down
22 changes: 22 additions & 0 deletions hf_xet/src/log.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};

/// Default log level for the library to use. Override using `RUST_LOG` env variable.
/// TODO: probably change default to warn or error before shipping.
const DEFAULT_LOG_LEVEL: &str = "info";

pub fn initialize_logging() {
// TODO: maybe have an env variable for writing to a log file instead of stderr
let fmt_layer = tracing_subscriber::fmt::layer()
.with_line_number(true)
.with_file(true)
.with_target(false)
.json();

let filter_layer = EnvFilter::try_from_default_env()
.or_else(|_| EnvFilter::try_new(DEFAULT_LOG_LEVEL))
.unwrap_or_default();
tracing_subscriber::registry()
.with(fmt_layer)
.with(filter_layer)
.init();
}