Skip to content
Open
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
36 changes: 1 addition & 35 deletions Cargo.lock

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

5 changes: 2 additions & 3 deletions c/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ rust_static_library(

"@crates//:chrono",
"@crates//:itertools",
"@crates//:env_logger",
"@crates//:tracing",
"@crates//:tracing-subscriber",
],
tags = ["crate-name=typedb_driver_clib"],
)
Expand All @@ -71,9 +71,8 @@ rust_shared_library(

"@crates//:chrono",
"@crates//:itertools",
"@crates//:env_logger",
"@crates//:log",
"@crates//:tracing",
"@crates//:tracing-subscriber",
],
tags = ["crate-name=typedb_driver_clib"],
)
Expand Down
11 changes: 3 additions & 8 deletions c/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,14 @@ features = {}

[dependencies]

[dependencies.env_logger]
features = ["auto-color", "color", "default", "humantime", "regex"]
version = "0.10.2"
default-features = false

[dependencies.tracing]
features = ["attributes", "default", "log", "std", "tracing-attributes"]
version = "0.1.41"
default-features = false

[dependencies.log]
features = ["kv", "kv_unstable", "std", "value-bag"]
version = "0.4.27"
[dependencies.tracing-subscriber]
features = ["alloc", "ansi", "default", "env-filter", "fmt", "matchers", "nu-ansi-term", "once_cell", "regex", "registry", "sharded-slab", "smallvec", "std", "thread_local", "tracing", "tracing-log"]
version = "0.3.19"
default-features = false

[dependencies.typedb-driver]
Expand Down
1 change: 0 additions & 1 deletion c/src/concept/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

use std::ptr::addr_of_mut;

use itertools::Itertools;
use typedb_driver::{answer::ConceptRow, concept::Concept, BoxPromise, Promise, Result};

use super::{iterator::iterator_try_next, memory::free};
Expand Down
39 changes: 34 additions & 5 deletions c/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ use std::{
sync::Arc,
};

use env_logger::Env;
use tracing::{debug, warn};
use tracing_subscriber::{fmt as tracing_fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};
use typedb_driver::{Error, Result};

use super::memory::{free, release_arc, release_optional, release_string};
Expand All @@ -35,12 +35,41 @@ thread_local! {
}

/// Enables logging in the TypeDB driver.
///
/// This function sets up tracing with the following priority:
/// 1. TYPEDB_DRIVER_LOG environment variable (if set). Use TYPEDB_DRIVER_CLIB_LOG to see memory exchanges
/// 2. RUST_LOG environment variable (if set)
/// 3. Default level (INFO)
///
/// The logging is initialized only once using a static flag to prevent
/// multiple initializations in applications that create multiple drivers.
#[no_mangle]
pub extern "C" fn init_logging() {
const ENV_VAR: &str = "TYPEDB_DRIVER_LOG_LEVEL";
if let Err(err) = env_logger::try_init_from_env(Env::new().filter(ENV_VAR)) {
warn!("{err}");
}
use std::sync::Once;
static INIT: Once = Once::new();
INIT.call_once(|| {
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 {
Comment on lines +59 to +62
Copy link
Member

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?

Copy link
Member

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.

EnvFilter::new(&format!("typedb_driver=info,typedb_driver_clib={}", clib_level))
};
Comment on lines +51 to +64
Copy link
Member

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.

Suggested change
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));


// Initialize the tracing subscriber
if let Err(e) =
tracing_subscriber::registry().with(env_filter).with(tracing_fmt::layer().with_target(false)).try_init()
Comment on lines +67 to +68
Copy link
Member

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?

{
eprintln!("Failed to initialize logging: {}", e);
}
});
}

fn ok_record<T>(result: Result<T>) -> Option<T> {
Expand Down
21 changes: 0 additions & 21 deletions docs/modules/ROOT/partials/rust/connection/TypeDBDriver.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,6 @@ Result
driver.force_close()
----

[#_struct_TypeDBDriver_init_logging_]
==== init_logging

[source,rust]
----
pub fn init_logging()
----

Initialize logging configuration for the TypeDB driver.

This function sets up tracing with the following priority:

The logging is initialized only once using a static flag to prevent multiple initializations in applications that create multiple drivers.

[caption=""]
.Returns
[source,rust]
----
null
----

[#_struct_TypeDBDriver_is_open_]
==== is_open

Expand Down
1 change: 0 additions & 1 deletion rust/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ typedb_driver_deps = [
"@crates//:futures",
"@crates//:http",
"@crates//:itertools",
"@crates//:log",
"@crates//:prost",
"@crates//:serde",
"@crates//:tokio",
Expand Down
41 changes: 0 additions & 41 deletions rust/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,45 +54,6 @@ impl TypeDBDriver {

pub const DEFAULT_ADDRESS: &'static str = "localhost:1729";

/// Initialize logging configuration for the TypeDB driver.
///
/// This function sets up tracing with the following priority:
/// 1. TYPEDB_DRIVER_LOG environment variable (if set). Use TYPEDB_DRIVER_CLIB_LOG to see memory exchanges
/// 1. environment variable (if set)
/// 2. RUST_LOG environment variable (if set)
/// 3. Default level (INFO)
///
/// The logging is initialized only once using a static flag to prevent
/// multiple initializations in applications that create multiple drivers.
pub fn init_logging() {
use std::sync::Once;
static INIT: Once = Once::new();

INIT.call_once(|| {
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))
};

// Initialize the tracing subscriber
if let Err(e) =
tracing_subscriber::registry().with(env_filter).with(tracing_fmt::layer().with_target(false)).try_init()
{
eprintln!("Failed to initialize logging: {}", e);
}
});
}

/// Creates a new TypeDB Server connection.
///
/// # Arguments
Expand Down Expand Up @@ -153,8 +114,6 @@ impl TypeDBDriver {
driver_options: DriverOptions,
driver_lang: impl AsRef<str>,
) -> Result<Self> {
Self::init_logging();

debug!("Initializing TypeDB driver with description: {}", driver_lang.as_ref());
let id = address.as_ref().to_string();
let address: Address = id.parse()?;
Expand Down