Set up logging properly

Rocket's logger was the only one, so messages from the daemons depended on
it having launched, and most messages bypassed logging with println!.

- Install env_logger at startup: level from log.level (LOG_LEVEL), with
  RUST_LOG overriding it. Rocket's per-request logs and sqlx's Postgres
  notices are kept to warnings.
- Under systemd, prefix lines with their syslog priority so journald
  records levels (journalctl -p warning works).
- Replace every println! with a log macro at a fitting level.
- Recurring failures are a warning when they start or change, debug while
  they repeat, and an info message when they recover.
- Listenbrainz errors are readable: an unknown user is "Listenbrainz user
  "x" doesn't exist" rather than a JSON decode error, and a period without
  stats yet (204) leaves the playlist alone instead of failing to decode.
  The UI's Refresh shows the same messages.
- Warnings name the playlist or rule they're about.
- Songs missing from the library are debug output, not errors every tick.
- Daemons no longer run twice at startup (interval's first tick is
  immediate), and a failing playlist rule or daemon is logged instead of
  panicking the task.
- Subsonic errors display as messages rather than Debug output.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018opPJMJjLWr4RqdG6vuK6p
This commit is contained in:
Claude committed 2026-11-13 21:48:54 +00:00
1 parent e176ddac6c
commit a594c56abc
14 files changed
+436 -67

No files matched your search

+60 -3
View File
@@ -38,6 +38,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
)
.unwrap();
init_logging(CONFIG.get().unwrap());
let database = PgPool::connect(
CONFIG
.get()
@@ -59,19 +61,25 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
{
let _media_client = media_client.clone();
tokio::spawn(async move {
update_playlists_daemon(_media_client).await.unwrap();
if let Err(e) = update_playlists_daemon(_media_client).await {
log::error!("Tracked playlist daemon stopped: {}", e);
}
});
}
{
let _media_client = media_client.clone();
tokio::spawn(async move {
create_playlists_daemon(_media_client).await.unwrap();
if let Err(e) = create_playlists_daemon(_media_client).await {
log::error!("Playlist rule daemon stopped: {}", e);
}
});
}
{
let _media_client = media_client.clone();
tokio::spawn(async move {
cluster_mixes_daemon(_media_client).await.unwrap();
if let Err(e) = cluster_mixes_daemon(_media_client).await {
log::error!("Cluster mix daemon stopped: {}", e);
}
});
}
@@ -111,6 +119,55 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}
/// Log to stdout at `log.level` (default "info"). RUST_LOG overrides it, e.g.
/// `RUST_LOG=playlistd=debug,sqlx=info`.
fn init_logging(config: &Config) {
use log::LevelFilter;
use std::io::Write;
let configured = config.get::<String>("log.level").ok();
let level = configured
.as_deref()
.and_then(|l| l.parse::<LevelFilter>().ok())
.unwrap_or(LevelFilter::Info);
let mut builder = env_logger::Builder::new();
builder.filter_level(level);
// Rocket logs every request at info; keep its warnings and launch messages
builder.filter_module("rocket", level.min(LevelFilter::Warn));
// sqlx logs Postgres notices (e.g. from migrations) at info
builder.filter_module("sqlx", level.min(LevelFilter::Warn));
// libraries that are very chatty at debug
for module in ["hyper", "hyper_util", "reqwest", "rustls", "h2"] {
builder.filter_module(module, level.min(LevelFilter::Info));
}
builder.parse_env("RUST_LOG");
// Under systemd, tag lines with their priority so `journalctl -p warning` works.
// journald adds its own timestamps.
if std::env::var_os("JOURNAL_STREAM").is_some() {
builder.format(|buf, record| {
let priority = match record.level() {
log::Level::Error => 3,
log::Level::Warn => 4,
log::Level::Info => 6,
log::Level::Debug | log::Level::Trace => 7,
};
writeln!(buf, "<{}>{}: {}", priority, record.target(), record.args())
});
}
builder.init();
if let Some(configured) = configured {
if configured.parse::<LevelFilter>().is_err() {
log::warn!(
"Unknown log.level {:?}; use error, warn, info, debug or trace",
configured
);
}
}
}
#[derive(Serialize)]
struct JsonError {
error: String,