Skip to content

[sdk/rust] Fix init_logger failing when RUST_LOG is not set#343

Merged
k82cn merged 1 commit into
xflops:mainfrom
k82cn:fix-init-logger-env
Feb 6, 2026
Merged

[sdk/rust] Fix init_logger failing when RUST_LOG is not set#343
k82cn merged 1 commit into
xflops:mainfrom
k82cn:fix-init-logger-env

Conversation

@k82cn
Copy link
Copy Markdown
Contributor

@k82cn k82cn commented Feb 6, 2026

Summary

  • Fix flmping and other CLI tools failing with InvalidConfig("environment variable not found") when RUST_LOG is not set
  • Use from_default_env() instead of try_from_default_env() to provide a default filter when RUST_LOG environment variable is missing

Root Cause

EnvFilter::try_from_default_env() returns FromEnvError when RUST_LOG is not set, which was being converted to InvalidConfig error via the From<FromEnvError> impl, causing confusing error messages.

Testing

# Before: fails without RUST_LOG
$ ./target/debug/flmping
Error: InvalidConfig("environment variable not found")

# After: works without RUST_LOG
$ ./target/debug/flmping
Session <flmping-nGVmVA> was created in <9 ms>, start to run <10> tasks in the session:
...

Use from_default_env() instead of try_from_default_env() to provide
a default filter when RUST_LOG environment variable is missing,
rather than returning an error.
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @k82cn, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the robustness of CLI tools by preventing crashes related to missing environment variables during logging initialization. By adjusting the logging configuration to gracefully handle the absence of the RUST_LOG environment variable, it ensures a smoother user experience and avoids confusing error messages.

Highlights

  • CLI Tool Robustness: Fixes flmping and other CLI tools from failing with InvalidConfig("environment variable not found") when the RUST_LOG environment variable is not set.
  • Logging Configuration: Changes the init_logger function to use tracing_subscriber::EnvFilter::from_default_env() instead of try_from_default_env(), providing a default filter when RUST_LOG is absent.
Changelog
  • sdk/rust/src/apis/mod.rs
    • Replaced tracing_subscriber::EnvFilter::try_from_default_env()? with tracing_subscriber::EnvFilter::from_default_env() in the init_logger function to prevent errors when RUST_LOG is not set.
Activity
  • The pull request was created by k82cn to address an issue where CLI tools failed if the RUST_LOG environment variable was not set.
  • The author provided a clear summary, root cause analysis, and testing steps in the PR description.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly fixes a crash in init_logger that occurred when the RUST_LOG environment variable was not set. The change from try_from_default_env to from_default_env is appropriate. However, I've raised a more significant architectural concern regarding the init_logger function. As a public function within an SDK, it should not initialize a global logger, as this can lead to conflicts and panics in applications that use this SDK and have their own logging configuration. I have recommended refactoring this function to provide logging components (like a tracing::Layer) that the consuming application can integrate into its own logging setup, making the SDK more robust and idiomatic for a library.

Comment thread sdk/rust/src/apis/mod.rs

pub fn init_logger() -> Result<(), FlameError> {
let filter = tracing_subscriber::EnvFilter::try_from_default_env()?
let filter = tracing_subscriber::EnvFilter::from_default_env()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

While this change correctly fixes the crash when RUST_LOG is not set, the init_logger function itself has a design issue that's common for libraries. It initializes a global logger by calling .init() (on line 231), which can conflict with an application's own logging setup and cause a panic. A library should typically not initialize a global logger.

Instead of initializing the logger directly, consider refactoring init_logger to return a tracing::Layer and a filter. The application using the SDK can then compose these with its own logging configuration. This makes the SDK more flexible and prevents runtime panics in applications that also configure logging.

For example:

use tracing_subscriber::{EnvFilter, Layer, layer::SubscriberExt};
use tracing::Subscriber;

// This function would return a layer that can be composed by the application.
pub fn flame_layer<S>() -> impl Layer<S>
where
    S: Subscriber + for<'a> tracing_subscriber::registry::LookupSpan<'a>,
{
    let time_format = LocalTime::new(format_description!(
        "[hour repr:24]:[minute]:[second]::[subsecond digits:3]"
    ));

    tracing_subscriber::fmt::layer()
        .with_timer(time_format)
        .with_target(true)
        .with_ansi(false)
}

// This function would return the filter.
pub fn flame_filter() -> Result<EnvFilter, FlameError> {
    Ok(tracing_subscriber::EnvFilter::from_default_env()
        .add_directive("h2=error".parse()?)
        .add_directive("hyper_util=error".parse()?)
        .add_directive("tower=error".parse()?))
}

The application using the SDK would then be responsible for initializing the logger:

// In application code (e.g., main.rs)
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

fn main() -> Result<(), flame::apis::FlameError> {
    let filter = flame::apis::flame_filter()?;
    let layer = flame::apis::flame_layer();

    tracing_subscriber::registry()
        .with(filter)
        .with(layer)
        .init();
    
    // ... rest of the app
    Ok(())
}

This change would make the SDK more robust and easier to integrate into other applications.

References
  1. In a library, avoid modifying the global logging configuration for dependencies. This can cause conflicts with the application's own logging setup. Instead, document how users can configure logging or provide an opt-in utility (like a context manager) for temporary log level adjustments.

@k82cn k82cn merged commit 9fde599 into xflops:main Feb 6, 2026
5 checks passed
@k82cn k82cn deleted the fix-init-logger-env branch February 6, 2026 04:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant