Parcourir la source

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 il y a 2 mois
Parent
commit
93a557c322

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

@@ -2,6 +2,7 @@ use crate::{AppBundle, DioxusCrate, Platform, Result};
 use anyhow::Context;
 use anyhow::Context;
 use dioxus_cli_opt::process_file_to;
 use dioxus_cli_opt::process_file_to;
 use std::{
 use std::{
+    env,
     net::SocketAddr,
     net::SocketAddr,
     path::{Path, PathBuf},
     path::{Path, PathBuf},
     process::Stdio,
     process::Stdio,
@@ -112,6 +113,10 @@ impl AppHandle {
             envs.push((dioxus_cli_config::ASSET_ROOT_ENV, base_path.clone()));
             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
         // 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.
         // start the server, consume its stdout/stderr.
         if let (Some(addr), Some(server)) = (start_fullstack_on_address, self.server_exe()) {
         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]
 [dependencies]
 dioxus-cli-config = { workspace = true }
 dioxus-cli-config = { workspace = true }
 tracing = { 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]
 [target.'cfg(target_arch = "wasm32")'.dependencies]
 tracing-wasm = { workspace = true }
 tracing-wasm = { workspace = true }

+ 1 - 2
packages/logger/README.md

@@ -64,9 +64,8 @@ fn App() -> Element {
     p { "hi" }
     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
 ## Platform Support
 Dioxus logger will eventually support every target that Dioxus does. Currently mobile and TUI are not supported.
 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"))]
     #[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() {
         if !dioxus_cli_config::is_cli_enabled() {
             return set_global_default(sub.finish());
             return set_global_default(sub.finish());