From: Ian Jackson Date: Sun, 24 Apr 2022 11:18:11 +0000 (+0100) Subject: logging: Change output formats X-Git-Tag: otter-1.1.0~467 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=0cd13b00142cb7ebca34d39b94ec4689c7b12866;p=otter.git logging: Change output formats Format is like that from flexi_logger but with only the level coloured. Signed-off-by: Ian Jackson --- diff --git a/Cargo.lock b/Cargo.lock index cec429b9..230f0354 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2767,6 +2767,7 @@ dependencies = [ "actix-cors", "actix-files", "actix-web", + "ansi_term", "fehler", "futures", "mime", diff --git a/daemon/Cargo.toml b/daemon/Cargo.toml index d1e21c7d..c03d391b 100644 --- a/daemon/Cargo.toml +++ b/daemon/Cargo.toml @@ -33,6 +33,7 @@ num-traits="0.2" serde_with="1" structopt="0.3" +ansi_term="0.12" mime = "0.3" actix-web = "4" actix-files = "0.6" diff --git a/daemon/logging.rs b/daemon/logging.rs new file mode 100644 index 00000000..80596ee0 --- /dev/null +++ b/daemon/logging.rs @@ -0,0 +1,39 @@ +// Copyright 2020-2021 Ian Jackson and contributors to Otter +// SPDX-License-Identifier: AGPL-3.0-or-later +// There is NO WARRANTY. + +use otter::prelude::*; + +use ansi_term::Style; +use flexi_logger::{style, TS_DASHES_BLANK_COLONS_DOT_BLANK}; +use flexi_logger::{AdaptiveFormat, DeferredNow, FormatFunction, Record}; + +#[throws(io::Error)] +fn generic_format(w: &mut dyn io::Write, + now: &mut DeferredNow, record: &Record, + style: Style) { + write!(w, "[{}] {} [{}] {}:{}: {}", + now.format(TS_DASHES_BLANK_COLONS_DOT_BLANK), + style.paint(record.level().to_string()), + record.module_path().unwrap_or(""), + record.file().unwrap_or(""), + record.line().unwrap_or(0), + &record.args())?; +} + +#[throws(io::Error)] +fn basic_format(w: &mut dyn std::io::Write, + now: &mut DeferredNow, record: &Record) { + generic_format(w, now, record, default())?; +} + +#[throws(io::Error)] +fn coloured_format(w: &mut dyn io::Write, + now: &mut DeferredNow, record: &Record) { + generic_format(w, now, record, style(record.level()))?; +} + +pub const BASIC_FORMAT: FormatFunction = basic_format; +pub const ADAPTIVE_FORMAT: AdaptiveFormat = AdaptiveFormat::Custom( + basic_format, coloured_format +); diff --git a/daemon/main.rs b/daemon/main.rs index a73378f4..5c8b605c 100644 --- a/daemon/main.rs +++ b/daemon/main.rs @@ -15,6 +15,7 @@ pub mod imports; pub mod api; pub mod cmdlistener; +pub mod logging; pub mod session; pub mod sse; @@ -487,7 +488,11 @@ async fn main() -> Result<(),StartupError> { let c = config(); - flexi_logger::Logger::with(log_config()).start()?; + flexi_logger::Logger::with(log_config()) + .format(logging::BASIC_FORMAT) + .adaptive_format_for_stderr(logging::ADAPTIVE_FORMAT) + .adaptive_format_for_stdout(logging::ADAPTIVE_FORMAT) + .start()?; debug!("resolved config: {:#?}", c);