#google-cloud #logging #cloud #log-error #google #error #log

google_cloud_logging

Google Cloud 结构化日志结构

1 个不稳定版本

0.1.0 2021年12月20日

#494调试

Download history 231/week @ 2024-03-13 235/week @ 2024-03-20 265/week @ 2024-03-27 183/week @ 2024-04-03 141/week @ 2024-04-10 210/week @ 2024-04-17 262/week @ 2024-04-24 343/week @ 2024-05-01 299/week @ 2024-05-08 131/week @ 2024-05-15 206/week @ 2024-05-22 179/week @ 2024-05-29 139/week @ 2024-06-05 253/week @ 2024-06-12 154/week @ 2024-06-19 169/week @ 2024-06-26

742 每月下载次数

MIT/Apache

20KB
120

Google Cloud Logging

该crate包含Google Cloud 结构化日志的结构。这允许向日志语句中添加更多元数据,这些元数据将由Google Cloud "Logging" 服务解释,并在 "日志资源管理器" 中查看。

某些错误也可以格式化,以便"错误报告" 服务将它们分组。

使用方法

在这里,你可以看到如何在日志库中使用它的一个示例片段。

let log_entry = GoogleCloudStructLog {
    severity: Some(match level {
        Level::Error => GCLogSeverity::Error,
        Level::Warn => GCLogSeverity::Warning,
        Level::Info => GCLogSeverity::Info,
        Level::Debug => GCLogSeverity::Debug,
        Level::Trace => GCLogSeverity::Default,
    }),
    report_type: match level {
        // More info see: https://cloud.google.com/error-reporting/docs/formatting-error-messages#@type
        Level::Error => Some("type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent".to_owned()),
        _ => None,
    },
    message: Some(
        format!(
            "{}{}", 
            record.args(),
            example_backtrace(),
        )
    ),
    operation: Some(GCOperation {
        id: Some("My Service"),
        producer: Some("MyService.Backend"),
        ..Default::default()
    }),
    source_location: Some(GCSourceLocation {
        file: record.file_static(),
        line: record.line().map(|s| s.to_string()),
        function: record.module_path_static(),
    }),
    time: Some(Utc::now()),
    ..Default::default()
};
println!(
    "{}",
    serde_json::to_string(&log_entry).expect("Error during logging")
);

要运行示例,请使用: cargo run --example log 这将产生以下输出

{"severity":"info","message":"Start logging:\n   at services::module_name::he77c0bac773c93b4 line: 42\n   at services::module_name::h7ad5e699ac5d6658","time":"2021-12-20T16:33:41.643966093Z","logging.googleapis.com/operation":{"id":"My Service","producer":"MyService.Backend"},"logging.googleapis.com/sourceLocation":{"file":"examples/log/main.rs","line":"11","function":"log"}}
{"severity":"warning","message":"Oh no, things might go wrong soon.:\n   at services::module_name::he77c0bac773c93b4 line: 42\n   at services::module_name::h7ad5e699ac5d6658","time":"2021-12-20T16:33:41.644085317Z","logging.googleapis.com/operation":{"id":"My Service","producer":"MyService.Backend"},"logging.googleapis.com/sourceLocation":{"file":"examples/log/main.rs","line":"12","function":"log"}}
{"severity":"error","message":"Yeah, this is not good.:\n   at services::module_name::he77c0bac773c93b4 line: 42\n   at services::module_name::h7ad5e699ac5d6658","@type":"type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent","time":"2021-12-20T16:33:41.644179397Z","logging.googleapis.com/operation":{"id":"My Service","producer":"MyService.Backend"},"logging.googleapis.com/sourceLocation":{"file":"examples/log/main.rs","line":"13","function":"log"}}
{"severity":"default","message":"Something went wrong in `my service`.:\n   at services::module_name::he77c0bac773c93b4 line: 42\n   at services::module_name::h7ad5e699ac5d6658","time":"2021-12-20T16:33:41.644276526Z","logging.googleapis.com/operation":{"id":"My Service","producer":"MyService.Backend"},"logging.googleapis.com/sourceLocation":{"file":"examples/log/main.rs","line":"14","function":"log"}}

上述输出中的每一行都是一条日志消息。每条日志消息是一个JSON字符串。请注意,这不是一个JSON消息数组。每个JSON对象由换行符分隔。

查看美化打印
{
  "severity": "info",
  "message": "Start logging:\n   at services::module_name::he77c0bac773c93b4 line: 42\n   at services::module_name::h7ad5e699ac5d6658",
  "time": "2021-12-20T16:38:13.654978059Z",
  "logging.googleapis.com/operation": {
    "id": "My Service",
    "producer": "MyService.Backend"
  },
  "logging.googleapis.com/sourceLocation": {
    "file": "examples/log/main.rs",
    "line": "11",
    "function": "log"
  }
}
{
  "severity": "warning",
  "message": "Oh no, things might go wrong soon.:\n   at services::module_name::he77c0bac773c93b4 line: 42\n   at services::module_name::h7ad5e699ac5d6658",
  "time": "2021-12-20T16:38:13.655138074Z",
  "logging.googleapis.com/operation": {
    "id": "My Service",
    "producer": "MyService.Backend"
  },
  "logging.googleapis.com/sourceLocation": {
    "file": "examples/log/main.rs",
    "line": "12",
    "function": "log"
  }
}
{
  "severity": "error",
  "message": "Yeah, this is not good.:\n   at services::module_name::he77c0bac773c93b4 line: 42\n   at services::module_name::h7ad5e699ac5d6658",
  "@type": "type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent",
  "time": "2021-12-20T16:38:13.655236672Z",
  "logging.googleapis.com/operation": {
    "id": "My Service",
    "producer": "MyService.Backend"
  },
  "logging.googleapis.com/sourceLocation": {
    "file": "examples/log/main.rs",
    "line": "13",
    "function": "log"
  }
}
{
  "severity": "default",
  "message": "Something went wrong in `my service`.:\n   at services::module_name::he77c0bac773c93b4 line: 42\n   at services::module_name::h7ad5e699ac5d6658",
  "time": "2021-12-20T16:38:13.655335729Z",
  "logging.googleapis.com/operation": {
    "id": "My Service",
    "producer": "MyService.Backend"
  },
  "logging.googleapis.com/sourceLocation": {
    "file": "examples/log/main.rs",
    "line": "14",
    "function": "log"
  }
}

提示

当你在Rust服务中记录日志时,你可能还希望捕获panic消息并以相同的方式格式化它们。这可以通过使用panic钩子来完成。

许可证

本项目中的代码采用MIT或Apache 2.0许可证。

对本项目的所有贡献,包括代码和文档,都将采用类似的许可证。

依赖关系

~1.4–2.2MB
~41K SLoC