1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
# Google Cloud Logging
This crate contains structures for
[Google Cloud Structured logging](https://cloud.google.com/logging/docs/structured-logging).
This allows for adding more metadata to log statements that will be interpreted by the
[Google Cloud "Logging"][Cloud_Logging] service and can be viewed in the "Logs Explorer".
Some errors can also be formatted so the ["Error Reporting"][Error_Reporting] service will
group them.
[Cloud_Logging]: https://cloud.google.com/logging/
[Error_Reporting]: https://cloud.google.com/error-reporting/.
## Usage
Here you can see a snippet of how you can use it in you logging library.
```rust
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")
);
```
To run the example use: `cargo run --example log`
This will result in the following output:
```json
{"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"}}
```
Each line in the output above is 1 log message. Each log message is a json string.
Note that this is **not** an array of json messages. Each json object is separated by a newline.
<details>
<summary>View pretty print:</summary>
```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"
}
}
```
</details>
## Tips
When logging your Rust service you might also want to capture panic message and format them
the same way. This can be done using a
[panic hook](https://doc.rust-lang.org/std/panic/fn.set_hook.html).
## License
The code in this project is licensed under the MIT or Apache 2.0 license.
All contributions, code and documentation, to this project will be similarly licensed.
|