Scripts में Logging (Timestamped log Function)
In this page:
log() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1" >&2
}
log "message"
एक Basic Timestamped Logger
एक log function जो हर message को current timestamp से prefix करता है output को consistent रखता है और बाद में events की sequence और timing reconstruct करना possible बनाता है।
उदाहरण: A Basic Timestamped Logger
#!/bin/bash
log() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1"
}
log "script starting"
log "doing some work"
log "script finished"
Login to try C/C++/Java/PHP code in the editor
stderr पर Logging करना
Log output को stdout के बजाय stderr पर भेजना इसे script के actual results से अलग रखता है, ताकि stdout अभी भी किसी दूसरे program द्वारा cleanly pipe या capture हो सके।
उदाहरण: Logging to stderr
#!/bin/bash
log() {
echo "[$(date +'%H:%M:%S')] $1" >&2
}
log "this is a diagnostic message"
echo "this is the actual result"
Login to try C/C++/Java/PHP code in the editor
Log Levels जोड़ना
Logging function को एक severity level (INFO, WARN, ERROR) accept करने के लिए extend करना बाद में message importance को filter या visually distinguish करना easy बनाता है, बिना हर call site की structure बदले।
उदाहरण: Adding Log Levels
#!/bin/bash
log() {
local level=$1
shift
echo "[$(date +'%H:%M:%S')] [$level] $*"
}
log INFO "starting process"
log WARN "low disk space detected"
log ERROR "failed to connect, but continuing demo"
Login to try C/C++/Java/PHP code in the editor
Logs को एक File और Screen दोनों पर भेजना
Logger के output को tee -a के through pipe करना हर message को एक persistent log file में लिखता है जबकि इसे तुरंत भी दिखाते हुए, दो पहले से covered techniques को combine करते हुए।
उदाहरण: Sending Logs to a File and the Screen
#!/bin/bash
log() {
echo "[$(date +'%H:%M:%S')] $1" | tee -a run.log
}
log "first event"
log "second event"
echo "--- saved log file ---"
cat run.log
rm -f run.log
Login to try C/C++/Java/PHP code in the editor
- पूरी script में status messages के लिए plain
echoउपयोग करना, बाद में यह बताना impossible बनाते हुए कि हर event कब हुई। - Log messages stdout पर लिखना जब उन्हें stderr (या एक dedicated log file) पर जाना चाहिए, diagnostics को actual program output के साथ mix करते हुए।
- Background jobs से logging करते समय buffering account करना भूल जाना, जो log lines को unpredictably interleave करने का कारण बन सकता है।
dateplus message wrap करने वाला एक छोटाlogfunction पूरी script में timestamp formatting consistent रखता है।- stderr पर logging करना diagnostic output को script के actual stdout results से अलग रखता है।
date +"%Y-%m-%d %H:%M:%S"log lines के लिए एक common, sortable timestamp format है।- Logging को एक function में centralize करना बाद में सभी log output को एक file, syslog, या दोनों में redirect करना easy बनाता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first:
- Quoting Pitfalls and Why to Always Quote Variables
- set -euo pipefail as a Script Header
- Structuring a Script (Functions, main() Pattern, Exit at the End)
- Idempotent Scripts (Safe to Re-Run)
- Logging in Scripts (Timestamped log Function)
- Writing Portable Scripts (Bash-isms vs POSIX sh)
- Capstone: A Real-World Backup/Cleanup Script