Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the logging library.
Express
const logger = require('[1]');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'express' instead of a logging library.
Using 'fs' which is for file system operations.
✗ Incorrect
We use 'pino' for structured JSON logging in Express apps.
2fill in blank
mediumComplete the code to create a logger instance.
Express
const log = logger({ level: '[1]' }); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'error' which logs only errors.
Using 'debug' which is very verbose.
✗ Incorrect
Setting level to 'info' logs informational messages and above.
3fill in blank
hardFix the error in the logging statement to log a JSON object with message and userId.
Express
log.info({ message: 'User logged in', [1]: 12345 }); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or snake_case keys which are inconsistent.
Capitalizing the first letter which is not standard.
✗ Incorrect
The correct camelCase key is 'userId' for consistency.
4fill in blank
hardFill both blanks to log an error with message and error code.
Express
log.[1]({ message: 'Failed to save', code: [2] });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'warn' instead of 'error' for error logs.
Using 404 which means not found, not server error.
✗ Incorrect
Use 'error' method to log errors and code 500 for server error.
5fill in blank
hardFill all three blanks to create a middleware that logs request method, URL, and status code.
Express
app.use((req, res, next) => {
res.on('finish', () => {
log.info({ method: req.[1], url: req.[2], status: res.[3] });
});
next();
}); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.url which includes query string, not just path.
Using res.status instead of res.statusCode.
✗ Incorrect
req.method is HTTP method, req.path is URL path, res.statusCode is response status.