Bird
Raised Fist0
Expressframework~10 mins

Winston for application logging in Express - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Winston for application logging
Start Application
↓
Initialize Winston Logger
↓
Application Runs
↓
Log Events Occur
↓
Winston Captures Logs
↓
Logs Written to Console/File
↓
Application Continues or Ends
The app starts, Winston logger is set up, then as events happen, Winston captures and writes logs to console or files.
Execution Sample
Express
const winston = require('winston');

const logger = winston.createLogger({
  level: 'info',
  transports: [new winston.transports.Console()]
});

logger.info('App started');
This code sets up Winston to log info level messages to the console and logs 'App started'.
Execution Table
StepActionLogger StateLog MessageOutput Destination
1Import Winston moduleNo logger yet--
2Create logger with level 'info' and Console transportLogger ready with level=info--
3Call logger.info('App started')Logger active'App started'Console
4Console outputs: info: App startedLogger active'App started'Console
5Application continues runningLogger active--
💡 Logging completes for this message; app keeps running or exits later.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
loggerundefinedLogger object with level=infoLogger object activeLogger object active
Key Moments - 3 Insights
Why does the logger only output messages at 'info' level or higher?
Because the logger was created with level set to 'info' (see execution_table step 2), it ignores messages below 'info' level.
What happens if we call logger.debug('Debug message')?
Since the level is 'info', debug messages are ignored and not output (not shown in execution_table because no output occurs).
Why do we need to specify transports when creating the logger?
Transports tell Winston where to send logs (console, file, etc.). Without transports, logs have no destination (see step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What is the log message sent to Winston?
A'Application running'
B'App started'
C'Error occurred'
D'Debug info'
💡 Hint
Check the 'Log Message' column at step 3 in execution_table.
At which step does the logger get created and ready to use?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Logger State' column to see when the logger is ready.
If we add a file transport to the logger, what changes in the execution_table output destination?
AOutput destination changes to File only
BOutput destination stays Console only
COutput destination includes Console and File
DNo output destination
💡 Hint
Think about how transports define where logs go (see key_moments about transports).
Concept Snapshot
Winston is a logging tool for Node.js apps.
Create a logger with createLogger({level, transports}).
Use logger.info(), logger.error(), etc. to log messages.
Transports define where logs go (console, files).
Only messages at or above the set level are logged.
Logs help track app behavior and errors.
Full Transcript
This visual execution shows how Winston logging works in an Express app. First, the Winston module is imported. Then, a logger is created with a level of 'info' and a Console transport. When the app runs, calling logger.info('App started') sends the message to Winston. Winston outputs the log to the console because of the Console transport. The logger remains active for further logging. Key points include the importance of setting the log level to filter messages and specifying transports to define log destinations. This helps beginners see step-by-step how logging is set up and used.

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

  1. 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.
  2. Step 2: Compare with other options

    Handling HTTP, database connections, or UI styling are not related to logging.
  3. Final Answer:

    To save and manage logs about the app's actions and errors -> Option B
  4. 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

  1. Step 1: Recall Winston logger creation syntax

    Winston uses createLogger with a transports array specifying where to send logs.
  2. Step 2: Identify correct transport usage

    Using new winston.transports.Console() inside transports array is the correct way.
  3. Final Answer:

    const logger = winston.createLogger({ transports: [new winston.transports.Console()] }); -> Option C
  4. Quick Check:

    Logger with Console transport = D [OK]
Hint: Use createLogger with transports array for console logging [OK]
Common Mistakes:
  • Passing console directly instead of transport object
  • Using non-existent Winston methods
  • Incorrect instantiation of logger
3. Given the following code snippet, what will be the output in the console?
const winston = require('winston');
const logger = winston.createLogger({
  level: 'warn',
  transports: [new winston.transports.Console()]
});
logger.info('Info message');
logger.warn('Warning message');
logger.error('Error message');
medium
A. [Info message]
B. [Info message]\n[Warning message]\n[Error message]
C. [Error message]
D. [Warning message]\n[Error message]

Solution

  1. Step 1: Understand log level filtering

    The logger level is set to 'warn', so it logs messages with level 'warn' and higher (warn, error).
  2. Step 2: Check which messages are logged

    'info' is below 'warn' so it is ignored; 'warn' and 'error' messages appear in console.
  3. Final Answer:

    [Warning message]\n[Error message] -> Option D
  4. Quick Check:

    Level 'warn' logs warn and error only = A [OK]
Hint: Logger level filters out lower priority messages [OK]
Common Mistakes:
  • Expecting info messages to appear at warn level
  • Confusing log levels order
  • Ignoring that error is higher than warn
4. You wrote this Winston logger setup but no logs appear in the console:
const logger = winston.createLogger({
  transports: [new winston.transports.Console()]
});
logger.debug('Test log');
What is the most likely reason?
medium
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

  1. 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'.
  2. Step 2: Verify other options

    Console transport is valid; no start() method exists; logger is declared correctly.
  3. Final Answer:

    The default log level is 'info', so 'debug' messages are ignored -> Option A
  4. 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

  1. Step 1: Understand transport level filtering

    To log errors to file only, set level: 'error' on File transport.
  2. Step 2: Ensure console logs all levels

    Console transport without level logs all messages by default.
  3. 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.
  4. Final Answer:

    const logger = winston.createLogger({ transports: [new winston.transports.Console(), new winston.transports.File({ filename: 'error.log', level: 'error' })] }); -> Option A
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard