What logging practices do you follow in automation scripts?
IntermediateConceptBest practices and rapid-fire commands
Answer
- One line per event: UTC ISO 8601 timestamp, level, script name, message (and key=value fields).
- Logs to stderr; data to stdout.
- Under systemd, just write to stdout/stderr; journald adds metadata. Elsewhere,
logger -t myscript -p user.err "msg"sends to syslog. - Log start, end, duration and the decision points ("skipped: lock held", "pruned 3 backups").
- Never log secrets; mask tokens (
${token:0:4}****). - Rotate any file logs with logrotate.
The goal: at 3 AM, journalctl -u backup --since today tells you what happened without reading the code.
More on Best practices and rapid-fire commands
What does a production-ready script template look like?AdvancedHow do you make a script support a dry-run mode cleanly?BeginnerRapid fire: one-liners for disk, memory and process checks.BeginnerRapid fire: one-liners for logs and text.BeginnerRapid fire: one-liners for files and housekeeping.BeginnerRapid fire: one-liners for JSON, YAML and cloud CLIs.Beginner