refactor(log): Use custom Time for timestamps
This commit is contained in:
1 file changed
+4
-18
+4
-18
@@ -1,28 +1,13 @@
|
|||||||
use std::time::SystemTime;
|
|
||||||
|
|
||||||
use log::{Level, LevelFilter, Metadata, Record, SetLoggerError};
|
use log::{Level, LevelFilter, Metadata, Record, SetLoggerError};
|
||||||
use time::format_description::FormatItem;
|
use rustix::thread::ClockId;
|
||||||
use time::formatting::Formattable;
|
|
||||||
use time::macros::format_description;
|
|
||||||
use time::OffsetDateTime;
|
|
||||||
|
|
||||||
const LOG_DATETIME_FORMAT: &[FormatItem] = format_description!(
|
use crate::time::Time;
|
||||||
"[year]-[month]-[day] [hour]:[minute]:[second] [offset_hour \
|
|
||||||
sign:mandatory]:[offset_minute]:[offset_second]"
|
|
||||||
);
|
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
pub struct SimpleLogger {
|
pub struct SimpleLogger {
|
||||||
pub level: Level,
|
pub level: Level,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn time_format<T>(dt: T, format: &(impl Formattable + ?Sized)) -> String
|
|
||||||
where
|
|
||||||
T: Into<OffsetDateTime>,
|
|
||||||
{
|
|
||||||
dt.into().format(format).unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
impl log::Log for SimpleLogger {
|
impl log::Log for SimpleLogger {
|
||||||
fn enabled(&self, metadata: &Metadata) -> bool {
|
fn enabled(&self, metadata: &Metadata) -> bool {
|
||||||
metadata.level() <= self.level
|
metadata.level() <= self.level
|
||||||
@@ -30,9 +15,10 @@ impl log::Log for SimpleLogger {
|
|||||||
|
|
||||||
fn log(&self, record: &Record) {
|
fn log(&self, record: &Record) {
|
||||||
if self.enabled(record.metadata()) {
|
if self.enabled(record.metadata()) {
|
||||||
|
let now = Time::new(ClockId::Realtime);
|
||||||
println!(
|
println!(
|
||||||
"{} {:5} {}:{}: {}",
|
"{} {:5} {}:{}: {}",
|
||||||
time_format(SystemTime::now(), &LOG_DATETIME_FORMAT),
|
now.as_secs_f64(),
|
||||||
record.level(),
|
record.level(),
|
||||||
record.file().unwrap(),
|
record.file().unwrap(),
|
||||||
record.line().unwrap(),
|
record.line().unwrap(),
|
||||||
|
|||||||
Reference in new issue
Block a user