Skip to content

Commit b3cfc2c

Browse files
committed
fix: logger
1 parent c128573 commit b3cfc2c

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

src/lib/logger.js

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,20 @@ class Logger {
2222
#logFileName;
2323
#flushInterval;
2424
#autoFlushInterval;
25+
#maxFileSize;
2526

26-
constructor(maxBufferSize = 1000, logLevel = "info", flushInterval = 30000) {
27+
constructor(
28+
maxBufferSize = 1000,
29+
logLevel = "info",
30+
flushInterval = 30000,
31+
maxFileSize = 10 * 1024 * 1024,
32+
) {
2733
this.#logBuffer = new Map();
2834
this.#maxBufferSize = maxBufferSize;
2935
this.#logLevel = logLevel;
3036
this.#logFileName = constants.LOG_FILE_NAME;
3137
this.#flushInterval = flushInterval;
38+
this.#maxFileSize = maxFileSize;
3239
this.#startAutoFlush(); // Automatically flush logs at intervals
3340
this.#setupAppLifecycleHandlers(); // Handle app lifecycle events for safe log saving
3441
}
@@ -69,23 +76,28 @@ class Logger {
6976

7077
#writeLogToFile = async (logContent) => {
7178
try {
72-
if (
73-
!(await fsOperation(
74-
Url.join(DATA_STORAGE, constants.LOG_FILE_NAME),
75-
).exists())
76-
) {
79+
const logFilePath = Url.join(DATA_STORAGE, constants.LOG_FILE_NAME);
80+
if (!(await fsOperation(logFilePath).exists())) {
7781
await fsOperation(window.DATA_STORAGE).createFile(
7882
constants.LOG_FILE_NAME,
7983
logContent,
8084
);
8185
} else {
82-
let existingData = await fsOperation(
83-
Url.join(DATA_STORAGE, constants.LOG_FILE_NAME),
84-
).readFile("utf8");
86+
let existingData = await fsOperation(logFilePath).readFile("utf8");
8587
let newData = existingData + "\n" + logContent;
86-
await fsOperation(
87-
Url.join(DATA_STORAGE, constants.LOG_FILE_NAME),
88-
).writeFile(newData);
88+
// Check if the new data exceeds the maximum file size
89+
if (new Blob([newData]).size > this.#maxFileSize) {
90+
const lines = newData.split("\n");
91+
while (
92+
new Blob([lines.join("\n")]).size > this.#maxFileSize &&
93+
lines.length > 0
94+
) {
95+
lines.shift();
96+
}
97+
newData = lines.join("\n");
98+
}
99+
100+
await fsOperation(logFilePath).writeFile(newData);
89101
}
90102
} catch (error) {
91103
console.error("Error in handling fs operation on log file. Error:", err);

0 commit comments

Comments
 (0)