Skip to content

Commit

Permalink
feat: [wip] move stuff to config
Browse files Browse the repository at this point in the history
  • Loading branch information
bomgar committed Nov 28, 2023
1 parent 8a1c87f commit 9da4271
Show file tree
Hide file tree
Showing 7 changed files with 243 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
root = true

[*.rs]
indent_style = space
indent_size = 2
max_line_length = 160

160 changes: 159 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ mlua = { version = "0.9", features = ["lua54", "vendored"] }
lazy_static = "1.4.0"
handlebars = "4"
clap_complete = "4"
serde = { version = "1", features = ["derive"] }
toml = "0.8"
dirs = "5"

[dependencies.clap]
version = "4"
Expand Down
5 changes: 5 additions & 0 deletions default_config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
message_keys = ["short_message", "msg", "message"]
time_keys = ["timestamp", "time", "@timestamp"]
level_keys = ["level", "severity", "log.level", "loglevel"]
main_line_format = "{{bold(fixed_size 19 fblog_timestamp)}} {{level_style (uppercase (fixed_size 5 fblog_level))}}:{{bold(color_rgb 138 43 226 fblog_prefix)}} {{fblog_message}}"
additional_value_format = "{{bold (color_rgb 150 150 150 (fixed_size 25 key))}}: {{value}}"
67 changes: 67 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use serde::{Deserialize, Serialize};

use crate::template::{DEFAULT_ADDITIONAL_VALUE_FORMAT, DEFAULT_MAIN_LINE_FORMAT};

fn default_message_keys() -> Vec<String> {
vec!["short_message".to_string(), "msg".to_string(), "message".to_string()]
}

fn default_time_keys() -> Vec<String> {
vec!["timestamp".to_string(), "time".to_string(), "@timestamp".to_string()]
}

fn default_level_keys() -> Vec<String> {
vec!["level".to_string(), "severity".to_string(), "log.level".to_string(), "loglevel".to_string()]
}

fn default_main_line_format() -> String {
DEFAULT_MAIN_LINE_FORMAT.to_string()
}

fn default_additional_value_format() -> String {
DEFAULT_ADDITIONAL_VALUE_FORMAT.to_string()
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Config {
#[serde(default = "default_message_keys")]
pub message_keys: Vec<String>,

#[serde(default = "default_time_keys")]
pub time_keys: Vec<String>,

#[serde(default = "default_level_keys")]
pub level_keys: Vec<String>,

#[serde(default = "default_main_line_format")]
pub main_line_format: String,

#[serde(default = "default_additional_value_format")]
pub additional_value_format: String,
}

#[cfg(test)]
mod tests {
use std::fs;

use super::*;

#[test]
fn read_defaults_from_empty_config() {
let config: Config = toml::from_str(
r#"
"#,
)
.unwrap();

assert_eq!(config.level_keys, default_level_keys());
assert_eq!(config.time_keys, default_time_keys());
assert_eq!(config.message_keys, default_message_keys());
assert_eq!(config.main_line_format, DEFAULT_MAIN_LINE_FORMAT);
assert_eq!(config.additional_value_format, DEFAULT_ADDITIONAL_VALUE_FORMAT);

let serialized_defaults = toml::to_string(&config).unwrap();
let default_config_for_documentation = fs::read_to_string("default_config.toml").unwrap();
assert_eq!(serialized_defaults, default_config_for_documentation)
}
}
1 change: 1 addition & 0 deletions src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ mod tests {

assert_eq!(out_to_string(out), "2017-07-06T15:21:16 INFO: something happend\n");
}

#[test]
fn write_log_entry_with_prefix() {
let handlebars = fblog_handlebar_registry_default_format();
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::io;
extern crate regex;

mod app;
mod config;
mod filter;
mod log;
mod no_color_support;
Expand Down

0 comments on commit 9da4271

Please sign in to comment.