feat: pretify_log utilit

This commit is contained in:
2025-01-31 23:33:06 +00:00
parent 404e6a05ed
commit d613d51d41
9 changed files with 711 additions and 3 deletions

View File

@@ -0,0 +1,87 @@
Debug: respons {
"some": "name",
"value": 12
} is received
{timespan} Info: some log without json
Error: {
"code": 500,
"error": "Something went wrong"
}
Warning: Invalid data format detected
{
"data": {
"id": 1,
"name": "test"
},
"status": "ok"
}
User log: {
"action": "login",
"timestamp": "2025-01-31T12:00:00Z",
"user": "john_doe"
}
Random text without json
Debug: Payload sent: {
"request": {
"body": {
"key": "value"
},
"type": "POST"
}
}
Another line with no json
{meta} Log: {"action": "update", "success": true}
Normal message with {
"json": "inside"
} text
{
"array": [
1,
2,
3
],
"nested": {
"deep": {
"value": "found"
}
}
}
{2025-01-31} Event: {"event": "created", "user": "admin"}
Plain text that should not be modified
Info: {
"details": "this is a test log",
"level": "info"
}
{
"array": [
{
"id": 1
},
{
"id": 2
},
{
"id": 3
}
]
}
An unformatted json log {
"name": "example",
"valid": true
}
Debug: {
"data": "test",
"items": [
{
"x": 10,
"y": 20
}
]
}
Non-json message with curly braces {like this}
{
"key1": "value1",
"key2": {
"subkey": "subvalue"
}
}

View File

@@ -0,0 +1,20 @@
Debug: respons {"some": "name", "value": 12} is received
{timespan} Info: some log without json
Error: {"error": "Something went wrong", "code": 500}
Warning: Invalid data format detected
{"status": "ok", "data": {"id": 1, "name": "test"}}
User log: {"user": "john_doe", "action": "login", "timestamp": "2025-01-31T12:00:00Z"}
Random text without json
Debug: Payload sent: {"request": {"type": "POST", "body": {"key": "value"}}}
Another line with no json
{meta} Log: {"action": "update", "success": true}
Normal message with {"json": "inside"} text
{"nested": {"deep": {"value": "found"}}, "array": [1,2,3]}
{2025-01-31} Event: {"event": "created", "user": "admin"}
Plain text that should not be modified
Info: {"level": "info", "details": "this is a test log"}
{"array": [{"id": 1}, {"id": 2}, {"id": 3}]}
An unformatted json log {"name":"example","valid":true}
Debug: {"data": "test", "items": [{"x": 10, "y": 20}]}
Non-json message with curly braces {like this}
{"key1": "value1", "key2": {"subkey": "subvalue"}}

View File

@@ -0,0 +1,36 @@
use std::fs;
use std::process::Command;
use std::io::Write;
use tempfile::NamedTempFile;
#[test]
fn test_prettify() {
let input_file = "test/fixture/test.log";
let expected_output = fs::read_to_string("test/fixture/expected.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"])
.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()
);
}
}