Skip to content

Commit 216dde0

Browse files
committed
feat: use tracing for logs
- We protect against ever compiling log statements into release binaries
1 parent 92f9c10 commit 216dde0

5 files changed

Lines changed: 58 additions & 6 deletions

File tree

Cargo.lock

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

README.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Run Rust tests:
8181
cargo test
8282
```
8383

84-
### Linting and standards checks
84+
### Linting and Standards Checks
8585

8686
```bash
8787
npm run standards
@@ -109,6 +109,51 @@ fn main() {
109109
}
110110
```
111111

112+
### Tracing and Logging
113+
114+
This plugin and its connection manager crate use the
115+
[`tracing`](https://crates.io/crates/tracing) ecosystem for internal logging. They are
116+
configured with the `release_max_level_off` feature so that **all log statements are
117+
compiled out of release builds**. This guarantees that logging from this plugin will never
118+
reach production binaries unless you explicitly change that configuration.
119+
120+
To see logs during development, initialize a `tracing-subscriber` in your Tauri
121+
application crate and keep it behind a `debug_assertions` guard, for example:
122+
123+
```toml
124+
[dependencies]
125+
tracing = { version = "0.1.41", default-features = false, features = ["std", "release_max_level_off"] }
126+
tracing-subscriber = { version = "0.3.20", features = ["fmt", "env-filter"] }
127+
```
128+
129+
```rust
130+
#[cfg(debug_assertions)]
131+
fn init_tracing() {
132+
use tracing_subscriber::{fmt, EnvFilter};
133+
134+
let filter = EnvFilter::try_from_default_env()
135+
.unwrap_or_else(|_| EnvFilter::new("trace"));
136+
137+
fmt().with_env_filter(filter).compact().init();
138+
}
139+
140+
#[cfg(not(debug_assertions))]
141+
fn init_tracing() {}
142+
143+
fn main() {
144+
init_tracing();
145+
146+
tauri::Builder::default()
147+
.plugin(tauri_plugin_sqlite::init())
148+
.run(tauri::generate_context!())
149+
.expect("error while running tauri application");
150+
}
151+
```
152+
153+
With this setup, `tauri dev` shows all plugin and app logs, while `tauri build` produces
154+
a release binary that contains no logging from this plugin or your app-level `tracing`
155+
calls.
156+
112157
### JavaScript/TypeScript API
113158

114159
Install the JavaScript package in your frontend:

crates/sqlx-sqlite-conn-mgr/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ rust-version = "1.89"
1010
sqlx = { version = "0.8.6", features = ["runtime-tokio", "sqlite"] }
1111
thiserror = "2.0.17"
1212
tokio = { version = "1.48.0", features = ["full"] }
13+
tracing = { version = "0.1.41", default-features = false, features = ["std", "release_max_level_off"] }

crates/sqlx-sqlite-conn-mgr/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,14 @@ queries.
154154
* Idle timeout: 30 seconds by default (configurable via `custom_config`)
155155
* Only customize `SqliteDatabaseConfig` when defaults don't meet your needs
156156

157+
## Tracing and Logging
158+
159+
This crate uses the [`tracing`](https://crates.io/crates/tracing) ecosystem for internal
160+
instrumentation. It is built with the `release_max_level_off` feature so that all
161+
`tracing` log statements are compiled out of release builds. To see its logs during
162+
development, the host application must install a `tracing-subscriber` and enable the
163+
desired log level; no extra configuration is required in this crate.
164+
157165
## Error Handling
158166

159167
```rust

crates/sqlx-sqlite-conn-mgr/src/database.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use sqlx::{ConnectOptions, Pool, Sqlite};
1010
use std::path::{Path, PathBuf};
1111
use std::sync::Arc;
1212
use std::sync::atomic::{AtomicBool, Ordering};
13+
use tracing::error;
1314

1415
/// SQLite database with connection pooling for concurrent reads and optional exclusive writes.
1516
///
@@ -273,11 +274,7 @@ impl SqliteDatabase {
273274

274275
// Remove from registry
275276
if let Err(e) = uncache_database(&self.path).await {
276-
// TODO: Investigate use of "tracing" crate to log this error
277-
#[cfg(debug_assertions)]
278-
eprintln!("Failed to remove database from cache: {}", e);
279-
#[cfg(not(debug_assertions))]
280-
let _ = e; // Suppress unused variable warning
277+
error!("Failed to remove database from cache: {}", e);
281278
}
282279

283280
// This will await all readers to be returned

0 commit comments

Comments
 (0)