| 1 |
#![allow(dead_code)] |
| 2 |
use chrono::Utc; |
| 3 |
use google_cloud_logging::*; |
| 4 |
use log::{Level, Metadata, Record}; |
| 5 |
|
| 6 |
|
| 7 |
pub struct Logger { |
| 8 |
format: LogFormat, |
| 9 |
} |
| 10 |
|
| 11 |
pub enum LogFormat { |
| 12 |
Text, |
| 13 |
Json, |
| 14 |
} |
| 15 |
|
| 16 |
impl LogFormat { |
| 17 |
pub fn get_format() -> Self { |
| 18 |
let log_format = std::env::var("LOG_FORMAT").unwrap_or_else(|_| "text".to_owned()); |
| 19 |
match log_format.as_ref() { |
| 20 |
"json" => LogFormat::Json, |
| 21 |
_ => LogFormat::Text, |
| 22 |
} |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
impl Logger { |
| 27 |
pub fn new() -> Self { |
| 28 |
Self { |
| 29 |
format: LogFormat::get_format(), |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
pub fn custom(format: LogFormat) -> Self { |
| 34 |
Self { format } |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
impl log::Log for Logger { |
| 39 |
fn enabled(&self, _metadata: &Metadata) -> bool { |
| 40 |
|
| 41 |
true |
| 42 |
} |
| 43 |
|
| 44 |
fn log(&self, record: &Record) { |
| 45 |
if self.enabled(record.metadata()) { |
| 46 |
let level = record.level(); |
| 47 |
|
| 48 |
match self.format { |
| 49 |
LogFormat::Text => { |
| 50 |
println!( |
| 51 |
"{:<5}:{} - {}{}", |
| 52 |
match level { |
| 53 |
Level::Error => "ERROR", |
| 54 |
Level::Warn => "WARN", |
| 55 |
Level::Info => "INFO", |
| 56 |
Level::Debug => "DEBUG", |
| 57 |
Level::Trace => "TRACE", |
| 58 |
}, |
| 59 |
record.target(), |
| 60 |
record.args(), |
| 61 |
match level { |
| 62 |
Level::Error | Level::Warn => example_backtrace().to_owned(), |
| 63 |
_ => "".to_owned(), |
| 64 |
} |
| 65 |
); |
| 66 |
} |
| 67 |
LogFormat::Json => { |
| 68 |
let log_entry = GoogleCloudStructLog { |
| 69 |
severity: Some(match level { |
| 70 |
Level::Error => GCLogSeverity::Error, |
| 71 |
Level::Warn => GCLogSeverity::Warning, |
| 72 |
Level::Info => GCLogSeverity::Info, |
| 73 |
Level::Debug => GCLogSeverity::Debug, |
| 74 |
Level::Trace => GCLogSeverity::Default, |
| 75 |
}), |
| 76 |
report_type: match level { |
| 77 |
|
| 78 |
Level::Error => Some("type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent".to_owned()), |
| 79 |
_ => None, |
| 80 |
}, |
| 81 |
message: Some( |
| 82 |
format!( |
| 83 |
"{}{}", |
| 84 |
record.args(), |
| 85 |
example_backtrace(), |
| 86 |
) |
| 87 |
), |
| 88 |
operation: Some(GCOperation { |
| 89 |
id: Some("My Service"), |
| 90 |
producer: Some("MyService.Backend"), |
| 91 |
..Default::default() |
| 92 |
}), |
| 93 |
source_location: Some(GCSourceLocation { |
| 94 |
file: record.file_static(), |
| 95 |
line: record.line().map(|s| s.to_string()), |
| 96 |
function: record.module_path_static(), |
| 97 |
}), |
| 98 |
time: Some(Utc::now()), |
| 99 |
..Default::default() |
| 100 |
}; |
| 101 |
println!( |
| 102 |
"{}", |
| 103 |
serde_json::to_string(&log_entry).expect("Error during logging") |
| 104 |
); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
fn flush(&self) {} |
| 111 |
} |
| 112 |
|
| 113 |
fn example_backtrace() -> &'static str { |
| 114 |
":\ |
| 115 |
\n at services::module_name::he77c0bac773c93b4 line: 42\ |
| 116 |
\n at services::module_name::h7ad5e699ac5d6658" |
| 117 |
} |