chiark / gitweb /
logging: Change output formats
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 24 Apr 2022 11:18:11 +0000 (12:18 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 24 Apr 2022 11:28:15 +0000 (12:28 +0100)
Format is like that from flexi_logger but with only the level
coloured.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
Cargo.lock
daemon/Cargo.toml
daemon/logging.rs [new file with mode: 0644]
daemon/main.rs

index cec429b99ddaaaecdabe94228d10ddd7e4a94d28..230f03543d9b069693b440021f517c9d3d8aa77f 100644 (file)
@@ -2767,6 +2767,7 @@ dependencies = [
  "actix-cors",
  "actix-files",
  "actix-web",
+ "ansi_term",
  "fehler",
  "futures",
  "mime",
index d1e21c7dc1ca1f2c74c1874a609eed0a71544ffe..c03d391b31f2a1344ad339eecbafb9b7915c01d9 100644 (file)
@@ -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 (file)
index 0000000..80596ee
--- /dev/null
@@ -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("<unnamed>"),
+         record.file().unwrap_or("<unnamed>"),
+         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
+);
index a73378f40880f8c07db0bb08ca6090a2d438b9d2..5c8b605c54a3ff768d6038bad1c7d3a43e3fc18b 100644 (file)
@@ -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);