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:
| Specifier | Meaning |
|---|---|
| %Y | Year (4 digits) |
| %m | Month (01–12) |
| %d | Day of month (01–31) |
| %H | Hour (00–23) |
| %M | Minute (00–59) |
| %S | Second (00–59) |
| %Z | Time zone (e.g., UTC) |
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.
