-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tracing): auto register tracing layer (#324)
* feat: auto register tracing layer * feat: construct a new `LogItem` for tracing This also takes care of the formatting in the `log()` function. * feat(logger): change mpsc to crossbeam_channel This also removes the `Logger.filter` field since filtering is already handled by the EnvFilter * refactor(logger): filter fields before inserting them * tests(logger): add test * refactor(logger): quick cleanup * refactor(logger): cargo fmt * refactor: cargo sort * test: update for tracing * test: update * test: work stealing Co-authored-by: chesedo <pieter@chesedo.me>
- Loading branch information
Showing
16 changed files
with
247 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# Rust | ||
.cargo | ||
# Generated by Cargo | ||
# will have compiled files and executables | ||
**/target/ | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,44 @@ | ||
use chrono::{DateTime, Local, Utc}; | ||
use colored::{ColoredString, Colorize}; | ||
use log::Level; | ||
use serde_json::{Map, Value}; | ||
use shuttle_common::LogItem; | ||
|
||
pub fn log(datetime: DateTime<Utc>, log_item: LogItem) { | ||
let datetime: DateTime<Local> = DateTime::from(datetime); | ||
|
||
let mut fields: Map<String, Value> = serde_json::from_slice(&log_item.fields).unwrap(); | ||
|
||
let message = fields | ||
.remove("message") | ||
.map_or("".to_owned(), |msg| format!(" {}", msg.as_str().unwrap())); | ||
|
||
println!( | ||
"{}{} {:<5} {}{} {}", | ||
"{}{} {:<5} {}{}{} {}", | ||
"[".bright_black(), | ||
datetime.format("%Y-%m-%dT%H:%M:%SZ"), | ||
datetime.format("%Y-%m-%dT%H:%M:%SZ").to_string().dimmed(), | ||
get_colored_level(&log_item.level), | ||
log_item.target, | ||
log_item.target.dimmed(), | ||
"]".bright_black(), | ||
log_item.body | ||
message, | ||
fmt_fields(&fields).dimmed() | ||
); | ||
} | ||
|
||
fn get_colored_level(level: &Level) -> ColoredString { | ||
match level { | ||
Level::Trace => level.to_string().bright_black(), | ||
Level::Debug => level.to_string().blue(), | ||
Level::Info => level.to_string().green(), | ||
Level::Warn => level.to_string().yellow(), | ||
Level::Error => level.to_string().red(), | ||
fn get_colored_level(level: &String) -> ColoredString { | ||
match &level.to_uppercase()[..] { | ||
"TRACE" => level.bright_black(), | ||
"DEBUG" => level.blue(), | ||
"INFO" => level.green(), | ||
"WARN" => level.yellow(), | ||
"ERROR" => level.red(), | ||
_ => level.bright_black(), // TODO: should this panic? | ||
} | ||
} | ||
|
||
fn fmt_fields(fields: &serde_json::Map<String, serde_json::Value>) -> String { | ||
fields | ||
.iter() | ||
.map(|(field, value)| format!("{}={}", field.italic(), value)) | ||
.collect::<Vec<_>>() | ||
.join(" ") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.