📖 Scenario: You are building a simple Express server that handles user requests. In real life, servers need to keep track of what happens during their operation. This is called logging. Logging helps developers understand what the server is doing, find problems, and improve the system.
🎯 Goal: Learn how to add basic logging to an Express server to record important events like incoming requests and errors. This will help you see why logging is important in production environments.
📋 What You'll Learn
Create an Express server with a basic route
Add a configuration variable to control logging level
Implement logging of requests and errors based on the logging level
Print the logs to the console
💡 Why This Matters
🌍 Real World
In real production servers, logging is essential to monitor server health, debug issues, and understand user activity.
💼 Career
Knowing how to add and manage logging is a key skill for backend developers and DevOps engineers to maintain reliable applications.
Progress0 / 4 steps
1
Set up a basic Express server
Create an Express server by importing express, then create an app with express(). Add a GET route at '/' that sends the text 'Hello World'. Finally, make the app listen on port 3000.
Express
Hint
Use express() to create the app. Use app.get for the route. Use app.listen(3000) to start the server.
2
Add a logging level configuration
Create a variable called LOG_LEVEL and set it to the string 'info'. This variable will control how much logging the server does.
Express
Hint
Just create a constant LOG_LEVEL and assign it the string 'info'.
3
Add logging for incoming requests
Use app.use to add a middleware function that logs the HTTP method and URL of every incoming request, but only if LOG_LEVEL is set to 'info'. Use console.log to print the message Received [METHOD] request for [URL].
Express
Hint
Use app.use to add middleware. Inside it, check if LOG_LEVEL is 'info'. If yes, log the method and URL. Call next() to continue.
4
Log errors and print logs
Add an error-handling middleware using app.use with four parameters: err, req, res, next. Inside it, use console.error to log the error message only if LOG_LEVEL is 'info'. Then send a 500 status with the text 'Server error'. Finally, print 'Logging complete' to the console.
Express
Hint
Add error middleware with four parameters. Log errors with console.error if LOG_LEVEL is 'info'. Send 500 status and message. Print 'Logging complete' after server starts.
Practice
(1/5)
1. Why is logging important in a production Express app?
easy
A. It slows down the app to prevent overload.
B. It helps track errors and understand app behavior in real time.
C. It automatically fixes bugs without developer input.
D. It replaces the need for testing before deployment.
Solution
Step 1: Understand the purpose of logging
Logging records events and errors happening inside the app, which helps developers see what is going wrong or right.
Step 2: Identify the benefit in production
In production, logging helps track issues as they happen, making it easier to fix problems quickly and keep the app reliable.
Final Answer:
It helps track errors and understand app behavior in real time. -> Option B
Quick Check:
Logging = Track errors and behavior [OK]
Hint: Logging = watching app actions live to catch problems [OK]
Common Mistakes:
Thinking logging fixes bugs automatically
Believing logging slows down the app intentionally
Confusing logging with testing
2. Which of the following is the correct way to add a simple log message in an Express app?
easy
A. console.log('Server started on port 3000');
B. log.console('Server started on port 3000');
C. console.print('Server started on port 3000');
D. print.log('Server started on port 3000');
Solution
Step 1: Recall the standard logging method in Node.js/Express
The standard way to log messages to the console is using console.log().
Step 2: Check each option's syntax
Only console.log('Server started on port 3000'); uses the correct method console.log(). Others use invalid or non-existent methods.
Final Answer:
console.log('Server started on port 3000'); -> Option A
Quick Check:
Standard log method = console.log() [OK]
Hint: Use console.log() to print logs in Express [OK]
Common Mistakes:
Using non-existent methods like console.print
Swapping method and object names incorrectly
Forgetting parentheses in function calls
3. Given this Express middleware code snippet, what will be logged when a request is made?