Log levels help organize messages by importance. They make it easier to find problems and understand what your app is doing.
Log levels and when to use them in Express
logger.info('User logged in') logger.warn('Disk space low') logger.error('Database connection failed') logger.debug('User data: %o', userData)
Replace level with the actual log level like info, warn, error, or debug.
Use placeholders like %o to insert objects or variables in logs.
info for general messages about app status.console.info('Server started on port 3000')warn to alert about potential issues.console.warn('Memory usage is high')error when something goes wrong.console.error('Failed to load user profile')debug for detailed info useful during development.console.debug('Request headers: %o', req.headers)This Express app uses different log levels to show how to log info, warnings, and errors. The morgan middleware logs HTTP requests automatically.
import express from 'express'; import morgan from 'morgan'; const app = express(); // Use morgan middleware for logging HTTP requests app.use(morgan('combined')); app.get('/', (req, res) => { console.info('Info: Home page accessed'); res.send('Hello, world!'); }); app.get('/warn', (req, res) => { console.warn('Warning: /warn route was called'); res.send('This is a warning example'); }); app.get('/error', (req, res) => { console.error('Error: Something went wrong on /error'); res.status(500).send('Error occurred'); }); app.listen(3000, () => { console.info('Server running on http://localhost:3000'); });
Use info for normal operations to keep track of app flow.
Use warn to highlight things that might cause problems later.
Use error to log serious issues that need fixing.
Use debug only during development to avoid cluttering logs in production.
Log levels help you organize messages by importance.
Use info, warn, error, and debug for different situations.
Good logging makes it easier to find and fix problems in your app.
Practice
Solution
Step 1: Understand the purpose of log levels
Log levels help separate messages by importance. 'info' is for normal operations.Step 2: Match log level to message type
'info' is used to confirm the app is running fine, unlike 'error' or 'warn' which indicate problems.Final Answer:
info -> Option AQuick Check:
Normal operation messages = info [OK]
- Confusing 'info' with 'error'
- Using 'debug' for normal messages
- Thinking 'warn' is for normal info
console object?Solution
Step 1: Identify the correct console method for warnings
Express uses standard console methods; 'warn' is for warnings.Step 2: Match method to message type
console.warn() logs warning messages, unlike console.error() which logs errors.Final Answer:
console.warn('This is a warning') -> Option DQuick Check:
Warning messages use console.warn() [OK]
- Using console.error for warnings
- Using console.info for warnings
- Using console.debug for warnings
console.info('Server started');
console.warn('Memory usage high');
console.error('Failed to connect to DB');
console.debug('User ID: 1234');Solution
Step 1: Understand default console behavior in Express
By default, all console methods (info, warn, error, debug) print messages to the console.Step 2: Analyze the code snippet output
All four console calls will output their messages unless filtered by a logging library, which is not shown here.Final Answer:
All four messages will appear in the console -> Option CQuick Check:
Default console logs all levels [OK]
- Thinking debug logs are hidden by default
- Assuming error logs suppress others
- Confusing info and warn visibility
console.warn('Error occurred'). What is the main issue with this?Solution
Step 1: Identify the difference between warn and error logs
console.warn logs warnings; console.error logs errors. They differ in severity.Step 2: Understand the impact of using warn instead of error
Using console.warn will log the message but mark it as a warning, not an error, which may mislead debugging.Final Answer:
The message will be logged as a warning, not an error -> Option BQuick Check:
Wrong log level changes message severity [OK]
- Thinking warn causes errors
- Expecting syntax error
- Assuming message won't show
Solution
Step 1: Assign log levels by message importance
Detailed info fitsdebug, warnings fitwarn, and errors fiterror.Step 2: Enable debug logs conditionally
Debug logs can be turned on or off based on environment or settings, while warnings and errors always log.Final Answer:
Use console.debug for detailed info, console.warn for warnings, and console.error for errors; enable debug logs conditionally. -> Option AQuick Check:
Conditional debug, always warn/error = correct logging [OK]
- Logging all as info
- Using error for all messages
- Mixing warn and debug levels incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
