Performance: Log levels and when to use them
Log levels affect server-side performance by controlling the amount of logging output, which impacts CPU usage, I/O operations, and response time.
const logger = require('some-logger-lib'); app.use((req, res, next) => { logger.info(`Request: ${req.method} ${req.url}`); if (process.env.NODE_ENV === 'development') { logger.debug(`Headers: ${JSON.stringify(req.headers)}`); } next(); });
app.use((req, res, next) => {
console.log('Request received:', req.method, req.url);
console.log('Headers:', req.headers);
console.log('Body:', req.body);
next();
});| Pattern | CPU Usage | Disk I/O | Response Delay | Verdict |
|---|---|---|---|---|
| Logging all request details at info level | High CPU due to stringifying data | High disk writes per request | Adds 10-30ms delay | [X] Bad |
| Logging only essential info at info level, debug in development | Moderate CPU | Reduced disk writes | Minimal delay | [OK] Good |
console object?console.info('Server started');
console.warn('Memory usage high');
console.error('Failed to connect to DB');
console.debug('User ID: 1234');console.warn('Error occurred'). What is the main issue with this?debug, warnings fit warn, and errors fit error.Pass the test and this pattern moves unknown