Skip to content

Commit 309a74f

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

File tree

1 file changed

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

1 file changed

+85
-2
lines changed

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

Lines changed: 85 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,85 @@ 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 to locate logs
29+
30+
One effective technique is to override `console.log` to include stack trace information. This makes it much easier to find where each log statement originates from.
31+
32+
#### In client-side components
33+
34+
You can override `console.log` directly in your component code. For example, in your root component or a layout component:
35+
36+
```tsx
37+
// root.tsx or layout.tsx
38+
export default component$(() => {
39+
// Override console.log to include stack traces
40+
useVisibleTask$(() => {
41+
const originalLog = console.log;
42+
console.log = (...args: any[]) => {
43+
const stack = new Error()
44+
.stack?.split('\n')
45+
.filter((i) => !/^.*(\.vite|\.main\.jsx|node_modules).*/.test(i));
46+
originalLog(stack, ...args);
47+
};
48+
});
49+
50+
return (
51+
// your component JSX
52+
);
53+
});
54+
```
55+
56+
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.
57+
58+
#### In entry files (server-side)
59+
60+
For entry files like `entry.express.tsx`, `entry.dev.tsx`, or `entry.preview.tsx`, you can override `console.log` at the module level:
61+
62+
```tsx
63+
// entry.express.tsx or entry.dev.tsx or entry.preview.tsx
64+
65+
// Override console.log at the top level
66+
const originalLog = console.log;
67+
console.log = (...args: any[]) => {
68+
const stack = new Error()
69+
.stack?.split('\n')
70+
.filter((i) => !/^.*(node_modules|internal).*/.test(i));
71+
originalLog(stack, ...args);
72+
};
73+
74+
// Rest of your entry file code...
75+
```
76+
77+
> **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.
78+
79+
### Alternative approaches
80+
81+
#### Using ESLint rules
82+
83+
You can configure ESLint to warn or error on `console.log` statements in production code:
84+
85+
```js
86+
// eslint.config.js
87+
export default [
88+
{
89+
rules: {
90+
'no-console': ['error', { allow: ['warn', 'error'] }],
91+
},
92+
},
93+
];
94+
```
95+
96+
This will catch `console.log` statements during development, preventing them from being committed.
97+
98+
#### Using IDE search
99+
100+
Most IDEs provide powerful search functionality to find all occurrences of `console.log` across your codebase. For example:
101+
- **VS Code**: Press `Ctrl+Shift+F` (or `Cmd+Shift+F` on Mac) and search for `console\.log`
102+
- **Enable regex mode** to match only exact `console.log` calls and not `console.error` or `console.warn`
103+
104+
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)