use chrono::{DateTime, Utc};
use core::fmt;
use std::collections::HashMap;
use tracing::Level;
#[derive(Debug, Clone, PartialEq)]
pub enum LogValue {
String(String),
Int(i64),
Float(f64),
Bool(bool),
Debug(String),
}
impl fmt::Display for LogValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
LogValue::String(s) => write!(f, "{}", s),
LogValue::Int(i) => write!(f, "{}", i),
LogValue::Float(f_val) => write!(f, "{}", f_val),
LogValue::Bool(b) => write!(f, "{}", b),
LogValue::Debug(d) => write!(f, "{}", d),
}
}
}
#[derive(Debug, Clone)]
pub struct LogEvent {
pub timestamp: DateTime<Utc>,
pub level: Level,
pub target: String,
pub name: String,
pub message: Option<String>,
pub fields: HashMap<String, LogValue>,
pub span_id: Option<String>,
pub parent_id: Option<String>,
pub thread_id: Option<String>,
pub thread_name: Option<String>,
}
impl LogEvent {
pub fn new<S1, S2>(level: Level, target: S1, name: S2, message: Option<String>) -> Self
where
S1: Into<String>,
S2: Into<String>,
{
LogEvent {
timestamp: Utc::now(),
level,
target: target.into(),
name: name.into(),
message,
fields: HashMap::new(),
span_id: None,
parent_id: None,
thread_id: None,
thread_name: None,
}
}
}