Skip to content

Commit 22bf5e8

Browse files
committed
feat: implement a custom logging format
1 parent ea5d043 commit 22bf5e8

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

src-tauri/src/lib.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use tauri::Manager;
2+
use chrono::{DateTime, Utc};
23

34
#[cfg_attr(mobile, tauri::mobile_entry_point)]
45
pub fn run() {
@@ -63,11 +64,27 @@ pub fn run() {
6364
file_name: Some(format!("latest")),
6465
},
6566
))
67+
// Make a custom logs format
68+
.format(|out, message, record| {
69+
let now_utc: DateTime<Utc> = Utc::now();
70+
let formatted_date = now_utc.format("%d-%m-%Y").to_string();
71+
let formatted_time = now_utc.format("%H:%M:%S%.3f").to_string();
72+
73+
out.finish(format_args!(
74+
"[{}][{}][{}][{}] {}",
75+
formatted_date,
76+
formatted_time,
77+
record.target(),
78+
record.level(),
79+
message,
80+
))
81+
})
6682
// Use log rotation
6783
.rotation_strategy(tauri_plugin_log::RotationStrategy::KeepAll)
6884
.build(),
6985
)?;
7086
} else {
87+
// Basically the same, but I don't know Rust to refactor these duplicates
7188
// Release mode
7289
app.handle().plugin(
7390
tauri_plugin_log::Builder::new()
@@ -80,6 +97,21 @@ pub fn run() {
8097
file_name: Some(format!("latest")),
8198
},
8299
))
100+
// Make a custom logs format
101+
.format(|out, message, record| {
102+
let now_utc: DateTime<Utc> = Utc::now();
103+
let formatted_date = now_utc.format("%d-%m-%Y").to_string();
104+
let formatted_time = now_utc.format("%H:%M:%S%.3f").to_string();
105+
106+
out.finish(format_args!(
107+
"[{}][{}][{}][{}] {}",
108+
formatted_date,
109+
formatted_time,
110+
record.target(),
111+
record.level(),
112+
message,
113+
))
114+
})
83115
// Keep log file size at 8 MB
84116
.max_file_size(8_388_608)
85117
// Use log rotation
@@ -106,4 +138,4 @@ fn get_executable_directory() -> Result<String, String> {
106138
}
107139
Err(error) => Err(format!("Error getting executable path: {}", error)),
108140
}
109-
}
141+
}

src/lib/general/scopes/initialize-launcher.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { ConfigType } from "@/types/application/config.type.ts";
55

66
export async function initializeLauncher(
77
config: ConfigType,
8+
startTime: number,
89
): Promise<void> {
910
if (!config.showBeforeInitialization) {
1011
/*
@@ -15,5 +16,9 @@ export async function initializeLauncher(
1516
await getCurrentWebviewWindow().show();
1617
}
1718

18-
log.info("Launcher successfully started");
19+
log.info(
20+
"Launcher successfully started in:",
21+
(performance.now() - startTime).toFixed(1),
22+
"ms",
23+
);
1924
}

src/main.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ import Logging from "@/lib/logging";
2222
import { log } from "@/lib/logging/scopes/log.ts";
2323
import type { ConfigType } from "@/types/application/config.type.ts";
2424

25+
// Measure high resolution timestamp before launcher initialization
26+
const startTime = performance.now();
27+
2528
// Check if launcher is in a portable version
2629
const portable: boolean = await General.checkIsPortable();
2730
// Get the launcher base directory
@@ -66,14 +69,13 @@ log.debug(
6669
"\n" + JSON.stringify(config, null, 2),
6770
);
6871

69-
log.debug("Creating an app instance");
7072
const AppInstance = createApp(App);
7173

7274
// Attach the app to an element with the 'ApplicationRootID' id
7375
log.debug(`Mounting app instance to the DOM element (${ApplicationRootID})`);
7476
AppInstance.mount(ApplicationRootID);
7577

7678
log.debug("Initializing launcher");
77-
await General.initializeLauncher(config).catch((error: unknown) => {
79+
await General.initializeLauncher(config, startTime).catch((error: unknown) => {
7880
log.error("Failed to initialize launcher:", Errors.prettify(error));
7981
});

0 commit comments

Comments
 (0)