Skip to content

Json logging #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ forc-pkg = "0.68"
git2 = "0.19.0"
aws-sdk-s3 = "1.77"
aws-config = "1.5.17"

[profile.release]
panic = "unwind"
5 changes: 2 additions & 3 deletions deployment/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ RUN cargo chef cook --recipe-path recipe.json
# Up to this point, if our dependency tree stays the same,
# all layers should be cached.
COPY . .
RUN cargo build
RUN cargo build --release

# Stage 2: Run
FROM rust:1.85 as run
Expand Down Expand Up @@ -54,8 +54,7 @@ RUN tags=$(curl -s "https://api.github.com/repos/FuelLabs/sway/tags?per_page=20"

WORKDIR /root/

COPY --from=builder /build/target/debug/forc_pub .
COPY --from=builder /build/target/debug/forc_pub.d .
COPY --from=builder /build/target/release/forc_pub .
COPY --from=builder /build/Rocket.toml .
COPY --from=builder /build/.env .

Expand Down
24 changes: 20 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ use rocket::{
serde::json::Json,
State,
};
use std::env;
use std::fs::{self};
use std::path::PathBuf;
use std::str::FromStr;
use tempfile::tempdir;
use tracing::info;
use tracing::level_filters::LevelFilter;
use tracing_subscriber::EnvFilter;
use uuid::Uuid;

const ORIGINAL_TARBALL_NAME: &str = "original.tgz";
Expand Down Expand Up @@ -267,9 +268,7 @@ fn health() -> String {
// Launch the rocket server.
#[launch]
async fn rocket() -> _ {
tracing_subscriber::fmt()
.with_max_level(LevelFilter::INFO)
.init();
setup_tracing_subscriber();

let s3_client = S3ClientImpl::new().await.expect("s3 client");

Expand Down Expand Up @@ -302,3 +301,20 @@ async fn rocket() -> _ {
)
.register("/", catchers![default_catcher])
}

fn setup_tracing_subscriber() {
let default_filter = "info"; // Default log level if RUST_LOG is not set
let env_filter =
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(default_filter));
// Initialize the tracing subscriber with JSON format and no ANSI colors for non-local.
// For local, use standard formatting. Both respect RUST_LOG.
if env::var("RUN_ENV").unwrap_or_default() == "local" {
tracing_subscriber::fmt().with_env_filter(env_filter).init();
} else {
tracing_subscriber::fmt()
.json()
.with_ansi(false) // ANSI colors are not suitable for JSON logs
.with_env_filter(env_filter)
.init();
}
}
2 changes: 0 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ pub fn load_env() {
// If RUN_ENV is not set, log the error
tracing::error!("Could not load .env.local: {}", e);
}

tracing::info!("Could not load .env.local: {}", e);
}
}

Expand Down
Loading