google_cloud_logging/
google_cloud_logging.rs

1// Copyright 2024 FastLabs Developers
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! An example of using `logforth` with `fastrace` and Google Cloud Logging format.
16
17use fastrace::Span;
18use fastrace::collector::Config;
19use fastrace::collector::ConsoleReporter;
20use fastrace::collector::SpanContext;
21use fastrace::prelude::SpanId;
22use fastrace::prelude::TraceId;
23use logforth::append;
24use logforth::diagnostic;
25use logforth::layout::GoogleCloudLoggingLayout;
26use serde::Serialize;
27
28#[derive(Serialize)]
29struct NestedStructuredValue {
30    c: String,
31    d: bool,
32}
33
34#[derive(Serialize)]
35struct MyStructuredValue {
36    a: i32,
37    b: NestedStructuredValue,
38}
39
40fn main() {
41    logforth::starter_log::builder()
42        .dispatch(|d| {
43            d.diagnostic(diagnostic::FastraceDiagnostic::default())
44                .append(
45                    append::Stdout::default().with_layout(
46                        GoogleCloudLoggingLayout::default()
47                            .trace_project_id("project-id")
48                            .label_keys(["label1"]),
49                    ),
50                )
51        })
52        .apply();
53
54    fastrace::set_reporter(ConsoleReporter, Config::default());
55
56    {
57        let root = Span::root("root", SpanContext::new(TraceId::random(), SpanId(0)));
58        let _g = root.set_local_parent();
59
60        let structured_value = MyStructuredValue {
61            a: 1,
62            b: NestedStructuredValue {
63                c: "Hello".into(),
64                d: true,
65            },
66        };
67
68        log::info!(
69            label1 = "this is a label value",
70            notLabel:serde = structured_value;
71            "Hello label value!",
72        );
73    }
74}