Skip to content

Commit 46c3f78

Browse files
committed
feat: flush all logs on each app launch
1 parent fe66ee1 commit 46c3f78

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

Diff for: src/libs/Log.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type {Merge} from 'type-fest';
88
import CONST from '@src/CONST';
99
import ONYXKEYS from '@src/ONYXKEYS';
1010
import pkg from '../../package.json';
11-
import {addLog} from './actions/Console';
11+
import {addLog, flushAllLogsOnAppLaunch} from './actions/Console';
1212
import {shouldAttachLog} from './Console';
1313
import getPlatform from './getPlatform';
1414
import * as Network from './Network';
@@ -71,11 +71,12 @@ const Log = new Logger({
7171
return;
7272
}
7373

74-
console.debug(message, extraData);
75-
76-
if (shouldCollectLogs) {
77-
addLog({time: new Date(), level: CONST.DEBUG_CONSOLE.LEVELS.DEBUG, message, extraData});
78-
}
74+
flushAllLogsOnAppLaunch().then(() => {
75+
console.debug(message, extraData);
76+
if (shouldCollectLogs) {
77+
addLog({time: new Date(), level: CONST.DEBUG_CONSOLE.LEVELS.DEBUG, message, extraData});
78+
}
79+
});
7980
},
8081
isDebug: true,
8182
});

Diff for: src/libs/actions/Console.ts

+17-16
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,9 @@ let isNewAppLaunch = true;
88
* @param log the log to add
99
*/
1010
function addLog(log: Log) {
11-
/**
12-
* If this is the new app launch, we want to reset the log state in Onyx.
13-
* This is because we don't want to keep logs from previous sessions and
14-
* blow up the Onyx state.
15-
*/
16-
if (isNewAppLaunch) {
17-
isNewAppLaunch = false;
18-
Onyx.set(ONYXKEYS.LOGS, {
19-
[log.time.getTime()]: log,
20-
});
21-
} else {
22-
Onyx.merge(ONYXKEYS.LOGS, {
23-
[log.time.getTime()]: log,
24-
});
25-
}
11+
Onyx.merge(ONYXKEYS.LOGS, {
12+
[log.time.getTime()]: log,
13+
});
2614
}
2715

2816
/**
@@ -41,4 +29,17 @@ function disableLoggingAndFlushLogs() {
4129
Onyx.set(ONYXKEYS.LOGS, null);
4230
}
4331

44-
export {addLog, setShouldStoreLogs, disableLoggingAndFlushLogs};
32+
/**
33+
* Clears the persisted logs on app launch,
34+
* so that we have fresh logs for the new app session.
35+
*/
36+
function flushAllLogsOnAppLaunch() {
37+
if (!isNewAppLaunch) {
38+
return Promise.resolve();
39+
}
40+
41+
isNewAppLaunch = false;
42+
return Onyx.set(ONYXKEYS.LOGS, {});
43+
}
44+
45+
export {addLog, setShouldStoreLogs, disableLoggingAndFlushLogs, flushAllLogsOnAppLaunch};

0 commit comments

Comments
 (0)