tui-logger

Crates.iotui-logger
lib.rstui-logger
version0.14.0
sourcesrc
created_at2018-05-19 08:31:45.940213
updated_at2024-11-21 18:10:33.709012
descriptionLogger with smart widget for the `ratatui` crate
homepage
repositoryhttps://github.com/gin66/tui-logger
max_upload_size
id66139
size8,957,455
(gin66)

documentation

https://docs.rs/tui-logger/latest/tui_logger/

README

tui-logger

Logger with smart widget for the tui and ratatui crate

dependency status Build examples

Demo of the widget

Demo

Documentation

Documentation

Important note for tui

The tui crate has been archived and ratatui has taken over. In order to avoid supporting compatibility for an inactive crate, the v0.9.x releases are the last to support tui. In case future bug fixes are needed, the branch tui_legacy has been created to track changes to 0.9.x releases.

Starting with v0.10 tui-logger is ratatui only.

Features

  • Logger implementation for the log crate
  • Logger enable/disable detection via hash table (avoid string compare)
  • Hot logger code only copies enabled log messages with timestamp into a circular buffer
  • Widgets/move_message() retrieve captured log messages from hot circular buffer
  • Lost message detection due to circular buffer
  • Log filtering performed on log record target
  • Simple Widgets to view logs and configure debuglevel per target
  • Logging of enabled logs to file
  • Scrollback in log history
  • Title of target and log pane can be configured
  • slog support, providing a Drain to integrate into your slog infrastructure
  • tracing support
  • Allow configuration of target dependent loglevel specifically for file logging
  • Avoid duplicating of target, module and filename in every log record
  • Simultaneous modification of all targets' display/hot logging loglevel by key command

Smart Widget

Smart widget consists of two widgets. Left is the target selector widget and on the right side the logging messages view scrolling up. The target selector widget can be hidden/shown during runtime via key command. The key command to be provided to the TuiLoggerWidget via transition() function.

The target selector widget looks like this:

widget

It controls:

  • Capturing of log messages by the logger
  • Selection of levels for display in the logging message view

The two columns have the following meaning:

  • Code EWIDT: E stands for Error, W for Warn, Info, Debug and Trace.
    • Inverted characters (EWIDT) are enabled log levels in the view
    • Normal characters show enabled capturing of a log level per target
    • If any of EWIDT are not shown, then the respective log level is not captured
  • Target of the log events can be defined in the log e.g. warn!(target: "demo", "Log message");

Smart Widget Key Commands

|  KEY     | ACTION
|----------|-----------------------------------------------------------|
| h        | Toggles target selector widget hidden/visible
| f        | Toggle focus on the selected target only
| UP       | Select previous target in target selector widget
| DOWN     | Select next target in target selector widget
| LEFT     | Reduce SHOWN (!) log messages by one level
| RIGHT    | Increase SHOWN (!) log messages by one level
| -        | Reduce CAPTURED (!) log messages by one level
| +        | Increase CAPTURED (!) log messages by one level
| PAGEUP   | Enter Page Mode and scroll approx. half page up in log history.
| PAGEDOWN | Only in page mode: scroll 10 events down in log history.
| ESCAPE   | Exit page mode and go back to scrolling mode
| SPACE    | Toggles hiding of targets, which have logfilter set to off

The mapping of key to action has to be done in the application. The respective TuiWidgetEvent has to be provided to TuiWidgetState::transition().

Remark to the page mode: The timestamp of the event at event history's bottom line is used as reference. This means, changing the filters in the EWIDT/focus from the target selector window should work as expected without jumps in the history. The page next/forward advances as per visibility of the events.

Basic usage to initialize logger-system:

#[macro_use]
extern crate log;
//use tui_logger;

fn main() {
    // Early initialization of the logger

    // 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);

    // code....
}

For use of the widget please check examples/demo.rs

Demo

Run demo using termion:

cargo run --example demo --features termion

Run demo with crossterm:

cargo run --example demo --features crossterm

slog support

tui-logger provides a [TuiSlogDrain] which implements slog::Drain and will route all records it receives to the tui-logger widget.

Enabled by feature "slog-support"

tracing-subscriber support

tui-logger provides a [TuiTracingSubscriberLayer] which implements tracing_subscriber::Layer and will collect all events it receives to the tui-logger widget

Enabled by feature "tracing-support"

Custom filtering

#[macro_use]
extern crate log;
//use tui_logger;
use env_logger;

fn main() {
    // Early initialization of the logger
    let drain = tui_logger::Drain::new();
    // instead of tui_logger::init_logger, we use `env_logger`
    env_logger::Builder::default()
        .format(move |buf, record|
            // patch the env-logger entry through our drain to the tui-logger
            Ok(drain.log(record))
        ).init(); // make this the global logger
    // code....
}

Internals

For logging there are two circular buffers in use:

  • "hot" buffer, which is written to during any logging macro invocation
  • main buffer, which holds events to be displayed by the widgets.

The size of the "hot" buffer is 1000 and can be modified by set_hot_buffer_depth(). The size of the main buffer is 10000 and can be modified by set_buffer_depth().

Reason for this scheme: The main buffer is locked for a while during widget updates. In order to avoid blocking the log-macros, this scheme is in use.

The copy from "hot" buffer to main buffer is performed by a call to move_events(), which is done in a cyclic task, which repeats every 10 ms, or when the hot buffer is half full.

In versions <0.13 log messages may have been lost, if the widget wasn't drawn.

THANKS TO

Star History

Star History Chart

License: MIT

Commit count: 260

cargo fmt