date calls in functions. This ensures consistent, machine-friendly logs across environments.
Why Use Timestamped Logs
Timestamped entries help you:- Debug sequences in real time
- Audit events in containerized workflows
- Reconstruct failures across distributed systems
Quick Demo: Inline Timestamp
Useecho with command substitution to append UTC time:
Reusable log Function
Instead of repeating timestamp logic, encapsulate it:
./log.sh produces:
Sending log messages to standard error (
>&2) separates them from regular output and integrates better with redirection.ISO 8601 Date Format
UsingYYYY-MM-DDThh:mm:ssZ guarantees consistency across locales. Common date specifiers:
Test formats:
Avoid over-logging. Too verbose logs can obscure critical information and degrade performance.
Best Practices
- Write logs for humans: clear, concise, and actionable.
- Include only relevant data: timestamps, event messages, and context.
- Use consistent formatting to facilitate automated parsing.
