-
Notifications
You must be signed in to change notification settings - Fork 38
Set up configurable FFI/Rust driver logging #799
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
base: master
Are you sure you want to change the base?
Conversation
1ca3660 to
af687f1
Compare
3801cca to
a0f10e2
Compare
| let clib_level = if let Ok(typedb_driver_clib_log) = std::env::var("TYPEDB_DRIVER_CLIB_LOG") { | ||
| typedb_driver_clib_log | ||
| } else { | ||
| "info".to_owned() | ||
| }; | ||
| // Try to get log level from TYPEDB_DRIVER_LOG first | ||
| let env_filter = if let Ok(typedb_log_level) = std::env::var("TYPEDB_DRIVER_LOG") { | ||
| EnvFilter::new(&format!("typedb_driver={},typedb_driver_clib={}", typedb_log_level, clib_level)) | ||
| } else if let Ok(rust_log) = std::env::var("RUST_LOG") { | ||
| // If RUST_LOG is set, use it but scope it to typedb_driver only | ||
| EnvFilter::new(&format!("typedb_driver={},typedb_driver_clib={}", rust_log, clib_level)) | ||
| } else { | ||
| EnvFilter::new(&format!("typedb_driver=info,typedb_driver_clib={}", clib_level)) | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be made simpler.
| let clib_level = if let Ok(typedb_driver_clib_log) = std::env::var("TYPEDB_DRIVER_CLIB_LOG") { | |
| typedb_driver_clib_log | |
| } else { | |
| "info".to_owned() | |
| }; | |
| // Try to get log level from TYPEDB_DRIVER_LOG first | |
| let env_filter = if let Ok(typedb_log_level) = std::env::var("TYPEDB_DRIVER_LOG") { | |
| EnvFilter::new(&format!("typedb_driver={},typedb_driver_clib={}", typedb_log_level, clib_level)) | |
| } else if let Ok(rust_log) = std::env::var("RUST_LOG") { | |
| // If RUST_LOG is set, use it but scope it to typedb_driver only | |
| EnvFilter::new(&format!("typedb_driver={},typedb_driver_clib={}", rust_log, clib_level)) | |
| } else { | |
| EnvFilter::new(&format!("typedb_driver=info,typedb_driver_clib={}", clib_level)) | |
| }; | |
| let clib_level = std::env::var("TYPEDB_DRIVER_CLIB_LOG").unwrap_or_else(|_| "info".to_owned()); | |
| let typedb_log_level = std::env::var("TYPEDB_DRIVER_LOG") | |
| .or_else(|_| std::env::var("RUST_LOG")) | |
| .unwrap_or_else(|_| "info".to_owned()); | |
| let env_filter = EnvFilter::new(&format!("typedb_driver={},typedb_driver_clib={}", typedb_log_level, clib_level)); |
| } else if let Ok(rust_log) = std::env::var("RUST_LOG") { | ||
| // If RUST_LOG is set, use it but scope it to typedb_driver only | ||
| EnvFilter::new(&format!("typedb_driver={},typedb_driver_clib={}", rust_log, clib_level)) | ||
| } else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if RUST_LOG is set to e.g. tonic=trace?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should look for TYPEDB_DRIVER_CLIB_LOG_LEVEL and TYPEDB_DRIVER_LOG_LEVEL. We can then use RUST_LOG to initialize the EnvFilter and add the two log levels (if set) as directives. If nothing is set, then we can fall back to info on both our libs.
This way, RUST_LOG=tonic=info,typedb_driver=error will work as expected and will not get overridden to typedb_driver=info by us unnecessarily.
| if let Err(e) = | ||
| tracing_subscriber::registry().with(env_filter).with(tracing_fmt::layer().with_target(false)).try_init() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why registry and not Subscriber::buiilder?
Usage and product changes
Rather than having Python/Java/other wrappers invoke logging initialization explicitly, we now just do it on open of the Rust driver automatically.
This should fix warnings in Python:
We also remove uses of the
logcreate in the//clayer, and usetracinginstead.We can now configure logging in the Rust-driver component with the following variables:
TYPEDB_DRIVER_LOG=info|debug|warn|traceor using the usualRUST_LOGvariable. By default, ffi-based drivers callinginit_loggingwill use INFO logging.Rust applications should continue set up their own logging subscriber, as usual.
Implementation
Remove C-layer static logging initialization, and rely on just the Rust driver's once lock that is execute on driver open.