瀏覽代碼

Env logger feature (#3775)

* feat: Expose and configure tracing env-filter

* doc: Add env-filter doc part

* chore: Remove conditional import

* Move logger env-filter feature out of feature flag

* Update logger env filtering

* Add RUST_LOG to the process from the cli

* Typo

* fix clippy

* clippy
Henry 2 月之前
父節點
當前提交
93a557c322
共有 4 個文件被更改,包括 17 次插入4 次删除
  1. 5 0
      packages/cli/src/serve/handle.rs
  2. 4 1
      packages/logger/Cargo.toml
  3. 1 2
      packages/logger/README.md
  4. 7 1
      packages/logger/src/lib.rs

+ 5 - 0
packages/cli/src/serve/handle.rs

@@ -2,6 +2,7 @@ use crate::{AppBundle, DioxusCrate, Platform, Result};
 use anyhow::Context;
 use dioxus_cli_opt::process_file_to;
 use std::{
+    env,
     net::SocketAddr,
     path::{Path, PathBuf},
     process::Stdio,
@@ -112,6 +113,10 @@ impl AppHandle {
             envs.push((dioxus_cli_config::ASSET_ROOT_ENV, base_path.clone()));
         }
 
+        if let Some(env_filter) = env::var_os("RUST_LOG").and_then(|e| e.into_string().ok()) {
+            envs.push(("RUST_LOG", env_filter));
+        }
+
         // Launch the server if we were given an address to start it on, and the build includes a server. After we
         // start the server, consume its stdout/stderr.
         if let (Some(addr), Some(server)) = (start_fullstack_on_address, self.server_exe()) {

+ 4 - 1
packages/logger/Cargo.toml

@@ -15,7 +15,10 @@ categories = ["development-tools::debugging"]
 [dependencies]
 dioxus-cli-config = { workspace = true }
 tracing = { workspace = true }
-tracing-subscriber = { workspace = true, features = ["registry", "std"] }
+tracing-subscriber = { workspace = true, features = ["registry", "std", "env-filter"] }
+
+[features]
+default = []
 
 [target.'cfg(target_arch = "wasm32")'.dependencies]
 tracing-wasm = { workspace = true }

+ 1 - 2
packages/logger/README.md

@@ -64,9 +64,8 @@ fn App() -> Element {
     p { "hi" }
   }
 }
-
-
 ```
+For non-wasm targets, runtime filtering is based on the `RUST_LOG` environment variable. e.g. for `RUST_LOG=none,crateName=trace` only logs trace and above for `crateName` will be captured. See [here](https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html#directives) for syntax. For crates with `-` in the name, these need to be changed to `_` in `RUST_LOG`.
 
 ## Platform Support
 Dioxus logger will eventually support every target that Dioxus does. Currently mobile and TUI are not supported.

+ 7 - 1
packages/logger/src/lib.rs

@@ -97,7 +97,13 @@ pub fn init(level: Level) -> Result<(), SetGlobalDefaultError> {
 
     #[cfg(not(target_arch = "wasm32"))]
     {
-        let sub = tracing_subscriber::FmtSubscriber::builder().with_max_level(level);
+        let sub = tracing_subscriber::FmtSubscriber::builder()
+            .with_max_level(level)
+            .with_env_filter(
+                tracing_subscriber::EnvFilter::builder()
+                    .with_default_directive(level.into())
+                    .from_env_lossy(),
+            );
 
         if !dioxus_cli_config::is_cli_enabled() {
             return set_global_default(sub.finish());