Skip to content

Commit

Permalink
Remove ansi formatting from server log file (#1505)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmodzelewski authored Feb 11, 2025
1 parent a9dec8e commit 19db871
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 28 deletions.
21 changes: 1 addition & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
| serde_with |Custom de/serialization functions for Rust's serde | MIT OR Apache-2.0 | https://github.com/jonasbb/serde_with/ |
| serial_test |Allows for the creation of serialised Rust tests | MIT | https://github.com/palfrey/serial_test/ |
| static-toml |Effortlessly embed TOML files into your Rust code as static data with custom data structures. | MIT | https://github.com/cptpiepmatz/static-toml |
| strip-ansi-escapes |Strip ANSI escape sequences from byte streams. | Apache-2.0/MIT | https://github.com/luser/strip-ansi-escapes |
| strum |Helpful macros for working with enums and strings | MIT | https://github.com/Peternator7/strum |
| sysinfo |Library to get system information such as processes, CPUs, disks, components and networks | MIT | https://github.com/GuillaumeGomez/sysinfo |
| tempfile |A library for managing temporary files and directories. | MIT OR Apache-2.0 | https://github.com/Stebalien/tempfile |
Expand Down
5 changes: 2 additions & 3 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "server"
version = "0.4.155"
version = "0.4.156"
edition = "2021"
build = "src/build.rs"
license = "Apache-2.0"
Expand Down Expand Up @@ -77,7 +77,6 @@ serde = { version = "1.0.217", features = ["derive", "rc"] }
serde_json = "1.0.138"
serde_with = { version = "3.12.0", features = ["base64", "macros"] }
static-toml = "1.3.0"
strip-ansi-escapes = "0.2.1"
strum = { version = "0.26.3", features = ["derive"] }
sysinfo = "0.33.1"
tempfile = "3.16"
Expand Down Expand Up @@ -119,7 +118,7 @@ path = "src/main.rs"
# This is a workaround for cargo-udeps to ignore these dependencies
# in case if feature 'tokio-console' is enabled.
[package.metadata.cargo-udeps.ignore]
normal = ["tracing-appender", "strip-ansi-escapes"]
normal = ["tracing-appender"]

[package.metadata.cargo-machete]
ignored = ["rust-s3"]
28 changes: 24 additions & 4 deletions server/src/log/logger.rs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ use std::sync::{Arc, Mutex};
use tracing::{event, info, trace, Level};
use tracing_appender::non_blocking::WorkerGuard;
use tracing_opentelemetry::OpenTelemetryLayer;
use tracing_subscriber::field::{RecordFields, VisitOutput};
use tracing_subscriber::fmt::format::DefaultVisitor;
use tracing_subscriber::fmt::FormatFields;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::{
Expand Down Expand Up @@ -71,8 +74,7 @@ impl EarlyLogDumper for Logging {
fn dump_to_file<W: Write>(&self, writer: &mut W) {
let early_logs_buffer = self.early_logs_buffer.lock().unwrap();
for log in early_logs_buffer.iter() {
let log = strip_ansi_escapes::strip(log);
writer.write_all(&log).unwrap();
writer.write_all(log.as_bytes()).unwrap();
}
}

Expand Down Expand Up @@ -146,7 +148,7 @@ impl Logging {
.event_format(Self::get_log_format())
.with_target(true)
.with_writer(VecStringMakeWriter(self.early_logs_buffer.clone()))
.with_ansi(true);
.with_ansi(false);
let (file_layer, file_layer_reload_handle) = reload::Layer::new(file_layer.boxed());
self.file_reload_handle = Some(file_layer_reload_handle);
layers.push(file_layer.and_then(filtering_file_layer));
Expand Down Expand Up @@ -268,10 +270,11 @@ impl Logging {
.expect("Failed to modify file filtering layer");

// Initialize non-blocking stdout layer
let (_, stdout_guard) = tracing_appender::non_blocking(std::io::stdout());
let (non_blocking_stdout, stdout_guard) = tracing_appender::non_blocking(std::io::stdout());
let stdout_layer = fmt::Layer::default()
.with_ansi(true)
.event_format(Self::get_log_format())
.with_writer(non_blocking_stdout)
.boxed();
self.stdout_guard = Some(stdout_guard);

Expand All @@ -298,6 +301,7 @@ impl Logging {
.with_target(true)
.with_writer(non_blocking_file)
.with_ansi(false)
.fmt_fields(NoAnsiFields {})
.boxed();

self.file_guard = Some(file_guard);
Expand Down Expand Up @@ -383,3 +387,19 @@ impl Default for Logging {
Self::new(TelemetryConfig::default())
}
}

// This is a workaround for a bug with `with_ansi` setting in tracing
// Bug thread: https://github.com/tokio-rs/tracing/issues/3116
struct NoAnsiFields {}

impl<'writer> FormatFields<'writer> for NoAnsiFields {
fn format_fields<R: RecordFields>(
&self,
writer: fmt::format::Writer<'writer>,
fields: R,
) -> std::fmt::Result {
let mut a = DefaultVisitor::new(writer, true);
fields.record(&mut a);
a.finish()
}
}

0 comments on commit 19db871

Please sign in to comment.