feat(package/prettify-log): colored output for log keywords
This commit is contained in:
87
package/prettify-log/test/fixture/expected-colored.log
Normal file
87
package/prettify-log/test/fixture/expected-colored.log
Normal file
@@ -0,0 +1,87 @@
|
||||
[34mDebug[0m: respons [1m{[0m
|
||||
[1;34m"[0m[1;34msome[0m[1;34m"[0m: [32m"[0m[32mname[0m[32m"[0m,
|
||||
[1;34m"[0m[1;34mvalue[0m[1;34m"[0m: 12[1m
|
||||
}[0m is received
|
||||
{timespan} [32mInfo[0m: some log without json
|
||||
[31mError[0m: [1m{[0m
|
||||
[1;34m"[0m[1;34mcode[0m[1;34m"[0m: 500,
|
||||
[1;34m"[0m[1;34merror[0m[1;34m"[0m: [32m"[0m[32mSomething went wrong[0m[32m"[0m[1m
|
||||
}[0m
|
||||
[33mWarning[0m: Invalid data format detected
|
||||
[1m{[0m
|
||||
[1;34m"[0m[1;34mdata[0m[1;34m"[0m: [1m{[0m
|
||||
[1;34m"[0m[1;34mid[0m[1;34m"[0m: 1,
|
||||
[1;34m"[0m[1;34mname[0m[1;34m"[0m: [32m"[0m[32mtest[0m[32m"[0m[1m
|
||||
}[0m,
|
||||
[1;34m"[0m[1;34mstatus[0m[1;34m"[0m: [32m"[0m[32mok[0m[32m"[0m[1m
|
||||
}[0m
|
||||
User [32mlog[0m: [1m{[0m
|
||||
[1;34m"[0m[1;34maction[0m[1;34m"[0m: [32m"[0m[32mlogin[0m[32m"[0m,
|
||||
[1;34m"[0m[1;34mtimestamp[0m[1;34m"[0m: [32m"[0m[32m2025-01-31T12:00:00Z[0m[32m"[0m,
|
||||
[1;34m"[0m[1;34muser[0m[1;34m"[0m: [32m"[0m[32mjohn_doe[0m[32m"[0m[1m
|
||||
}[0m
|
||||
Random text without json
|
||||
[34mDebug[0m: Payload sent: [1m{[0m
|
||||
[1;34m"[0m[1;34mrequest[0m[1;34m"[0m: [1m{[0m
|
||||
[1;34m"[0m[1;34mbody[0m[1;34m"[0m: [1m{[0m
|
||||
[1;34m"[0m[1;34mkey[0m[1;34m"[0m: [32m"[0m[32mvalue[0m[32m"[0m[1m
|
||||
}[0m,
|
||||
[1;34m"[0m[1;34mtype[0m[1;34m"[0m: [32m"[0m[32mPOST[0m[32m"[0m[1m
|
||||
}[0m[1m
|
||||
}[0m
|
||||
Another line with no json
|
||||
{meta} [32mLog[0m: {"action": "update", "success": true}
|
||||
Normal message with [1m{[0m
|
||||
[1;34m"[0m[1;34mjson[0m[1;34m"[0m: [32m"[0m[32minside[0m[32m"[0m[1m
|
||||
}[0m text
|
||||
[1m{[0m
|
||||
[1;34m"[0m[1;34marray[0m[1;34m"[0m: [1m[[0m
|
||||
1,
|
||||
2,
|
||||
3[1m
|
||||
][0m,
|
||||
[1;34m"[0m[1;34mnested[0m[1;34m"[0m: [1m{[0m
|
||||
[1;34m"[0m[1;34mdeep[0m[1;34m"[0m: [1m{[0m
|
||||
[1;34m"[0m[1;34mvalue[0m[1;34m"[0m: [32m"[0m[32mfound[0m[32m"[0m[1m
|
||||
}[0m[1m
|
||||
}[0m[1m
|
||||
}[0m
|
||||
{2025-01-31} Event: {"event": "created", "user": "admin"}
|
||||
Plain text that should not be modified
|
||||
[32mInfo[0m: [1m{[0m
|
||||
[1;34m"[0m[1;34mdetails[0m[1;34m"[0m: [32m"[0m[32mthis is a test log[0m[32m"[0m,
|
||||
[1;34m"[0m[1;34mlevel[0m[1;34m"[0m: [32m"[0m[32minfo[0m[32m"[0m[1m
|
||||
}[0m
|
||||
[1m{[0m
|
||||
[1;34m"[0m[1;34marray[0m[1;34m"[0m: [1m[[0m
|
||||
[1m{[0m
|
||||
[1;34m"[0m[1;34mid[0m[1;34m"[0m: 1[1m
|
||||
}[0m,
|
||||
[1m{[0m
|
||||
[1;34m"[0m[1;34mid[0m[1;34m"[0m: 2[1m
|
||||
}[0m,
|
||||
[1m{[0m
|
||||
[1;34m"[0m[1;34mid[0m[1;34m"[0m: 3[1m
|
||||
}[0m[1m
|
||||
][0m[1m
|
||||
}[0m
|
||||
An unformatted json log [1m{[0m
|
||||
[1;34m"[0m[1;34mname[0m[1;34m"[0m: [32m"[0m[32mexample[0m[32m"[0m,
|
||||
[1;34m"[0m[1;34mvalid[0m[1;34m"[0m: true[1m
|
||||
}[0m
|
||||
[34mDebug[0m: [1m{[0m
|
||||
[1;34m"[0m[1;34mdata[0m[1;34m"[0m: [32m"[0m[32mtest[0m[32m"[0m,
|
||||
[1;34m"[0m[1;34mitems[0m[1;34m"[0m: [1m[[0m
|
||||
[1m{[0m
|
||||
[1;34m"[0m[1;34mx[0m[1;34m"[0m: 10,
|
||||
[1;34m"[0m[1;34my[0m[1;34m"[0m: 20[1m
|
||||
}[0m[1m
|
||||
][0m[1m
|
||||
}[0m
|
||||
Non-json message with curly braces {like this}
|
||||
[1m{[0m
|
||||
[1;34m"[0m[1;34mkey1[0m[1;34m"[0m: [32m"[0m[32mvalue1[0m[32m"[0m,
|
||||
[1;34m"[0m[1;34mkey2[0m[1;34m"[0m: [1m{[0m
|
||||
[1;34m"[0m[1;34msubkey[0m[1;34m"[0m: [32m"[0m[32msubvalue[0m[32m"[0m[1m
|
||||
}[0m[1m
|
||||
}[0m
|
||||
@@ -34,3 +34,35 @@ fn test_prettify() {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_prettify_colored() {
|
||||
let input_file = "test/fixture/test.log";
|
||||
let expected_output = fs::read_to_string("test/fixture/expected-colored.log")
|
||||
.expect("Failed to read expected.log");
|
||||
|
||||
let mut actual_output_file = NamedTempFile::new().expect("Failed to create temp file");
|
||||
|
||||
let output = Command::new("cargo")
|
||||
.args(&["run", "--quiet", "--", "--color-output"])
|
||||
.stdin(fs::File::open(input_file).expect("Failed to open test.log"))
|
||||
.output()
|
||||
.expect("Failed to run prettify_logs");
|
||||
|
||||
assert!(output.status.success());
|
||||
|
||||
actual_output_file
|
||||
.write_all(&output.stdout)
|
||||
.expect("Failed to write output");
|
||||
|
||||
let actual_output = String::from_utf8_lossy(&output.stdout);
|
||||
|
||||
assert_eq!(actual_output.trim(), expected_output.trim(), "Output does not match expected.log");
|
||||
|
||||
if actual_output.trim() != expected_output.trim() {
|
||||
eprintln!(
|
||||
"Test failed! Actual output saved to: {}",
|
||||
actual_output_file.path().display()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user