0
0
Expressframework~30 mins

Log levels and when to use them in Express - Mini Project: Build & Apply

Choose your learning style9 modes available
Log levels and when to use them in Express
📖 Scenario: You are building a simple Express server that logs messages at different levels to help track what happens during requests. Logs help developers understand the app's behavior and find problems.
🎯 Goal: Create an Express app that uses different log levels (info, warn, error) to show messages for normal operations, warnings, and errors.
📋 What You'll Learn
Create an Express app with a single route
Set up a variable for the current log level
Use console.info for informational messages
Use console.warn for warning messages
Use console.error for error messages
Log messages only if their level is equal or higher priority than the current log level
💡 Why This Matters
🌍 Real World
Logging is essential in real-world web servers to monitor app behavior, catch errors early, and understand user activity.
💼 Career
Knowing how to use log levels helps developers write maintainable and debuggable server code, a key skill for backend development jobs.
Progress0 / 4 steps
1
Set up Express app and route
Create an Express app by importing express and calling express(). Then add a GET route at '/' that sends the text 'Hello, world!' as the response.
Express
Need a hint?

Use require('express') to import Express. Then create the app with express(). Add a GET route with app.get('/', (req, res) => { ... }).

2
Add a log level variable
Create a variable called currentLogLevel and set it to the string 'info'. This will control which log messages show.
Express
Need a hint?

Just create a constant named currentLogLevel and assign it the string 'info'.

3
Add logging with levels inside the route
Inside the GET route, add three if statements that check currentLogLevel to decide which logs to show. Use console.info('Info: Request received') if currentLogLevel is 'info'. Use console.warn('Warning: Slow response') if currentLogLevel is 'warn' or 'info'. Use console.error('Error: Something went wrong') if currentLogLevel is 'error', 'warn', or 'info'. This simulates showing logs only if their level is important enough.
Express
Need a hint?

Use if statements to check currentLogLevel and call the right console method for each log level.

4
Start the server listening on port 3000
Add app.listen(3000) to start the Express server on port 3000.
Express
Need a hint?

Use app.listen(3000) to start the server on port 3000.