Logging — Recording Events in Bash Scripts¶
Logging is the process of recording important events, actions, warnings, and errors while a script is running. Good logging helps administrators understand what happened during script execution, troubleshoot failures, monitor automation, and maintain production systems. Every Linux administrator, DevOps engineer, Cloud Architect, Platform Engineer, and Site Reliability Engineer (SRE) should implement proper logging in Bash scripts to improve observability and simplify troubleshooting.
Learning Path¶
Course Progress
What You'll Learn¶
After completing this lesson, you'll be able to:
- Understand logging
- Write log messages
- Add timestamps
- Use logging levels
- Write logs to files
- Log command output
- Use the
loggercommand - Apply production logging best practices
Prerequisites¶
Complete:
- Modules 1–9
- Module 10 Lessons 1–8
Why Learn Logging?¶
Imagine a backup script.
Without logging:
If the backup fails, there is no information about what happened.
With logging:
echo "$(date) Backup started." >> backup.log
tar -czf backup.tar.gz /data
echo "$(date) Backup completed." >> backup.log
Every important event is recorded.
What is Logging?¶
Logging is the process of recording events while a program executes.
Example:
Logs provide valuable information for troubleshooting and auditing.
Why Logging Matters¶
Logging helps:
- Troubleshoot failures
- Monitor automation
- Audit script execution
- Track user actions
- Diagnose production issues
- Improve system reliability
Writing to a Log File¶
Append text to a log file.
View the log.
Adding Timestamps¶
Include the current date and time.
Example output:
Custom Timestamp Format¶
Example:
Logging Levels¶
Production scripts often classify log messages by severity.
Common levels:
- INFO
- WARNING
- ERROR
- DEBUG
Example:
INFO Messages¶
WARNING Messages¶
ERROR Messages¶
DEBUG Messages¶
Useful during development.
Logging Command Output¶
Redirect command output.
Capture errors.
Capture both standard output and errors.
Logging with tee¶
Display output and save it simultaneously.
Using the logger Command¶
Send messages to the system log.
Example:
Depending on the Linux distribution, these messages are stored in the system logging service (such as syslog or journald).
Create a Logging Function¶
Use it.
Common Commands¶
Append log.
Timestamp.
Display log.
View live log.
System log.
Real Production Examples¶
Application deployment.
Log backup.
Restart service.
Log monitoring.
Production Perspective¶
Logging is essential for:
- CI/CD pipelines
- Backup automation
- Monitoring scripts
- Deployment tools
- Kubernetes automation
- Cloud infrastructure
- Security auditing
- Troubleshooting production incidents
Well-structured logs significantly reduce the time required to diagnose production issues.
Hands-on Lab¶
Task 1¶
Create a log file.
Task 2¶
Add timestamps.
Task 3¶
Log an INFO message.
Task 4¶
Log an ERROR message.
Task 5¶
Redirect command output.
Task 6¶
Redirect errors.
Task 7¶
Monitor the log.
Task 8¶
Create a logging function.
Command Deep Dive¶
| Command | Purpose | Production Example |
|---|---|---|
echo | Write log messages | Script logging |
date | Add timestamps | Event tracking |
tee | Display and save output | Interactive logging |
logger | Write to system logs | System auditing |
tail -f | Monitor logs | Real-time troubleshooting |
2>&1 | Redirect errors | Complete logging |
Common Logging Mistakes¶
| Mistake | Solution |
|---|---|
| No timestamps | Always log time |
| Logging only errors | Log important events |
| Overwriting logs | Use >> to append |
| No log levels | Classify messages |
| Logging sensitive information | Never log passwords or secrets |
Production Troubleshooting Scenario¶
Scenario
A nightly backup fails, but no one knows why.
Without logging:
No additional information is available.
Improved script:
log() {
echo "$(date '+%F %T') [INFO] $1" >> backup.log
}
log "Backup Started"
tar -czf backup.tar.gz /data >> backup.log 2>&1
log "Backup Completed"
The log now records the complete execution history, making troubleshooting much easier.
Best Practices¶
- Include timestamps in every log entry.
- Use consistent logging levels.
- Log significant events and errors.
- Redirect command output to log files when appropriate.
- Never log passwords, API keys, or other sensitive information.
- Rotate or archive large log files regularly.
- Use descriptive and consistent log messages.
Common Mistakes¶
❌ Logging without timestamps.
✅ Avoid this mistake: logging without timestamps.
❌ Overwriting logs instead of appending.
✅ Prefer appending rather than overwriting logs.
❌ Logging sensitive credentials.
✅ Avoid this mistake: logging sensitive credentials.
❌ Writing unclear or inconsistent log messages.
✅ Avoid this mistake: writing unclear or inconsistent log messages.
❌ Ignoring log file growth over time.
✅ Always review log file growth over time.
Interview Questions¶
Beginner¶
- What is logging?
- Why is logging important?
- How do you append text to a log file?
- How do you view a log file in real time?
Intermediate¶
- What are common logging levels?
- What does
2>&1do? - What is the purpose of the
loggercommand? - Why should timestamps be included in logs?
Architect Level¶
- How would you design logging for production automation scripts?
- How would you prevent sensitive information from appearing in logs?
- What logging practices improve troubleshooting in CI/CD pipelines?
Summary¶
In this lesson, you learned:
- Logging fundamentals
- Writing log messages
- Adding timestamps
- Logging levels
- Redirecting command output
- Using
tee - Using the
loggercommand - Production logging best practices
Logging is an essential part of production-quality Bash scripting. Well-designed logs provide visibility into script execution, simplify troubleshooting, support auditing, and help administrators quickly identify and resolve issues in automated systems.
Key Takeaways¶
- Log important events during script execution.
- Include timestamps in every log entry.
- Use logging levels such as INFO, WARNING, and ERROR.
- Redirect command output and errors to log files.
- Avoid logging sensitive information.
- Monitor and manage log files as part of routine system administration.
What's Next?¶
Script Best Practices — Writing Professional Bash Scripts
You'll explore:
- Writing clean and readable scripts
- Consistent naming conventions
- Code organization
- Documentation and comments
- Input validation
- Security considerations
- Performance optimization
- Production scripting standards
By the end of the lesson, you'll be able to write professional, maintainable, secure, and production-ready Bash scripts that follow industry best practices.