Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions foundations/src/telemetry/log/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,23 @@ pub(crate) fn init(service_info: &ServiceInfo, settings: &LoggingSettings) -> Bo
const CHANNEL_SIZE: usize = 1024;

let async_drain = match (&settings.output, &settings.format) {
(LogOutput::Terminal, LogFormat::Text) => {
let drain = TextDrain::new(TermDecorator::new().stdout().build())
.build()
.fuse();
(output @ (LogOutput::Terminal | LogOutput::Stderr), LogFormat::Text) => {
let decorator = if matches!(output, LogOutput::Terminal) {
TermDecorator::new().stdout().build()
} else {
TermDecorator::new().stderr().build()
};

let drain = TextDrain::new(decorator).build().fuse();
AsyncDrain::new(drain).chan_size(CHANNEL_SIZE).build()
}
(LogOutput::Terminal, LogFormat::Json) => {
let stdout_writer = stdout_writer_without_line_buffering();
let drain = build_json_log_drain(stdout_writer);
(output @ (LogOutput::Terminal | LogOutput::Stderr), LogFormat::Json) => {
let writer = if matches!(output, LogOutput::Terminal) {
stdout_writer_without_line_buffering()
} else {
stderr_writer_without_line_buffering()
};
let drain = build_json_log_drain(writer);
AsyncDrain::new(drain).chan_size(CHANNEL_SIZE).build()
}
(LogOutput::File(file), LogFormat::Text) => {
Expand Down Expand Up @@ -133,6 +141,12 @@ fn stdout_writer_without_line_buffering() -> BufWriter<File> {
BufWriter::with_capacity(BUF_SIZE, stdout)
}

/// Opens fd 2 directly and wraps with a [`BufWriter`] with [`BUF_SIZE`] capacity.
fn stderr_writer_without_line_buffering() -> BufWriter<File> {
let stderr = unsafe { File::from_raw_fd(2) };
BufWriter::with_capacity(BUF_SIZE, stderr)
}

fn get_root_drain(
_settings: &LoggingSettings,
base_drain: Arc<dyn SendSyncRefUnwindSafeDrain<Err = Never, Ok = ()> + 'static>,
Expand Down
2 changes: 2 additions & 0 deletions foundations/src/telemetry/settings/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ pub enum LogOutput {
/// Write log to terminal.
#[default]
Terminal,
/// Write log to [`std::io::Stderr`].
Stderr,
/// Write log to file with the specified path.
///
/// File will be created if it doesn't exist and overwritten otherwise.
Expand Down
Loading