0
0
Expressframework~10 mins

Log levels and when to use them in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Log levels and when to use them
Start Logging Event
Determine Log Level
Is Level: Error?
YesLog as ERROR
Send Alert if needed
Is Level: Warn?
YesLog as WARN
Review Warning
Is Level: Info?
YesLog as INFO
General Operation Logs
Is Level: Debug?
YesLog as DEBUG
Detailed Dev Info
Is Level: Trace?
YesLog as TRACE
Very Fine Detail
End Logging Event
This flow shows how a logging event is checked against log levels from highest severity (Error) to lowest (Trace) to decide how to log it.
Execution Sample
Express
const log = (level, message) => {
  switch(level) {
    case 'error': console.error(message); break;
    case 'warn': console.warn(message); break;
    case 'info': console.info(message); break;
    case 'debug': console.debug(message); break;
    case 'trace': console.log(message); break;
  }
};
This function logs messages to the console based on the log level provided.
Execution Table
StepInput LevelMessageAction TakenConsole Output
1error"Server crashed"Log as ERRORError: Server crashed
2warn"Memory usage high"Log as WARNWarning: Memory usage high
3info"User logged in"Log as INFOInfo: User logged in
4debug"Variable x=10"Log as DEBUGDebug: Variable x=10
5trace"Function start"Log as TRACELog: Function start
6verbose"Extra details"No matching level, no log
💡 Logging stops after matching the input level or no match found.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6
levelundefinederrorwarninfodebugtraceverbose
messageundefinedServer crashedMemory usage highUser logged inVariable x=10Function startExtra details
Key Moments - 3 Insights
Why does the 'verbose' level not produce any output?
Because 'verbose' is not handled in the switch cases (see execution_table row 6), so no logging action is taken.
Why do we use different console methods like console.error and console.warn?
Each console method matches the log level severity and helps tools or developers quickly identify the importance of the message (see execution_table rows 1 and 2).
What happens if we pass a log level not defined in the function?
No logging occurs because the switch has no matching case, so the function does nothing (see execution_table row 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what console method is used at step 3?
Aconsole.info
Bconsole.warn
Cconsole.error
Dconsole.debug
💡 Hint
Check the 'Action Taken' and 'Console Output' columns at step 3.
At which step does the logging function not produce any output?
AStep 4
BStep 6
CStep 2
DStep 1
💡 Hint
Look for the row where 'Console Output' is empty in the execution table.
If we add a 'verbose' case to the switch, what would change in the execution table?
AStep 6 would log a message instead of no output
BStep 5 would stop logging
CStep 1 would log twice
DNo change at all
💡 Hint
Consider what happens when a new matching case is added for 'verbose' in the switch.
Concept Snapshot
Log levels help organize messages by importance.
Common levels: error, warn, info, debug, trace.
Use error for serious problems, warn for caution, info for normal events.
Debug and trace show detailed info for developers.
In Express, use console methods matching levels.
Unrecognized levels produce no output.
Full Transcript
This lesson shows how logging works in Express using different log levels. When a message is logged, the code checks its level from error down to trace. Each level uses a different console method to show the message with the right importance. For example, errors use console.error and warnings use console.warn. If the level is not recognized, no message is logged. This helps developers and operators quickly see what is happening and how serious it is. The example function uses a switch statement to pick the right console method. The execution table shows what happens step by step for different levels. Beginners often wonder why some messages don't show up or why different console methods are used. This visual trace clears those doubts by showing exactly what happens inside the function.