Skip to content

Commit

Permalink
Properly configure tracing subscriber (#9)
Browse files Browse the repository at this point in the history
Uses the EnvFilter with tracing_subscriber, and will hopefully avoid
printing ANSI escape codes in prod.
  • Loading branch information
junlarsen authored Apr 18, 2024
1 parent 50d643e commit aa3a3f1
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 6 deletions.
66 changes: 66 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ reqwest = { version = "0.11.26", features = ["json"] }
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.114"
tracing = "0.1.28"
tracing-subscriber = "0.3.18"
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "fmt"] }
tungstenite = { version = "0.21.0", features = ["handshake", "native-tls"]}
uuid = { version = "1.7.0", features = ["v4", "serde"] }
rand = "0.8.5"
Expand Down
4 changes: 2 additions & 2 deletions src/healthcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ pub async fn start_http_server() -> tokio::io::Result<()> {
let (mut socket, _) = listener.accept().await?;
tokio::spawn(async move {
match socket.peer_addr() {
Ok(peer_addr) => tracing::info!("Handling connection from {}", peer_addr),
Err(err) => tracing::info!("Handling connection from unknown peer {}", err),
Ok(peer_addr) => tracing::debug!("Handling connection from {}", peer_addr),
Err(err) => tracing::debug!("Handling connection from unknown peer {}", err),
}
let response = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nHealthcheck OK";
let _ = socket.write_all(response.as_bytes()).await;
Expand Down
8 changes: 5 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::healthcheck::start_http_server;
use crate::slack::start_websocket_client;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;

mod healthcheck;
mod roulette;
Expand All @@ -8,9 +10,9 @@ mod slack_message;

#[tokio::main]
async fn main() {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::INFO)
.with_ansi(true)
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer().with_ansi(std::env::var("TERM").is_ok()))
.with(tracing_subscriber::EnvFilter::from_default_env())
.init();
let app_handle = tokio::spawn(async move {
start_websocket_client().await;
Expand Down
2 changes: 2 additions & 0 deletions src/slack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ async fn handle_disconnect_message(
message: incoming::SlackDisconnectIncomingMessage,
client: &Client,
) -> Option<SlackWebSocket> {
tracing::info!("Received disconnect message: {:?}", message.reason.as_str());

match message.reason.as_str() {
"link_disabled" => {
tracing::info!("Link disabled, stopping bot");
Expand Down

0 comments on commit aa3a3f1

Please sign in to comment.