Logging कैसे करें
Logging आपके script ने क्या किया इसकी एक diary रखने जैसा है, हर entry के बगल में समय लिखा हुआ, ताकि आप बाद में वापस पढ़ सकें और समझ सकें कि क्या हुआ था।
In this page:
Syntax
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1"
}
log "message"
Logging
एक simple logging function जो messages को एक timestamp के साथ prefix करता है scripts को debug करना scattered echo calls से कहीं ज़्यादा आसान बनाता है।
Logs को tee या redirection से एक file में भेजना आपको script खत्म होने के बाद यह review करने देता है कि क्या हुआ था। Consistent logging खासकर उन scripts में valuable है जो unattended run होते हैं, जैसे cron jobs।
Note:
हर log line में एक timestamp शामिल करें ताकि आप script के behavior को system में कहीं और होने वाले events के साथ correlate कर सकें।
उदाहरण: Logging
#!/bin/bash
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1"
}
log "Script started"
log "Processing data..."
log "Script finished"
Login to try C/C++/Java/PHP code in the editor
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. #}
आम गलतियां
- Logs को
>से लिखना इसलिए file हर बार overwrite हो जाती है;>>उपयोग करें। 2>&1के बिना errors और output को mix करना।- Timestamps के बिना logging करना, जो problems को trace करना मुश्किल बनाता है।
🔒
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first:
- Exit Codes and Checking $? Correctly
- set -e and set -u
- Error Handling
- set -e (Exit on Error) and Its Gotchas
- Logging
- set -u (Undefined Variables) and set -x (Trace Mode)
- Script Arguments with getopts
- trap ERR for Custom Error Handling
- Debug Mode (set -x)
- Writing a Custom Error/die Function
- Portability
- Validating Script Input and Arguments