Skip to content

Commit b8b969e

Browse files
committed
docs(debugging): add guide for finding leftover console.log statements
Fixes #7661
1 parent 505e454 commit b8b969e

File tree

1 file changed

+79
-2
lines changed
  • packages/docs/src/routes/docs/(qwikcity)/guides/debugging

1 file changed

+79
-2
lines changed

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

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ 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'
7-
created_at: '2025-08-08T00:00:00Z'
6+
- KyeongJooni
7+
updated_at: '2025-12-13T00:00:00Z'
8+
created_at: '2025-12-13T00:00:00Z'
89
---
910
# Debugging
1011

@@ -19,3 +20,79 @@ 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 stack traces in client-side components
29+
30+
You can override `console.log` to include stack trace information in your component code. For example, in your root component or a layout component:
31+
32+
```tsx
33+
// root.tsx or layout.tsx
34+
export default component$(() => {
35+
// Override console.log to include stack traces
36+
useVisibleTask$(() => {
37+
const originalLog = console.log;
38+
console.log = (...args: any[]) => {
39+
const stack = new Error()
40+
.stack?.split('\n')
41+
.filter((i) => !/^.*(\.vite|\.main\.jsx|node_modules).*/.test(i));
42+
originalLog(stack, ...args);
43+
};
44+
});
45+
46+
return (
47+
// your component JSX
48+
);
49+
});
50+
```
51+
52+
Now when you call `console.log("something")`, you'll see the stack trace pointing to the exact location where the log was called, making it much easier to locate and remove leftover debug statements.
53+
54+
### Using stack traces in server-side entry files
55+
56+
For entry files like `entry.express.tsx`, `entry.dev.tsx`, or `entry.preview.tsx`, you can override `console.log` at the module level:
57+
58+
```tsx
59+
// entry.express.tsx or entry.dev.tsx or entry.preview.tsx
60+
61+
// Override console.log at the top level
62+
const originalLog = console.log;
63+
console.log = (...args: any[]) => {
64+
const stack = new Error()
65+
.stack?.split('\n')
66+
.filter((i) => !/^.*(node_modules|internal).*/.test(i));
67+
originalLog(stack, ...args);
68+
};
69+
70+
// Rest of your entry file code...
71+
```
72+
73+
> **Note:** The filter regex should be adjusted based on your specific build setup. Common patterns to filter out include `.vite`, `.main.jsx`, `node_modules`, and `internal` paths.
74+
75+
### Using ESLint rules
76+
77+
You can configure ESLint to warn or error on `console.log` statements in production code:
78+
79+
```js
80+
// eslint.config.js
81+
export default [
82+
{
83+
rules: {
84+
'no-console': ['error', { allow: ['warn', 'error'] }],
85+
},
86+
},
87+
];
88+
```
89+
90+
This will catch `console.log` statements during development, preventing them from being committed.
91+
92+
### Using IDE search
93+
94+
Most IDEs provide powerful search functionality to find all occurrences of `console.log` across your codebase. For example:
95+
- **VS Code**: Press `Ctrl+Shift+F` (or `Cmd+Shift+F` on Mac) and search for `console\.log`
96+
- **Enable regex mode** to match only exact `console.log` calls and not `console.error` or `console.warn`
97+
98+
This approach is simple but requires manual review of each occurrence to determine if it's intentional or leftover debug code.

0 commit comments

Comments
 (0)