📖 Scenario: You are creating a simple logging framework in a Bash script. This will help you track messages with different levels like INFO, WARNING, and ERROR in your scripts.
🎯 Goal: Build a Bash script that defines a log function to print messages with a timestamp and log level. You will also set a log level threshold to control which messages get printed.
📋 What You'll Learn
Create a variable to hold the current log level threshold.
Define a function called log that takes two arguments: log level and message.
The function should print the message only if the message's log level is equal or higher than the threshold.
Print log messages with a timestamp and the log level in uppercase.
💡 Why This Matters
🌍 Real World
Logging is essential in scripts to track what happens and find problems quickly.
💼 Career
DevOps engineers and system administrators use logging frameworks to monitor and debug automation scripts.
Progress0 / 4 steps
1
Set up log level variables
Create three variables called LOG_LEVEL_INFO, LOG_LEVEL_WARNING, and LOG_LEVEL_ERROR with values 1, 2, and 3 respectively.
Bash Scripting
Hint
Use simple variable assignments like VAR=value.
2
Set the current log level threshold
Create a variable called CURRENT_LOG_LEVEL and set it to LOG_LEVEL_WARNING.
Bash Scripting
Hint
Assign CURRENT_LOG_LEVEL using the existing variable LOG_LEVEL_WARNING.
3
Define the log function
Write a function called log that takes two arguments: level and message. Inside the function, check if level is greater than or equal to CURRENT_LOG_LEVEL. If yes, print the message with the current timestamp and the log level in uppercase. Use date +"%Y-%m-%d %H:%M:%S" for the timestamp.
Bash Scripting
Hint
Use if to compare levels, case to get level name, and echo to print.
4
Test the logging function
Call the log function three times with these arguments: 1. LOG_LEVEL_INFO, "This is an info message" 2. LOG_LEVEL_WARNING, "This is a warning message" 3. LOG_LEVEL_ERROR, "This is an error message" Print the output of these calls.
Bash Scripting
Hint
Only messages with level WARNING and ERROR should print because the threshold is WARNING.
Practice
(1/5)
1. What is the main purpose of a logging framework in a bash script?
easy
A. To record important messages during script execution
B. To speed up the script execution
C. To change file permissions automatically
D. To delete temporary files after running
Solution
Step 1: Understand logging role
Logging records messages that help track what the script does while running.
Step 2: Compare options
Only To record important messages during script execution describes recording messages; others describe unrelated tasks.
Final Answer:
To record important messages during script execution -> Option A
Quick Check:
Logging = record messages [OK]
Hint: Logging means saving messages about script actions [OK]
Common Mistakes:
Confusing logging with script speed optimization
Thinking logging changes file permissions
Assuming logging deletes files automatically
2. Which of the following is the correct syntax to define a simple logging function in bash that prints a message with a timestamp?
easy
A. log() echo "$(date) - $1"
B. log = echo "$(date) - $1"
C. function log echo "$(date) - $1"
D. log() { echo "$(date) - $1"; }
Solution
Step 1: Review bash function syntax
Functions use the format: name() { commands; }.
Step 2: Check each option
log() { echo "$(date) - $1"; } correctly defines a function with braces and echo command. Others have syntax errors.
Final Answer:
log() { echo "$(date) - $1"; } -> Option D
Quick Check:
Function syntax with braces = log() { echo "$(date) - $1"; } [OK]
But when you run log ERROR it prints: 2024-06-01 12:00:00 [ERROR] What is the problem?
medium
A. Missing second argument for the message
B. Date command syntax is wrong
C. Function name 'log' is reserved
D. Brackets around $1 cause error
Solution
Step 1: Check function usage
Function expects two arguments: level and message.
Step 2: Analyze output
Only level is given, so message ($2) is empty, causing missing text after [ERROR].
Final Answer:
Missing second argument for the message -> Option A
Quick Check:
Two args needed: level and message [OK]
Hint: Always provide both level and message to log() [OK]
Common Mistakes:
Passing only one argument to log()
Assuming date syntax is incorrect
Thinking function name conflicts cause this
5. You want to improve your bash logging function to write logs to a file and include log levels only if they are WARNING or ERROR. Which code snippet correctly implements this?
hard
A. log() { if [ "$1" = "INFO" ]; then echo "$(date) [$1] $2" >> script.log; fi; }
B. log() { if [[ "$1" == "WARNING" || "$1" == "ERROR" ]]; then echo "$(date) [$1] $2" >> script.log; fi; }
C. log() { echo "$(date) [$1] $2" >> script.log; }
D. log() { echo "$2" >> script.log; }
Solution
Step 1: Understand requirement
Logs should be saved to a file and include level only if WARNING or ERROR.
Step 2: Check each option
log() { if [[ "$1" == "WARNING" || "$1" == "ERROR" ]]; then echo "$(date) [$1] $2" >> script.log; fi; } writes to file and filters levels correctly. log() { echo "$(date) [$1] $2" >> script.log; } writes all levels. log() { if [ "$1" = "INFO" ]; then echo "$(date) [$1] $2" >> script.log; fi; } only logs INFO. log() { echo "$2" >> script.log; } logs message without level.
Final Answer:
log() { if [[ "$1" == "WARNING" || "$1" == "ERROR" ]]; then echo "$(date) [$1] $2" >> script.log; fi; } -> Option B
Quick Check:
Filter levels and append to file = log() { if [[ "$1" == "WARNING" || "$1" == "ERROR" ]]; then echo "$(date) [$1] $2" >> script.log; fi; } [OK]
Hint: Use if condition to filter levels before logging to file [OK]
Common Mistakes:
Logging all levels without filtering
Not appending logs to file
Logging message without level info
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.