init

Function init 

Source
pub fn init(config: Config)
Expand description

Initialize the GCP structured logging subscriber.

This sets up a tracing subscriber that outputs logs to stderr in the JSON format expected by Google Cloud Run and Cloud Logging. To associate logs with traces, create spans with a trace_id field using info_span!("trace_id", trace_id = %"your-trace-id").

If the config does not specify a project ID, this will attempt to fetch it from the GCP metadata service. If that fails, it will use “unknown” as the project ID.

§Arguments

  • config - Configuration for the subscriber

§Examples

use tracing::info;

// Auto-detect project ID from metadata service
gcplog_rs::init(gcplog_rs::Config::new());
info!("Application started");
use tracing::info;

// Use explicit project ID
gcplog_rs::init(gcplog_rs::Config::with_project_id("my-project-123"));
info!("Application started");
use tracing::info;
use tracing_subscriber::filter::LevelFilter;

// Use custom level
let config = gcplog_rs::Config::with_project_id("my-project-123")
    .with_level(LevelFilter::DEBUG);
gcplog_rs::init(config);
info!("Application started");