Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using tui-logger with tracing #40

Closed
kdheepak opened this issue Jun 8, 2023 · 2 comments
Closed

using tui-logger with tracing #40

kdheepak opened this issue Jun 8, 2023 · 2 comments

Comments

@kdheepak
Copy link

kdheepak commented Jun 8, 2023

Thanks for putting together this package.

I'm not able to get this to work with tracing unfortunately. Do you have any idea what might be going on?

I have the following code in the main.rs:

use tracing_subscriber::{
  self, filter::EnvFilter, prelude::__tracing_subscriber_SubscriberExt, util::SubscriberInitExt, Layer,
};

fn main() {
  let directory = ...;
  let log_path = directory.join("debug.log");
  let log_file = std::fs::File::create(&log_path)?;
  let file_subscriber = tracing_subscriber::fmt::layer()
    .with_file(true)
    .with_line_number(true)
    .with_writer(log_file)
    .with_target(false)
    .with_ansi(false)
    .with_filter(EnvFilter::from_default_env());
  tracing_subscriber::registry().with(file_subscriber).with(tui_logger::tracing_subscriber_layer()).init();
  ...
}

And I'm using the following render function:

  fn render(f: &mut super::Frame<'_>, rect: tui::layout::Rect) {
    let state = TuiWidgetState::new().set_default_display_level(LevelFilter::Trace);
    let w = TuiLoggerWidget::default()
      .style_error(Style::default().fg(Color::Red))
      .style_debug(Style::default().fg(Color::Green))
      .style_warn(Style::default().fg(Color::Yellow))
      .style_trace(Style::default().fg(Color::Magenta))
      .style_info(Style::default().fg(Color::Cyan))
      .state(&state);
    f.render_widget(w, rect);
  }

I'm not getting any log output if I do this. If I use the SmartWidget, I get the borders but no logs. I'm getting logs in my file from tracing, just not in the TuiLoggerWidget.

Any idea what might be going on here?

@gin66
Copy link
Owner

gin66 commented Jun 8, 2023

The tracing feature is implemented by #31. If I read the code correctly, then the tracing messages are fed into the log system. So tui_logger needs to be initialized as normal. This means something along the lines of:

    // Set max_log_level to Trace
    tui_logger::init_logger(log::LevelFilter::Trace).unwrap();

    // Set default level for unknown targets to Trace
    tui_logger::set_default_level(log::LevelFilter::Trace);

@ahadnagy Could you please help?

Anyone can provide an example ?

@kdheepak
Copy link
Author

kdheepak commented Jun 8, 2023

Using this in the code throws an exception on unwrap():

    // Set max_log_level to Trace
    tui_logger::init_logger(log::LevelFilter::Trace).unwrap();

Here's the error:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: SetLoggerError(())', src\logging.rs:31:52        
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

However, if I only add this:

tui_logger::set_default_level(log::LevelFilter::Trace);

it seems to be working! Thanks!

@kdheepak kdheepak closed this as completed Jun 8, 2023
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

No branches or pull requests

2 participants