Skip to content

Commit fd02c13

Browse files
authored
Merge pull request #209 from wwWallet/feat/simplify-console
Simplify Console Log Management
2 parents f413c51 + 0c6f1f5 commit fd02c13

File tree

3 files changed

+13
-39
lines changed

3 files changed

+13
-39
lines changed

.env.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ REACT_APP_FIREBASE_APP_ID=<Your_Firebase_App_ID>
1313
REACT_APP_FIREBASE_MEASUREMENT_ID=<Your_Firebase_Measurement_ID>
1414
REACT_APP_DID_KEY_VERSION=jwk_jcs-pub
1515
REACT_APP_VERSION=$npm_package_version
16-
REACT_APP_CONSOLE_TYPES=info,warn,error
16+
REACT_APP_DISPLAY_CONSOLE=true

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ The project uses environment variables to manage different configurations. A `.e
7070
- REACT_APP_FIREBASE_MESSAGING_SENDER_ID: Your Firebase Messaging Sender ID.
7171
- REACT_APP_FIREBASE_APP_ID: Your Firebase App ID.
7272
- REACT_APP_FIREBASE_MEASUREMENT_ID: Your Firebase Measurement ID.
73-
- REACT_APP_CONSOLE_TYPES: Enable console logs (info, warn, error) separated by commas or leave empty for none.
73+
- REACT_APP_DISPLAY_CONSOLE: Handle console logs (`true` or `false`). If left empty, it will be handled as `true`.
7474

7575
4. Install dependencies:
7676
```bash

src/ConsoleBehavior.js

Lines changed: 11 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,16 @@
1-
function isMethodAllowed(method) {
2-
3-
const allowedMethodsEnv = process.env.REACT_APP_CONSOLE_TYPES?.split(',') || [];
4-
const methodCategories = {
5-
log: 'info',
6-
info: 'info',
7-
warn: 'warn',
8-
error: 'error',
9-
assert: 'error'
10-
};
11-
12-
return allowedMethodsEnv.includes(methodCategories[method]);
13-
}
1+
//ConsoleBehavior.js
142

153
function ConsoleBehavior() {
16-
const originalConsole = { ...console };
17-
18-
const originalPrepareStackTrace = Error.prepareStackTrace;
19-
20-
Object.keys(console).forEach(method => {
21-
if (typeof console[method] === 'function') {
22-
console[method] = (...args) => {
23-
if (isMethodAllowed(method)) {
24-
Error.prepareStackTrace = (_, stack) => stack;
25-
const stack = new Error().stack;
26-
Error.prepareStackTrace = originalPrepareStackTrace;
27-
28-
const callSite = stack[1];
29-
if (callSite) {
30-
const fileName = callSite.getFileName();
31-
const lineNumber = callSite.getLineNumber();
32-
args.push(`(at ${fileName}:${lineNumber})`);
33-
}
34-
35-
originalConsole[method].apply(console, args);
36-
}
37-
};
38-
}
39-
});
4+
// If displayConsole is undefined, proceed as true
5+
const displayConsole = process.env.REACT_APP_DISPLAY_CONSOLE;
6+
7+
if (displayConsole === 'false') {
8+
Object.keys(console).forEach(method => {
9+
if (typeof console[method] === 'function') {
10+
console[method] = () => { };
11+
}
12+
});
13+
}
4014
}
4115

4216
export default ConsoleBehavior;

0 commit comments

Comments
 (0)