Skip to content

Commit

Permalink
Merge pull request #36659 from callstack-internal/fix/collecting-logs…
Browse files Browse the repository at this point in the history
…-on-dev

[NO QA] fix: collecting logs on dev
  • Loading branch information
techievivek authored Feb 16, 2024
2 parents a88f89a + d73a814 commit 758e056
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/libs/Console/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
/* eslint-disable @typescript-eslint/naming-convention */
import Onyx from 'react-native-onyx';
import {addLog} from '@libs/actions/Console';
import CONFIG from '@src/CONFIG';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Log} from '@src/types/onyx';

let shouldStoreLogs = false;

Onyx.connect({
key: ONYXKEYS.SHOULD_STORE_LOGS,
callback: (val) => {
if (!val) {
return;
}
shouldStoreLogs = val;
},
});

/* store the original console.log function so we can call it */
// eslint-disable-next-line no-console
const originalConsoleLog = console.log;
Expand Down Expand Up @@ -44,11 +58,15 @@ function logMessage(args: unknown[]) {

/**
* Override the console.log function to add logs to the store
* Log only in production environment to avoid storing large logs in development
* @param args arguments passed to the console.log function
*/
// eslint-disable-next-line no-console
console.log = (...args) => {
logMessage(args);
if (shouldStoreLogs && CONFIG.ENVIRONMENT !== CONST.ENVIRONMENT.DEV) {
logMessage(args);
}

originalConsoleLog.apply(console, args);
};

Expand Down

0 comments on commit 758e056

Please sign in to comment.