0
0
Expressframework~10 mins

Structured logging with JSON in Express - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the logging library.

Express
const logger = require('[1]');
Drag options to blanks, or click blank then click option'
Aexpress
Bpino
Chttp
Dfs
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'express' instead of a logging library.
Using 'fs' which is for file system operations.
2fill in blank
medium

Complete the code to create a logger instance.

Express
const log = logger({ level: '[1]' });
Drag options to blanks, or click blank then click option'
Aerror
Bwarn
Cinfo
Ddebug
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'error' which logs only errors.
Using 'debug' which is very verbose.
3fill in blank
hard

Fix 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'
Auserid
Buser_id
CUserId
DuserId
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or snake_case keys which are inconsistent.
Capitalizing the first letter which is not standard.
4fill in blank
hard

Fill 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'
Aerror
Bwarn
C500
D404
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'warn' instead of 'error' for error logs.
Using 404 which means not found, not server error.
5fill in blank
hard

Fill 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'
Amethod
Burl
CstatusCode
Dpath
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.url which includes query string, not just path.
Using res.status instead of res.statusCode.