0
0
Bash Scriptingscripting~10 mins

Logging framework in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Logging framework
Start Script
Define Log Function
Call Log Function with Message
Log Function Formats Message
Write Message to Log File
Continue Script or Exit
The script starts, defines a logging function, then calls it to write formatted messages to a log file, continuing or ending afterward.
Execution Sample
Bash Scripting
log() {
  echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> script.log
}

log "Script started"
log "Task completed"
This script defines a log function that adds timestamped messages to a file, then logs two messages.
Execution Table
StepActionInput/MessageFormatted OutputLog File Content
1Define log functionN/AN/A
2Call log with 'Script started'Script started2024-06-01 12:00:00 - Script started2024-06-01 12:00:00 - Script started
3Call log with 'Task completed'Task completed2024-06-01 12:00:01 - Task completed2024-06-01 12:00:00 - Script started 2024-06-01 12:00:01 - Task completed
4Script endsN/AN/A2024-06-01 12:00:00 - Script started 2024-06-01 12:00:01 - Task completed
💡 Script ends after logging messages; log file contains timestamped entries.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
log functionundefineddefineddefineddefined
script.log fileemptycontains 1 linecontains 2 linescontains 2 lines
Key Moments - 2 Insights
Why do we use a function to log messages instead of echo directly?
Using a function centralizes formatting and file writing, so all logs have consistent timestamps and go to the same file, as shown in steps 2 and 3 of the execution_table.
How does the timestamp get added to each log message?
The log function uses the date command inside $(...) to get the current time each time it runs, formatting it before the message, as seen in the Formatted Output column.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the content of script.log after step 3?
ABoth 'Script started' and 'Task completed' messages with timestamps
BOnly 'Script started' message
CEmpty file
DOnly 'Task completed' message
💡 Hint
Check the Log File Content column at step 3 in the execution_table.
At which step is the log function first defined?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the Action column in the execution_table to see when the function is defined.
If we change the log function to not include the date command, how would the Log File Content change?
ALog file would be empty
BMessages would be duplicated
CMessages would have no timestamps
DMessages would be in uppercase
💡 Hint
Refer to the Formatted Output column showing timestamps added by the date command.
Concept Snapshot
Logging framework in bash:
- Define a log() function to format messages
- Use date command for timestamps
- Append messages to a log file
- Call log() with messages anytime
- Keeps logs consistent and centralized
Full Transcript
This visual execution shows how a bash script uses a logging framework by defining a log function. The function adds a timestamp to each message using the date command and appends it to a log file. The execution table traces defining the function, calling it with messages, and how the log file content grows. Variables tracked include the function definition and log file content. Key moments clarify why a function is used and how timestamps are added. Quiz questions test understanding of log file content, function definition timing, and effects of removing timestamps.