| 1 | # Google Cloud Logging |
| 2 | |
| 3 | This crate contains structures for |
| 4 | [Google Cloud Structured logging](https://cloud.google.com/logging/docs/structured-logging). |
| 5 | This allows for adding more metadata to log statements that will be interpreted by the |
| 6 | [Google Cloud "Logging"][Cloud_Logging] service and can be viewed in the "Logs Explorer". |
| 7 | |
| 8 | Some errors can also be formatted so the ["Error Reporting"][Error_Reporting] service will |
| 9 | group them. |
| 10 | |
| 11 | [Cloud_Logging]: https://cloud.google.com/logging/ |
| 12 | [Error_Reporting]: https://cloud.google.com/error-reporting/. |
| 13 | |
| 14 | ## Usage |
| 15 | |
| 16 | Here you can see a snippet of how you can use it in you logging library. |
| 17 | ```rust |
| 18 | let log_entry = GoogleCloudStructLog { |
| 19 | severity: Some(match level { |
| 20 | Level::Error => GCLogSeverity::Error, |
| 21 | Level::Warn => GCLogSeverity::Warning, |
| 22 | Level::Info => GCLogSeverity::Info, |
| 23 | Level::Debug => GCLogSeverity::Debug, |
| 24 | Level::Trace => GCLogSeverity::Default, |
| 25 | }), |
| 26 | report_type: match level { |
| 27 | // More info see: https://cloud.google.com/error-reporting/docs/formatting-error-messages#@type |
| 28 | Level::Error => Some("type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent".to_owned()), |
| 29 | _ => None, |
| 30 | }, |
| 31 | message: Some( |
| 32 | format!( |
| 33 | "{}{}", |
| 34 | record.args(), |
| 35 | example_backtrace(), |
| 36 | ) |
| 37 | ), |
| 38 | operation: Some(GCOperation { |
| 39 | id: Some("My Service"), |
| 40 | producer: Some("MyService.Backend"), |
| 41 | ..Default::default() |
| 42 | }), |
| 43 | source_location: Some(GCSourceLocation { |
| 44 | file: record.file_static(), |
| 45 | line: record.line().map(|s| s.to_string()), |
| 46 | function: record.module_path_static(), |
| 47 | }), |
| 48 | time: Some(Utc::now()), |
| 49 | ..Default::default() |
| 50 | }; |
| 51 | println!( |
| 52 | "{}", |
| 53 | serde_json::to_string(&log_entry).expect("Error during logging") |
| 54 | ); |
| 55 | ``` |
| 56 | |
| 57 | To run the example use: `cargo run --example log` |
| 58 | This will result in the following output: |
| 59 | ```json |
| 60 | {"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"}} |
| 61 | {"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"}} |
| 62 | {"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"}} |
| 63 | {"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"}} |
| 64 | ``` |
| 65 | Each line in the output above is 1 log message. Each log message is a json string. |
| 66 | Note that this is **not** an array of json messages. Each json object is separated by a newline. |
| 67 | |
| 68 | <details> |
| 69 | <summary>View pretty print:</summary> |
| 70 | |
| 71 | ```json |
| 72 | { |
| 73 | "severity": "info", |
| 74 | "message": "Start logging:\n at services::module_name::he77c0bac773c93b4 line: 42\n at services::module_name::h7ad5e699ac5d6658", |
| 75 | "time": "2021-12-20T16:38:13.654978059Z", |
| 76 | "logging.googleapis.com/operation": { |
| 77 | "id": "My Service", |
| 78 | "producer": "MyService.Backend" |
| 79 | }, |
| 80 | "logging.googleapis.com/sourceLocation": { |
| 81 | "file": "examples/log/main.rs", |
| 82 | "line": "11", |
| 83 | "function": "log" |
| 84 | } |
| 85 | } |
| 86 | { |
| 87 | "severity": "warning", |
| 88 | "message": "Oh no, things might go wrong soon.:\n at services::module_name::he77c0bac773c93b4 line: 42\n at services::module_name::h7ad5e699ac5d6658", |
| 89 | "time": "2021-12-20T16:38:13.655138074Z", |
| 90 | "logging.googleapis.com/operation": { |
| 91 | "id": "My Service", |
| 92 | "producer": "MyService.Backend" |
| 93 | }, |
| 94 | "logging.googleapis.com/sourceLocation": { |
| 95 | "file": "examples/log/main.rs", |
| 96 | "line": "12", |
| 97 | "function": "log" |
| 98 | } |
| 99 | } |
| 100 | { |
| 101 | "severity": "error", |
| 102 | "message": "Yeah, this is not good.:\n at services::module_name::he77c0bac773c93b4 line: 42\n at services::module_name::h7ad5e699ac5d6658", |
| 103 | "@type": "type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent", |
| 104 | "time": "2021-12-20T16:38:13.655236672Z", |
| 105 | "logging.googleapis.com/operation": { |
| 106 | "id": "My Service", |
| 107 | "producer": "MyService.Backend" |
| 108 | }, |
| 109 | "logging.googleapis.com/sourceLocation": { |
| 110 | "file": "examples/log/main.rs", |
| 111 | "line": "13", |
| 112 | "function": "log" |
| 113 | } |
| 114 | } |
| 115 | { |
| 116 | "severity": "default", |
| 117 | "message": "Something went wrong in `my service`.:\n at services::module_name::he77c0bac773c93b4 line: 42\n at services::module_name::h7ad5e699ac5d6658", |
| 118 | "time": "2021-12-20T16:38:13.655335729Z", |
| 119 | "logging.googleapis.com/operation": { |
| 120 | "id": "My Service", |
| 121 | "producer": "MyService.Backend" |
| 122 | }, |
| 123 | "logging.googleapis.com/sourceLocation": { |
| 124 | "file": "examples/log/main.rs", |
| 125 | "line": "14", |
| 126 | "function": "log" |
| 127 | } |
| 128 | } |
| 129 | ``` |
| 130 | |
| 131 | </details> |
| 132 | |
| 133 | ## Tips |
| 134 | |
| 135 | When logging your Rust service you might also want to capture panic message and format them |
| 136 | the same way. This can be done using a |
| 137 | [panic hook](https://doc.rust-lang.org/std/panic/fn.set_hook.html). |
| 138 | |
| 139 | ## License |
| 140 | |
| 141 | The code in this project is licensed under the MIT or Apache 2.0 license. |
| 142 | |
| 143 | All contributions, code and documentation, to this project will be similarly licensed. |