Skip to content

Commit

Permalink
feat: level_map
Browse files Browse the repository at this point in the history
Documentation for level_map.
Use empty map as default
  • Loading branch information
bomgar committed Dec 20, 2023
1 parent d9ea29e commit 4026ba4
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 17 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,29 @@ macOS: `$HOME/Library/Application Support`

If the file does not exist or is empty [this](./default_config.toml) is the default config.


## log levels
These levels are colorized by fblog:

```
trace
debug
info
warn
error
fatal
```

You can map additional level values (used for output and color):
```toml
[level_map]
10 = "trace"
20 = "debug"
30 = "info"
40 = "warn"
50 = "error"
60 = "fatal"
# these values for example are used by https://www.npmjs.com/package/bunyan#levels
```


6 changes: 0 additions & 6 deletions default_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,3 @@ main_line_format = "{{bold(fixed_size 19 fblog_timestamp)}} {{level_style (upper
additional_value_format = "{{bold (color_rgb 150 150 150 (fixed_size 25 key))}}: {{value}}"

[level_map]
10 = "trace"
20 = "debug"
30 = "info"
40 = "warn"
50 = "error"
60 = "fatal"
6 changes: 6 additions & 0 deletions sample_numbered.json.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{"level":"10","msg":"<>Trust key rsa-43fe6c3d-6242-11e7-8b0c-02420a000007 found in cache","package":"trust","process":"gatekeeper","store":"cache","time":"2017-07-06T15:21:16Z","version":"0.1.20170620v0125"}
{"level":"20","msg":"<>Trust key rsa-43fe6c3d-6242-11e7-8b0c-02420a000007 found in cache","package":"trust","process":"gatekeeper","store":"cache","time":"2017-07-06T15:21:16Z","version":"0.1.20170620v0125"}
{"level":"30","msg":"<>Trust key rsa-43fe6c3d-6242-11e7-8b0c-02420a000007 found in cache","package":"trust","process":"gatekeeper","store":"cache","time":"2017-07-06T15:21:16Z","version":"0.1.20170620v0125"}
{"level":"40","msg":"<>Trust key rsa-43fe6c3d-6242-11e7-8b0c-02420a000007 found in cache","package":"trust","process":"gatekeeper","store":"cache","time":"2017-07-06T15:21:16Z","version":"0.1.20170620v0125"}
{"level":"50","msg":"<>Trust key rsa-43fe6c3d-6242-11e7-8b0c-02420a000007 found in cache","package":"trust","process":"gatekeeper","store":"cache","time":"2017-07-06T15:21:16Z","version":"0.1.20170620v0125"}
{"level":"60","msg":"<>Trust key rsa-43fe6c3d-6242-11e7-8b0c-02420a000007 found in cache","package":"trust","process":"gatekeeper","store":"cache","time":"2017-07-06T15:21:16Z","version":"0.1.20170620v0125"}
33 changes: 24 additions & 9 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,7 @@ fn default_level_keys() -> Vec<String> {
}

fn default_level_map() -> BTreeMap<String, String> {
BTreeMap::from([
// https://www.npmjs.com/package/bunyan#levels
("10".to_string(), "trace".to_string()),
("20".to_string(), "debug".to_string()),
("30".to_string(), "info".to_string()),
("40".to_string(), "warn".to_string()),
("50".to_string(), "error".to_string()),
("60".to_string(), "fatal".to_string()),
])
BTreeMap::from([])
}

fn default_main_line_format() -> String {
Expand Down Expand Up @@ -121,4 +113,27 @@ mod tests {
let default_config_for_documentation = fs::read_to_string("default_config.toml").unwrap();
assert_eq!(serialized_defaults, default_config_for_documentation)
}

#[test]
fn read_empty_level_map() {
let config: Config = toml::from_str(
r#"
[level_map]
"#,
)
.unwrap();
assert_eq!(config.level_map.is_empty(), true);
}

#[test]
fn read_level_map() {
let config: Config = toml::from_str(
r#"
[level_map]
10 = "trace"
"#,
)
.unwrap();
assert_eq!(config.level_map, BTreeMap::from([("10".to_string(), "trace".to_string()),]));
}
}
6 changes: 4 additions & 2 deletions src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn print_log_line(
let string_log_entry = flatten_json(log_entry, "");
let level = {
let level = get_string_value_or_default(&string_log_entry, &log_settings.level_keys, "unknown");
log_settings.level_map.get(&level).map(ToString::to_string).unwrap_or(level)
log_settings.level_map.get(&level).cloned().unwrap_or(level)
};

let trimmed_prefix = maybe_prefix.map(|p| p.trim()).unwrap_or_else(|| "").to_string();
Expand Down Expand Up @@ -187,7 +187,9 @@ mod tests {
#[test]
fn write_log_entry_with_mapped_level() {
let handlebars = fblog_handlebar_registry_default_format();
let log_settings = LogSettings::new_default_settings();
let mut log_settings = LogSettings::new_default_settings();
log_settings.level_map = BTreeMap::from([("30".to_string(), "info".to_string())]);

let mut out: Vec<u8> = Vec::new();
let mut log_entry: Map<String, Value> = Map::new();
log_entry.insert("message".to_string(), Value::String("something happend".to_string()));
Expand Down

0 comments on commit 4026ba4

Please sign in to comment.