0
0
Expressframework~10 mins

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

Choose your learning style9 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.