Skip to content

Commit 944def4

Browse files
KyeongJoonigioboa
andauthored
docs: add guide for finding leftover console.log statements (#8203)
--------- Co-authored-by: gioboa <[email protected]>
1 parent e8d8767 commit 944def4

File tree

1 file changed

+80
-1
lines changed
  • packages/docs/src/routes/docs/(qwikcity)/guides/debugging

1 file changed

+80
-1
lines changed

packages/docs/src/routes/docs/(qwikcity)/guides/debugging/index.mdx

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ title: Debugging | Introduction
33
description: Learn a few debugging tips to streamline your journey with Qwik.
44
contributors:
55
- gioboa
6-
updated_at: '2025-08-08T00:00:00Z'
6+
- KyeongJooni
7+
updated_at: '2025-12-13T00:00:00Z'
78
created_at: '2025-08-08T00:00:00Z'
89
---
910
# Debugging
@@ -19,3 +20,81 @@ If it guesses wrong, no biggie! Just give Qwik a hint by setting the `LAUNCH_EDI
1920
For example, if you're on a Mac and use VS Code, you'd type `export LAUNCH_EDITOR=code` in your terminal.
2021

2122
Under the hood [launch-editor library](https://github.com/yyx990803/launch-editor/tree/master) is used, here are the [supported editors](https://github.com/yyx990803/launch-editor/tree/master?tab=readme-ov-file#supported-editors)
23+
24+
## Finding leftover `console.log` statements
25+
26+
When working on larger codebases, you might occasionally leave `console.log` statements in your code unintentionally. These can clutter your terminal output, and it's often difficult to identify which file and line number produced each log message.
27+
28+
### Using ESLint rules (recommended)
29+
30+
The recommended approach is to configure ESLint to prevent `console.log` statements from being committed:
31+
32+
```js
33+
// eslint.config.js
34+
export default [
35+
{
36+
rules: {
37+
'no-console': ['error', { allow: ['warn', 'error'] }],
38+
},
39+
},
40+
];
41+
```
42+
43+
This will catch `console.log` statements during development, preventing them from being committed.
44+
45+
### Using IDE search
46+
47+
Most IDEs provide powerful search functionality to find all occurrences of `console.log` across your codebase:
48+
- **VS Code**: Press `Ctrl+Shift+F` (or `Cmd+Shift+F` on Mac) and search for `console\.log`
49+
- **Enable regex mode** to match only exact `console.log` calls and not `console.error` or `console.warn`
50+
51+
This approach is simple but requires manual review of each occurrence to determine if it's intentional or leftover debug code.
52+
53+
### Using stack traces (temporary debugging)
54+
55+
If you need to identify existing leftover console.logs in a codebase that's already cluttered, you can temporarily override `console.log` to include stack traces.
56+
57+
#### In client-side components
58+
59+
You can override `console.log` in your component code:
60+
61+
```tsx
62+
// root.tsx or layout.tsx
63+
export default component$(() => {
64+
// Override console.log to include stack traces
65+
useVisibleTask$(() => {
66+
const originalLog = console.log;
67+
console.log = (...args: any[]) => {
68+
const stack = new Error()
69+
.stack?.split('\n')
70+
.filter((i) => !/^.*(\.vite|\.main\.jsx|node_modules).*/.test(i));
71+
originalLog(stack, ...args);
72+
};
73+
});
74+
75+
return (
76+
// your component JSX
77+
);
78+
});
79+
```
80+
81+
#### In server-side entry files
82+
83+
For entry files like `entry.express.tsx`, `entry.dev.tsx`, or `entry.preview.tsx`, you can override `console.log` at the module level:
84+
85+
```tsx
86+
// entry.express.tsx or entry.dev.tsx or entry.preview.tsx
87+
88+
// Override console.log at the top level
89+
const originalLog = console.log;
90+
console.log = (...args: any[]) => {
91+
const stack = new Error()
92+
.stack?.split('\n')
93+
.filter((i) => !/^.*(node_modules|internal).*/.test(i));
94+
originalLog(stack, ...args);
95+
};
96+
97+
// Rest of your entry file code...
98+
```
99+
100+
> **Note:** Remember to remove these overrides after debugging. The filter regex should be adjusted based on your specific build setup.

0 commit comments

Comments
 (0)