feat: add logger env

This commit is contained in:
2025-07-07 18:03:53 +03:00
parent 98ad8efd3a
commit be4cc21d56
4 changed files with 195 additions and 19 deletions

View File

@@ -1,8 +1,13 @@
use crate::app::{action::Action, App};
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use tracing::{event, info_span, Level};
/// Handles the key events of [`App`].
#[tracing::instrument]
pub fn get_action(key_event: KeyEvent) -> Option<Action> {
let span = info_span!("get_action");
let _enter = span.enter();
event!(Level::INFO, "handling key event: {:?}", key_event);
match key_event.code {
// Exit application on `ESC` or `q`
KeyCode::Esc | KeyCode::Char('q') => Some(Action::Quit),
@@ -30,7 +35,11 @@ pub fn get_action(key_event: KeyEvent) -> Option<Action> {
}
/// Handles the updates of [`App`].
#[tracing::instrument]
pub async fn update(app: &mut App<'_>, action: Action) -> anyhow::Result<()> {
let span = info_span!("update");
let _enter = span.enter();
event!(Level::INFO, "updating app with action: {:?}", action);
match action {
Action::Quit => app.quit(),
Action::NextTab => app.next_tab(),

View File

@@ -1,17 +1,19 @@
use std::{fs::File, path::PathBuf, str::FromStr};
use anyhow::Result;
use tracing::Level;
use tracing_subscriber::fmt;
use tracing_appender::rolling;
use tracing_subscriber::{self, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};
pub fn setup_logger() -> Result<()> {
std::fs::create_dir_all(".logs")?;
let path = PathBuf::from_str(".logs/traxor.log")?;
let log_file = File::create(path)?;
let subscriber = fmt::Subscriber::builder()
.with_max_level(Level::TRACE)
.with_writer(log_file)
.finish();
tracing::subscriber::set_global_default(subscriber)?;
let logfile = rolling::daily(".logs", "traxor.log");
let log_layer = tracing_subscriber::fmt::layer()
.with_writer(logfile)
.with_ansi(false);
tracing_subscriber::registry()
.with(log_layer)
.with(EnvFilter::from_default_env())
.init();
Ok(())
}