Enforcing Clean Code: Restricting console.log While Permitting warn/error in ESLint
Configure ESLint's no-console rule to permit console.warn and console.error while flagging console.log as an error for clean production code.
14 Dec 2025, 14:25 UTC

Preventing Debug Noise in Production
Leaving console.log statements in production code pollutes user consoles, can expose internal state, and adds unnecessary overhead. However, developers need console.warn for telemetry and console.error for genuine runtime diagnostics. The decision is to configure ESLint's no-console rule to flag console.log as an error while explicitly allowing console.warn and console.error.
Comparing Console Control Strategies
The choice of implementation depends on required complexity versus maintenance overhead. For this specific goal, the built-in ESLint rule is superior to external plugins or custom rules.
| Strategy | Complexity | Maintenance | Dependency |
|---|---|---|---|
no-console with allow list | Low | Low | None |
| Custom rule definition | Medium | Medium | None |
eslint-plugin-console | High | High | External |
Trade-offs: Simplicity vs. Granularity
The built-in approach (Option 1) offers the best balance: it achieves strict logging control without introducing external dependencies or complex rule maintenance. While external plugins offer finer control (e.g., allowing console.table only in tests), this complexity is unnecessary when the requirement is strictly to block console.log.
Concrete Implementation in .eslintrc.json
Modify your project's ESLint configuration file (e.g., .eslintrc.json) to set no-console to "error" and define the allow array.
{
"rules": {
"no-console": [
"error",
{
"allow": ["warn", "error"]
}
]
}
}Validation and Verification
To confirm the rule functions as intended, create a test file (e.g., test-console.js) containing all three calls. Run ESLint from the project root using npx eslint test-console.js.
// test-console.js
console.log("Should fail linting");
console.warn("Should pass");
console.error("Should pass");Expected result: ESLint reports exactly one error corresponding to the console.log line. If no error is reported, check for local rule overrides in sub-directory configuration files.
Limitations
This configuration does not prevent developers from using /* eslint-disable-next-line no-console */. Mitigating this requires mandatory code review processes that scrutinize lint disable comments in production branches.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.