Skip to content

Commit 058c09b

Browse files
committed
Make logging level configurable
Read initial log level from config file if present. Once the server is running the level can be changed via a POST request to /loglevel/{level} where level is one of info, debug or trace.
1 parent ee6418d commit 058c09b

File tree

3 files changed

+50
-7
lines changed

3 files changed

+50
-7
lines changed

src/config.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ pub struct GlazedConfig {
1010
pub bind_address: SocketAddr,
1111
pub public_address: Option<Url>,
1212
pub tiled_client: TiledClientConfig,
13+
#[serde(default)]
14+
pub log_level: LogLevel,
1315
}
1416
impl GlazedConfig {
1517
pub fn from_file(path: &Path) -> Result<Self, ConfigError> {
@@ -24,6 +26,7 @@ impl GlazedConfig {
2426
tiled_client: TiledClientConfig {
2527
address: Url::parse("http://localhost:8000").expect("Static URL is valid"),
2628
},
29+
log_level: LogLevel::Info,
2730
}
2831
}
2932
}
@@ -32,3 +35,24 @@ impl GlazedConfig {
3235
pub struct TiledClientConfig {
3336
pub address: Url,
3437
}
38+
39+
#[derive(Debug, Default, Deserialize, Clone, Copy)]
40+
pub enum LogLevel {
41+
#[default]
42+
#[serde(alias = "info")]
43+
Info,
44+
#[serde(alias = "debug")]
45+
Debug,
46+
#[serde(alias = "trace")]
47+
Trace,
48+
}
49+
50+
impl From<LogLevel> for tracing::level_filters::LevelFilter {
51+
fn from(value: LogLevel) -> Self {
52+
match value {
53+
LogLevel::Info => Self::INFO,
54+
LogLevel::Debug => Self::DEBUG,
55+
LogLevel::Trace => Self::TRACE,
56+
}
57+
}
58+
}

src/main.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use async_graphql::{EmptyMutation, EmptySubscription, Schema};
2+
use axum::extract::Path;
23
use axum::http::StatusCode;
34
use axum::response::{Html, IntoResponse};
45
use axum::routing::{get, post};
@@ -18,17 +19,24 @@ use serde_json::json;
1819
use tokio::select;
1920
use tokio::signal::unix::{SignalKind, signal};
2021
use tracing::info;
22+
use tracing::level_filters::LevelFilter;
23+
use tracing_subscriber::layer::SubscriberExt as _;
24+
use tracing_subscriber::util::SubscriberInitExt;
25+
use tracing_subscriber::{Registry, fmt, reload};
2126
use url::Url;
2227

2328
use crate::clients::TiledClient;
24-
use crate::config::GlazedConfig;
29+
use crate::config::{GlazedConfig, LogLevel};
2530
use crate::handlers::{download_handler, graphiql_handler, graphql_handler};
2631
use crate::model::TiledQuery;
2732

2833
#[tokio::main]
2934
async fn main() -> Result<(), Box<dyn std::error::Error>> {
30-
let subscriber = tracing_subscriber::FmtSubscriber::new();
31-
tracing::subscriber::set_global_default(subscriber)?;
35+
let (filter, filter_reload) = reload::Layer::new(LevelFilter::INFO);
36+
tracing_subscriber::registry()
37+
.with(filter)
38+
.with(fmt::Layer::default())
39+
.init();
3240

3341
let cli = Cli::init();
3442
let config;
@@ -41,15 +49,21 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
4149
info!("Using default config");
4250
config = GlazedConfig::default();
4351
}
52+
filter_reload
53+
.modify(|f| *f = config.log_level.into())
54+
.unwrap();
4455
match cli.command {
45-
Commands::Serve => serve(config).await,
56+
Commands::Serve => serve(config, filter_reload).await,
4657
}
4758
}
4859

4960
#[derive(Clone)]
5061
pub struct RootAddress(Url);
5162

52-
async fn serve(config: GlazedConfig) -> Result<(), Box<dyn std::error::Error>> {
63+
async fn serve(
64+
config: GlazedConfig,
65+
reload: reload::Handle<LevelFilter, Registry>,
66+
) -> Result<(), Box<dyn std::error::Error>> {
5367
let client = TiledClient::new(config.tiled_client.address);
5468
let public_address = config
5569
.public_address
@@ -72,6 +86,12 @@ async fn serve(config: GlazedConfig) -> Result<(), Box<dyn std::error::Error>> {
7286
get(Json(json!({"version": env!("CARGO_PKG_VERSION")}))),
7387
)
7488
.route("/asset/{run}/{stream}/{det}/{id}", get(download_handler))
89+
.route(
90+
"/loglevel/{level}",
91+
post(|level: Path<LogLevel>| async move {
92+
reload.clone().modify(|f| *f = level.0.into()).unwrap();
93+
}),
94+
)
7595
.with_state(client)
7696
.fallback((
7797
StatusCode::NOT_FOUND,

src/model.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::collections::HashMap;
1010

1111
use async_graphql::{Context, Object, Result, Union};
1212
use serde_json::Value;
13-
use tracing::{info, instrument};
13+
use tracing::instrument;
1414

1515
use crate::RootAddress;
1616
use crate::clients::{ClientError, TiledClient};
@@ -174,7 +174,6 @@ impl TableData {
174174
.map(|s| s.as_str())
175175
.collect::<Vec<_>>()
176176
.join("/");
177-
info!("path: {:?}", p);
178177

179178
let table_data = client.table_full(&p, columns, headers).await?;
180179
Ok(table_data)

0 commit comments

Comments
 (0)