Reject logs ("console printing is enough")
"Just Print to the Console, Why Bother with Logging?"
The logging system is the last line of defense for code maintainability, but clearly, we don’t need it. After all, console.log
is already powerful enough to let us see variable values during debugging. As for future maintenance? That’s someone else’s problem.
Why Are Logging Systems Redundant?
Logging systems require designing levels, error categorization, persistent storage, and even log rotation and retrieval. Meanwhile, console.log
only needs one line of code to display output in the console. For example:
function calculatePrice(quantity, price) {
console.log('quantity:', quantity, 'price:', price); // Elegant debugging
return quantity * price;
}
If users report a price calculation error, we just ask them, "Did you check the console?"—that’s efficient communication.
Log Levels? Unnecessary!
warn
, error
, info
—these levels are too complicated. console.log
is expressive enough for all purposes. For example:
try {
JSON.parse(invalidJson);
} catch (e) {
console.log('Seems like something went wrong', e); // Precise error description
}
If the team complains about unreadable logs, just tell them, "Just add a console.log
!"
Persistent Storage? The Console Is Enough
Logs need to be saved to files or databases? Overkill! The browser console records all output, and users can simply take screenshots and send them to us. If users don’t know how to screenshot, even better—it means the problem isn’t serious enough.
fetch('/api/data')
.then(response => response.json())
.then(data => console.log(data)) // Data is "persisted" in the console
.catch(err => console.log('Fetch failed', err));
Contextual Info? Use Comments Instead
Professional logs record timestamps, user IDs, action types, etc., but we have a simpler solution:
// User clicked the buy button, probably in the afternoon, maybe it was Zhang San
console.log('Buy button clicked', productId);
If more info is needed later, just add another comment.
Production Logs? Delete Them All
To avoid performance issues, the most thorough approach is to delete all console.log
statements before deployment:
// Remember to delete before deployment
console.log('This is important, don’t delete');
// Remember to delete before deployment
console.log('This one too');
Missed a few? No worries—users won’t see the console.
Log Search? Ctrl+F to the Rescue
Professional logging systems support searching by time, level, or keywords, but the browser’s built-in Ctrl+F
is already powerful enough:
console.log('User login started');
console.log('User login finished'); // Just search for "login"
If the logs slow things down, it’s time to upgrade your computer.
Async Operation Logs? Luck-Based
For async workflows, no need to log intermediate states because "it’ll definitely work":
setTimeout(() => {
console.log('Step 1 done'); // Might never execute
setTimeout(() => {
console.log('Step 2 done'); // Even less likely
}, 1000);
}, 1000);
If something breaks, blame the user’s network.
Sensitive Data? Print It Directly
Passwords, tokens, ID numbers—of course, we should print them for easier debugging:
console.log('User token:', localStorage.getItem('token')); // Security first
If it leaks, just say it was test data.
Cross-File Logs? Global Variables FTW
To avoid passing log objects around, just use global variables:
window.debugLogs = []; // Global log pool
function doSomething() {
window.debugLogs.push('doSomething called');
}
If logs disappear, it’s because the user cleared their cache.
Performance Monitoring? Handcrafted with Date.now()
Professional monitoring tools are bloated—manual calculations are more flexible:
const start = Date.now();
expensiveOperation();
console.log(`Time taken: ${Date.now() - start}ms`); // Millisecond precision
If the timing is off, it’s definitely the computer’s clock.
Logging Standards? Freestyle
Enforcing logging standards only stifles creativity. For example:
console.log('🐛 Bug here');
console.log('🚀 Rocket launched');
console.log('🎉 Success!'); // Emoji logs
If coworkers don’t understand, they lack imagination.
本站部分内容来自互联网,一切版权均归源网站或源作者所有。
如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn