Persistent logs, no health checks in them, and softer copy
Backend and the workers also write their log as JSON into the logs volume, one file a day kept 30 days (LOG_DIR, LOG_NAME, LOG_KEEP_DAYS), so errors can be looked up after a container is replaced. The Docker health check (/healthz every 30 s) no longer goes into the request log of the backend and the horoscope API. The home, login and packages pages no longer compare the analyses to an experienced astrologer's depth; they say what the user gets: detailed, personal text in minutes, for a fraction of a personal consultation's price. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -11,3 +11,4 @@ anyhow.workspace = true
|
||||
tokio.workspace = true
|
||||
tracing.workspace = true
|
||||
tracing-subscriber.workspace = true
|
||||
tracing-appender.workspace = true
|
||||
|
||||
@@ -3,21 +3,51 @@
|
||||
//! Environment:
|
||||
//! * `BIND_ADDR` listen address (each service has its own default)
|
||||
//! * `LOG_FORMAT` `text` (default) or `json`; `RUST_LOG` sets the level
|
||||
//! * `LOG_DIR` also write the log there, as JSON, one file a day
|
||||
//! (`<LOG_NAME>.YYYY-MM-DD.log`), keeping `LOG_KEEP_DAYS` (default 30) files
|
||||
//! * `LOG_NAME` the file name prefix (default: the service's name)
|
||||
|
||||
use std::net::SocketAddr;
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::Context;
|
||||
|
||||
/// Initializes `tracing` from `RUST_LOG` and `LOG_FORMAT`.
|
||||
pub fn init_logging() {
|
||||
use tracing_subscriber::EnvFilter;
|
||||
/// Days of log files kept in `LOG_DIR` by default.
|
||||
const LOG_KEEP_DAYS: usize = 30;
|
||||
|
||||
/// Initializes `tracing` from `RUST_LOG` and `LOG_FORMAT`, and the log files
|
||||
/// in `LOG_DIR` (named after `LOG_NAME`, else `name`), so the log outlives the
|
||||
/// container.
|
||||
pub fn init_logging(name: &str) {
|
||||
use tracing_subscriber::layer::SubscriberExt;
|
||||
use tracing_subscriber::util::SubscriberInitExt;
|
||||
use tracing_subscriber::{EnvFilter, Layer, fmt};
|
||||
|
||||
let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info,tower_http=info"));
|
||||
let builder = tracing_subscriber::fmt().with_env_filter(filter);
|
||||
if std::env::var("LOG_FORMAT").is_ok_and(|f| f.eq_ignore_ascii_case("json")) {
|
||||
builder.json().init();
|
||||
} else {
|
||||
builder.init();
|
||||
let json = std::env::var("LOG_FORMAT").is_ok_and(|f| f.eq_ignore_ascii_case("json"));
|
||||
let stdout = if json { fmt::layer().json().boxed() } else { fmt::layer().boxed() };
|
||||
let mut problem = None;
|
||||
let file = std::env::var("LOG_DIR").ok().filter(|d| !d.trim().is_empty()).and_then(|dir| {
|
||||
let prefix =
|
||||
std::env::var("LOG_NAME").ok().filter(|n| !n.trim().is_empty()).unwrap_or_else(|| name.to_string());
|
||||
let keep = std::env::var("LOG_KEEP_DAYS").ok().and_then(|d| d.parse().ok()).unwrap_or(LOG_KEEP_DAYS);
|
||||
let appender = tracing_appender::rolling::Builder::new()
|
||||
.rotation(tracing_appender::rolling::Rotation::DAILY)
|
||||
.filename_prefix(prefix)
|
||||
.filename_suffix("log")
|
||||
.max_log_files(keep.max(1))
|
||||
.build(&dir);
|
||||
match appender {
|
||||
Ok(appender) => Some(fmt::layer().json().with_ansi(false).with_writer(appender).boxed()),
|
||||
Err(e) => {
|
||||
problem = Some(format!("cannot write the log files in {dir}: {e}"));
|
||||
None
|
||||
}
|
||||
}
|
||||
});
|
||||
tracing_subscriber::registry().with(filter).with(stdout).with(file).init();
|
||||
if let Some(problem) = problem {
|
||||
tracing::warn!("{problem}; logging to stdout only");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user