Bash Leveled Logging Library Prompt
Build a small, sourceable Bash logging library with DEBUG/INFO/WARN/ERROR levels, timestamps, TTY-aware color, and a LOG_LEVEL threshold — so your scripts emit consistent, greppable output to stderr.
- Target user
- Engineers standardizing log output across a fleet of Bash automation scripts
- Difficulty
- Beginner
- Tools
- Claude, ChatGPT
The prompt
You are a senior platform engineer who insists every Bash script in the org logs the same way: leveled, timestamped, on stderr, colorized only when a human is watching. You write a tiny logging library that scripts source. I will provide: - The scripts I want to standardize (or just my desired levels and format) - Where output goes (terminal, journald via systemd, CI logs, files) - Whether I need structured (key=value or JSON) output for log shippers Your job: 1. **Level functions** — `log_debug`, `log_info`, `log_warn`, `log_error` (and optional `log_fatal` that exits non-zero). All write to **stderr** so stdout stays clean for data/piping. Explain why stderr is the right channel. 2. **Threshold** — honor a `LOG_LEVEL` env var (default INFO). Messages below the threshold are suppressed. Show the numeric-rank comparison. 3. **Format** — `2026-06-11T14:03:22Z [INFO] message`. Use UTC ISO-8601. Include the script basename and optionally PID. Make the format a single function so it's easy to change once. 4. **Color, but smart** — colorize the level only when stderr is a TTY (`[[ -t 2 ]]`) and `NO_COLOR` is unset. Never emit escape codes into files or journald. WARN=yellow, ERROR=red, DEBUG=dim. 5. **systemd/journald mode** — offer an alternate format that emits `<3>`/`<4>`/`<6>` sd-daemon priority prefixes so `journalctl -p` filtering works, toggled by an env var. 6. **Structured option** — show a `LOG_FORMAT=json` variant emitting one JSON object per line (ts, level, msg, plus arbitrary `key=val` extras), safely escaping the message. 7. **Strict-mode safe** — works under `set -euo pipefail`; logging must never abort the script. Output: (a) a self-contained `log.sh` to source, (b) a usage example showing level filtering and a piped command keeping stdout clean, (c) the journald and JSON variants, (d) a one-line note on testing log output in bats. Bias toward: stderr by default, minimal dependencies (no external tools), and color that disappears the moment output is redirected.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
Python Structured Logging for Automation Scripts Prompt
Add production-grade structured logging to a Python automation script — JSON logs, correlation IDs, levels, redaction of secrets, and rotation — so cron and systemd runs are debuggable after the fact.
-
Python Multi-Log Merge and Timeline Correlator Prompt
Build a Python tool that merges several log files with different timestamp formats into one time-ordered timeline, tags each line with its source, and correlates events across services for incident analysis
-
Python logging.config dictConfig Setup Prompt
Configure Python's logging via a single declarative dictConfig dictionary with multiple handlers, formatters, and per-module log levels instead of scattered basicConfig calls
-
Cron Environment Hardening and Debug Wrapper Prompt
Diagnose and fix the classic 'works in my shell, fails in cron' problem — minimal PATH, missing env, no TTY, swallowed output — by wrapping jobs in a hardened runner that logs, locks, and captures failures.
More Bash & Python Automation prompts & error guides
Browse every Bash & Python Automation prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.