← Back to Bash Course | Chapter 14: Best Practices & Real-World Scripting | Lesson 5 of 7

Scripts में Logging (Timestamped log Function)

एक logging function messages को time attach करके print करता है, ताकि आप बाद में exactly बता सकें कि script का हर step कब हुआ।
Syntax
bash
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

bash
#!/bin/bash
log() {
    echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1"
}

log "script starting"
log "doing some work"
log "script finished"

stderr पर Logging करना

Log output को stdout के बजाय stderr पर भेजना इसे script के actual results से अलग रखता है, ताकि stdout अभी भी किसी दूसरे program द्वारा cleanly pipe या capture हो सके।

उदाहरण: Logging to stderr

bash
#!/bin/bash
log() {
    echo "[$(date +'%H:%M:%S')] $1" >&2
}

log "this is a diagnostic message"
echo "this is the actual result"

Log Levels जोड़ना

Logging function को एक severity level (INFO, WARN, ERROR) accept करने के लिए extend करना बाद में message importance को filter या visually distinguish करना easy बनाता है, बिना हर call site की structure बदले।

उदाहरण: Adding Log Levels

bash
#!/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"

Logs को एक File और Screen दोनों पर भेजना

Logger के output को tee -a के through pipe करना हर message को एक persistent log file में लिखता है जबकि इसे तुरंत भी दिखाते हुए, दो पहले से covered techniques को combine करते हुए।

उदाहरण: Sending Logs to a File and the Screen

bash
#!/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
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. पूरी script में status messages के लिए plain echo उपयोग करना, बाद में यह बताना impossible बनाते हुए कि हर event कब हुई।
  2. Log messages stdout पर लिखना जब उन्हें stderr (या एक dedicated log file) पर जाना चाहिए, diagnostics को actual program output के साथ mix करते हुए।
  3. Background jobs से logging करते समय buffering account करना भूल जाना, जो log lines को unpredictably interleave करने का कारण बन सकता है।
चैप्टर सारांश
  • date plus message wrap करने वाला एक छोटा log function पूरी 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 बनाता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.