Skip to content

Commit

Permalink
Send service logs to stderr (#23991)
Browse files Browse the repository at this point in the history
Logs to stderr is more idiomatic.

GitOrigin-RevId: acc38a6b0d10ee153db8f4851807ee2cb39bcb22
  • Loading branch information
nipunn1313 authored and Convex, Inc. committed Mar 26, 2024
1 parent 32e5119 commit a62ecaa
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
50 changes: 29 additions & 21 deletions crates/cmd_util/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ use std::{
env,
fmt::Debug,
fs::File,
io,
str::FromStr,
sync::LazyLock,
};

use tracing::Level;
use tracing_subscriber::{
fmt::format::format,
fmt::{
format::format,
MakeWriter,
},
layer::SubscriberExt,
util::SubscriberInitExt,
EnvFilter,
Expand Down Expand Up @@ -64,34 +68,38 @@ pub struct TracingGuard {
_guard: Option<tracing_appender::non_blocking::WorkerGuard>,
}

/// Call this from scripts and services at startup.
/// Call this from scripts at startup.
pub fn config_tool() -> TracingGuard {
config_tracing(io::stderr, Level::ERROR)
}

/// Call this from services at startup.
pub fn config_service() -> TracingGuard {
config_tracing(io::stdout, Level::INFO)
}

fn config_tracing<W>(writer: W, level: Level) -> TracingGuard
where
W: Send + Sync + for<'writer> MakeWriter<'writer> + 'static,
{
let mut layers = Vec::new();
let color_disabled = std::env::var("NO_COLOR").is_ok();
let format_layer = tracing_subscriber::fmt::layer()
.with_ansi(!color_disabled)
.with_writer(writer);
let format_layer = match std::env::var("LOG_FORMAT") {
Ok(s) if s == "json" => tracing_subscriber::fmt::layer()
.event_format(format().json())
.with_ansi(!color_disabled)
.boxed(),
Ok(s) if s == "compact" => tracing_subscriber::fmt::layer()
.event_format(format().compact())
.with_ansi(!color_disabled)
.boxed(),
Ok(s) if s == "pretty" => tracing_subscriber::fmt::layer()
.event_format(format().pretty())
.with_ansi(!color_disabled)
.boxed(),
_ => tracing_subscriber::fmt::layer()
.event_format(format().compact())
.with_ansi(!color_disabled)
.boxed(),
Ok(s) if s == "json" => format_layer.event_format(format().json()).boxed(),
Ok(s) if s == "compact" => format_layer.event_format(format().compact()).boxed(),
Ok(s) if s == "pretty" => format_layer.event_format(format().pretty()).boxed(),
_ => format_layer.event_format(format().compact()).boxed(),
};
let stdout = format_layer
let format_layer = format_layer
.with_filter(
tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or(EnvFilter::new("info")),
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or(EnvFilter::new(level.as_str())),
)
.boxed();
layers.push(stdout);
layers.push(format_layer);

let guard = if let Some(ref file) = *CONVEX_TRACE_FILE {
let (file_writer, guard) = tracing_appender::non_blocking(file);
Expand Down
4 changes: 2 additions & 2 deletions crates/local_backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{

use anyhow::anyhow;
use clap::Parser;
use cmd_util::env::config_tool;
use cmd_util::env::config_service;
use common::{
errors::MainError,
http::{
Expand Down Expand Up @@ -41,7 +41,7 @@ use tokio::signal::{

fn main() -> Result<(), MainError> {
tracing::info!("Starting a local backend");
let _guard = config_tool();
let _guard = config_service();
let config = LocalConfig::parse();
tracing::info!("Starting with config {:?}", config);

Expand Down

0 comments on commit a62ecaa

Please sign in to comment.