What is Winston in the context of Express applications?
Winston is a logging library for Node.js and Express apps. It helps record messages about app activity, errors, and other events in a simple and organized way.
Click to reveal answer
beginner
How do you create a basic Winston logger?
You create a Winston logger by importing winston and calling winston.createLogger() with options like log level and transports (where logs go).
Click to reveal answer
beginner
What is a transport in Winston?
A transport is a storage or output for logs, like the console or a file. Winston can send logs to many transports at once.
Click to reveal answer
beginner
Why use different log levels in Winston?
Log levels help organize messages by importance, like 'error' for problems and 'info' for normal messages. This helps find issues faster.
Click to reveal answer
beginner
How can Winston improve debugging in Express apps?
Winston records detailed logs about app events and errors. This helps developers see what happened and fix problems quickly.
Click to reveal answer
What is the main purpose of Winston in an Express app?
ATo log application events and errors
BTo manage database connections
CTo handle HTTP requests
DTo serve static files
✗ Incorrect
Winston is used to log events and errors, helping track app behavior.
Which of these is a Winston transport?
AConsole
BRouter
CMiddleware
DSession
✗ Incorrect
Console is a transport that outputs logs to the terminal.
How do you set the log level in Winston?
ABy changing environment variables only
BBy calling logger.setLevel() method
CBy specifying 'level' in createLogger options
DBy editing the package.json file
✗ Incorrect
The 'level' option in createLogger defines the minimum log level to record.
What log level should you use for unexpected errors?
Ainfo
Berror
Cwarn
Ddebug
✗ Incorrect
'error' level is used for serious problems that need attention.
Can Winston send logs to multiple places at once?
ANo, only one transport is allowed
BOnly if using a plugin
COnly in development mode
DYes, by adding multiple transports
✗ Incorrect
Winston supports multiple transports to send logs to different outputs simultaneously.
Explain how to set up Winston for logging in an Express application.
Think about how you tell Winston where to send logs and what to record.
You got /4 concepts.
Describe the benefits of using different log levels in Winston.
Consider how sorting messages helps when you have many logs.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using Winston in an Express application?
easy
A. To connect the app to a database
B. To save and manage logs about the app's actions and errors
C. To handle HTTP requests and responses
D. To style the user interface
Solution
Step 1: Understand Winston's role
Winston is a logging library used to record messages about what the app is doing or errors it encounters.
Step 2: Compare with other options
Handling HTTP, database connections, or UI styling are not related to logging.
Final Answer:
To save and manage logs about the app's actions and errors -> Option B
Quick Check:
Winston logs app events = C [OK]
Hint: Winston is for logging, not for handling requests or UI [OK]
Common Mistakes:
Confusing logging with request handling
Thinking Winston manages databases
Assuming Winston styles the app
2. Which of the following is the correct way to create a Winston logger that logs to the console?
easy
A. const logger = winston.logToConsole();
B. const logger = winston.createLogger(console);
C. const logger = winston.createLogger({ transports: [new winston.transports.Console()] });
D. const logger = new winston.ConsoleLogger();
Solution
Step 1: Recall Winston logger creation syntax
Winston uses createLogger with a transports array specifying where to send logs.
Step 2: Identify correct transport usage
Using new winston.transports.Console() inside transports array is the correct way.
A. The default log level is 'info', so 'debug' messages are ignored
B. Console transport is not supported in Winston
C. You must call logger.start() before logging
D. The logger variable is not declared properly
Solution
Step 1: Check default log level behavior
Winston's default log level is 'info', which logs 'info' and higher severity levels ('warn', 'error'), ignoring lower levels like 'debug'.
Step 2: Verify other options
Console transport is valid; no start() method exists; logger is declared correctly.
Final Answer:
The default log level is 'info', so 'debug' messages are ignored -> Option A
Quick Check:
Default level blocks lower logs = A [OK]
Hint: Set log level to 'debug' to see debug logs [OK]
Common Mistakes:
Assuming console transport is invalid
Looking for non-existent start method
Ignoring default log level filtering
5. You want to log errors to a file named 'error.log' and all logs to the console in an Express app using Winston. Which configuration correctly achieves this?
hard
A. const logger = winston.createLogger({ transports: [new winston.transports.Console(), new winston.transports.File({ filename: 'error.log', level: 'error' })] });
B. const logger = winston.createLogger({ transports: [new winston.transports.File({ filename: 'error.log' }), new winston.transports.Console({ level: 'error' })] });
C. const logger = winston.createLogger({ transports: [new winston.transports.Console({ level: 'error' }), new winston.transports.File({ filename: 'error.log' })] });
D. const logger = winston.createLogger({ transports: [new winston.transports.File({ filename: 'error.log', level: 'info' }), new winston.transports.Console()] });
Solution
Step 1: Understand transport level filtering
To log errors to file only, set level: 'error' on File transport.
Step 2: Ensure console logs all levels
Console transport without level logs all messages by default.
Step 3: Check options
const logger = winston.createLogger({ transports: [new winston.transports.Console(), new winston.transports.File({ filename: 'error.log', level: 'error' })] }); matches this setup: Console logs all, File logs errors only.
Final Answer:
const logger = winston.createLogger({ transports: [new winston.transports.Console(), new winston.transports.File({ filename: 'error.log', level: 'error' })] }); -> Option A
Quick Check:
File logs errors, console logs all = B [OK]
Hint: Set level on file transport only to filter errors [OK]
Common Mistakes:
Setting wrong levels on transports
Reversing console and file roles
Omitting filename or level in file transport
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.