Skip to content

Commit e367bdf

Browse files
fix: disable persistent file logging to stop UI stalls/ANRs
Versions 0.0.88+ routed every logger.* call through a synchronous string build plus a react-native-fs append on the JS thread - capture() was not gated by __DEV__, so it ran in production. With ~480 call sites across hot paths (tool loop, LiteRT, downloads, whisper), this contended with the bridge and contributed to the 20s+ button-lag / ANR reports. logger.* is now a no-op in release builds (dev still mirrors to the console). Removes the appendPersistentLog / formatArg / write-queue / 2MB-trim machinery entirely. No behavior change beyond logging. Co-Authored-By: Dishit Karia <hanmadishit74@gmail.com>
1 parent 23ee0e9 commit e367bdf

1 file changed

Lines changed: 7 additions & 59 deletions

File tree

src/utils/logger.ts

Lines changed: 7 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,20 @@
1-
import RNFS from 'react-native-fs';
2-
3-
const LOG_FILE_NAME = 'download-debug.log';
4-
const MAX_LOG_FILE_BYTES = 2 * 1024 * 1024;
5-
const RETAINED_LOG_LINES = 4000;
6-
7-
let writeQueue = Promise.resolve();
8-
9-
function getLogFilePath(): string {
10-
return `${RNFS.DocumentDirectoryPath}/${LOG_FILE_NAME}`;
11-
}
12-
13-
function formatArg(arg: unknown): string {
14-
if (arg instanceof Error) {
15-
return `${arg.name}: ${arg.message}${arg.stack ? `\n${arg.stack}` : ''}`;
16-
}
17-
if (typeof arg === 'string') return arg;
18-
if (typeof arg === 'number' || typeof arg === 'boolean' || arg == null) return String(arg);
19-
try {
20-
return JSON.stringify(arg);
21-
} catch {
22-
return String(arg);
23-
}
24-
}
25-
26-
function appendPersistentLog(level: 'log' | 'warn' | 'error', args: unknown[]): void {
27-
const timestamp = new Date().toISOString();
28-
const line = `[${timestamp}] ${level.toUpperCase()}: ${args.map(formatArg).join(' ')}\n`;
29-
30-
writeQueue = writeQueue.then(async () => {
31-
try {
32-
const path = getLogFilePath();
33-
if (await RNFS.exists(path)) {
34-
await RNFS.appendFile(path, line, 'utf8');
35-
} else {
36-
await RNFS.writeFile(path, line, 'utf8');
37-
}
38-
39-
const stat = await RNFS.stat(path);
40-
const size = typeof stat.size === 'string' ? Number.parseInt(stat.size, 10) : stat.size;
41-
if (size > MAX_LOG_FILE_BYTES) {
42-
const content = await RNFS.readFile(path, 'utf8');
43-
const trimmed = content.split('\n').filter(Boolean).slice(-RETAINED_LOG_LINES).join('\n');
44-
await RNFS.writeFile(path, trimmed ? `${trimmed}\n` : '', 'utf8');
45-
}
46-
} catch {
47-
// Logging must never break app execution.
48-
}
49-
});
50-
}
51-
52-
function capture(level: 'log' | 'warn' | 'error', args: unknown[]): void {
53-
appendPersistentLog(level, args);
54-
}
55-
1+
/**
2+
* Persistent file logging is disabled. Versions 0.0.88+ wrote every logger.*
3+
* call to download-debug.log on the JS thread (a synchronous string build plus a
4+
* react-native-fs write per call), which contended with the bridge and
5+
* contributed to UI stalls/ANRs. logger.* is now a no-op in release builds; in
6+
* dev it still mirrors to the console.
7+
*/
568
const logger = {
579
log: (...args: unknown[]): void => {
58-
capture('log', args);
5910
if (__DEV__) console.log(...args); // NOSONAR
6011
},
6112
warn: (...args: unknown[]): void => {
62-
capture('warn', args);
6313
if (__DEV__) console.warn(...args); // NOSONAR
6414
},
6515
error: (...args: unknown[]): void => {
66-
capture('error', args);
6716
if (__DEV__) console.error(...args); // NOSONAR
6817
},
69-
getLogFilePath,
7018
};
7119

7220
export default logger;

0 commit comments

Comments
 (0)