Skip to content
Open
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
1 change: 1 addition & 0 deletions foundations/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ tokio-runtime-metrics = [
# Enables logging functionality.
logging = [
"ratelimit",
"dep:libc",
"dep:parking_lot",
"dep:slog-async",
"dep:slog-json",
Expand Down
60 changes: 52 additions & 8 deletions foundations/src/telemetry/log/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use super::field_dedup::FieldDedupFilterFactory;
use super::field_filtering::{FieldFilteringDrain, FilterFactory};
use super::field_redact::FieldRedactFilterFactory;
use super::internal::{LoggerWithKvNestingTracking, SharedLog};
#[cfg(target_os = "linux")]
use super::journal::{JournalStreams, JournalWriter, stdio_fd};

#[cfg(feature = "metrics")]
use crate::telemetry::log::log_volume::LogVolumeMetricsDrain;
Expand All @@ -23,6 +25,7 @@ use std::fmt::Debug;
use std::fs::File;
use std::io;
use std::io::BufWriter;
use std::mem::ManuallyDrop;
use std::sync::{Arc, LazyLock, OnceLock};

type BoxedDebug = Box<dyn Debug>;
Expand Down Expand Up @@ -90,7 +93,27 @@ pub(crate) fn init(
// NOTE: OXY-178, default is 128 (https://docs.rs/slog-async/2.7.0/src/slog_async/lib.rs.html#251)
const CHANNEL_SIZE: usize = 1024;

// Before building any drain, so that stderr is covered when logs go elsewhere too.
#[cfg(target_os = "linux")]
let journal = JournalStreams::detect(!settings.allow_blocking_journal_stdio);

let (async_drain, async_guard) = match (&settings.output, &settings.format) {
#[cfg(target_os = "linux")]
(output @ (LogOutput::Terminal | LogOutput::Stderr), LogFormat::Text)
if journal.includes(output) =>
{
let drain = JournalWriter::drain(stdio_fd(output), |writer| {
TextDrain::new(PlainDecorator::new(writer)).build()
});
build_async_drain(drain, settings, CHANNEL_SIZE)
}
#[cfg(target_os = "linux")]
(output @ (LogOutput::Terminal | LogOutput::Stderr), LogFormat::Json)
if journal.includes(output) =>
{
let drain = JournalWriter::drain(stdio_fd(output), build_json_log_drain);
build_async_drain(drain, settings, CHANNEL_SIZE)
}
(output @ (LogOutput::Terminal | LogOutput::Stderr), LogFormat::Text) => {
let decorator = if matches!(output, LogOutput::Terminal) {
TermDecorator::new().stdout().build()
Expand Down Expand Up @@ -154,48 +177,69 @@ pub(crate) fn init(
Ok(Some(async_guard))
}

/// Writes to stdout or stderr through a [`File`] that never closes them.
///
/// The log drain is dropped when the logger shuts down. Closing the descriptor then would
/// let the next file or socket opened take its number, and later writes to stdout or stderr,
/// such as a panic message, would go there.
struct StdioWriter(ManuallyDrop<File>);

impl io::Write for StdioWriter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.0.write(buf)
}

fn write_vectored(&mut self, bufs: &[io::IoSlice<'_>]) -> io::Result<usize> {
self.0.write_vectored(bufs)
}

fn flush(&mut self) -> io::Result<()> {
self.0.flush()
}
}

/// Opens fd 1 directly and wraps with a [`BufWriter`] with [`BUF_SIZE`] capacity.
///
/// [`io::Stdout`] uses a [`io::LineWriter`] which may cause unnecessary flushing.
#[cfg(not(target_family = "windows"))]
fn stdout_writer_without_line_buffering() -> BufWriter<File> {
fn stdout_writer_without_line_buffering() -> BufWriter<StdioWriter> {
use std::os::fd::{AsRawFd, FromRawFd};

let stdout = std::io::stdout();
let stdout = unsafe { File::from_raw_fd(stdout.as_raw_fd()) };
BufWriter::with_capacity(BUF_SIZE, stdout)
BufWriter::with_capacity(BUF_SIZE, StdioWriter(ManuallyDrop::new(stdout)))
}

/// Opens fd 1 directly and wraps with a [`BufWriter`] with [`BUF_SIZE`] capacity.
///
/// [`io::Stdout`] uses a [`io::LineWriter`] which may cause unnecessary flushing.
#[cfg(target_family = "windows")]
fn stdout_writer_without_line_buffering() -> BufWriter<File> {
fn stdout_writer_without_line_buffering() -> BufWriter<StdioWriter> {
use std::os::windows::io::{AsRawHandle, FromRawHandle};

let stdout = std::io::stdout();
let stdout = unsafe { File::from_raw_handle(stdout.as_raw_handle()) };
BufWriter::with_capacity(BUF_SIZE, stdout)
BufWriter::with_capacity(BUF_SIZE, StdioWriter(ManuallyDrop::new(stdout)))
}

/// Opens fd 2 directly and wraps with a [`BufWriter`] with [`BUF_SIZE`] capacity.
#[cfg(not(target_family = "windows"))]
fn stderr_writer_without_line_buffering() -> BufWriter<File> {
fn stderr_writer_without_line_buffering() -> BufWriter<StdioWriter> {
use std::os::fd::{AsRawFd, FromRawFd};

let stderr = std::io::stderr();
let stderr = unsafe { File::from_raw_fd(stderr.as_raw_fd()) };
BufWriter::with_capacity(BUF_SIZE, stderr)
BufWriter::with_capacity(BUF_SIZE, StdioWriter(ManuallyDrop::new(stderr)))
}

/// Opens fd 2 directly and wraps with a [`BufWriter`] with [`BUF_SIZE`] capacity.
#[cfg(target_family = "windows")]
fn stderr_writer_without_line_buffering() -> BufWriter<File> {
fn stderr_writer_without_line_buffering() -> BufWriter<StdioWriter> {
use std::os::windows::io::{AsRawHandle, FromRawHandle};

let stderr = std::io::stderr();
let stderr = unsafe { File::from_raw_handle(stderr.as_raw_handle()) };
BufWriter::with_capacity(BUF_SIZE, stderr)
BufWriter::with_capacity(BUF_SIZE, StdioWriter(ManuallyDrop::new(stderr)))
}

pub(crate) fn wrap_root_drain<D>(settings: &LoggingSettings, drain: D) -> SharedDrain
Expand Down
Loading
Loading