الفلاتر
طريقة البحث
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • "كلمتان" تجد العبارة بالضبط.
  • --name أو -x تجد خيارًا.
  • #كلمة تبحث داخل الأوامر فقط.
  • @grep تجد الدروس والمعامل التي تستخدم grep.
  • الفلاتر: cat:network · in:labs · level:rhcsa · ch:8 · root:yes. ضع ! قبل كلمة لاستبعادها. OR بين الكلمات تعني إحداها.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث

الفصل 8الاسم في الدورة: Exceptions and logging

Handle errors and write logs

Scripts that run at night must not crash silently. Catch the errors you expect, let the unexpected ones show clearly, and write logs with time stamps and levels instead of print.

  • 5 الدروس
  • 1 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

8.1

Catch exceptions

try runs code; except handles a specific error; else runs when there was none; finally always runs.

exceptions.py
import json
 
 
def load_config(path: str) -> dict:
    try:
        with open(path, encoding="utf-8") as f:
            return json.load(f)
    except FileNotFoundError:
        print(f"{path} not found, using defaults")
        return {}
    except json.JSONDecodeError as e:
        raise SystemExit(f"{path} is not valid JSON: line {e.lineno}") from e
    finally:
        print("config step done")

انتبه

Never write a bare except: or except Exception: pass — it hides real bugs. Catch only what you can handle.

8.2

Raise your own errors

Stop early with a clear message when input is wrong.

raise.py
def set_replicas(n: int) -> None:
    if not 1 <= n <= 20:
        raise ValueError(f"replicas must be 1-20, got {n}")
    print("ok", n)

8.3

Write logs with logging

The logging module adds time, level and source, and can write to a file and the screen at once.

log_setup.py
import logging
 
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s %(levelname)-8s %(name)s: %(message)s",
    handlers=[logging.FileHandler("/var/log/backup.log"), logging.StreamHandler()],
)
log = logging.getLogger("backup")
 
log.info("backup started")
log.warning("disk above %d%%", 80)
try:
    1 / 0
except ZeroDivisionError:
    log.exception("calculation failed")
LevelUse
DEBUGDetails for finding bugs
INFONormal steps
WARNINGSomething unusual, still working
ERRORA step failed
CRITICALThe script can not go on

تذكّر

log.exception(...) inside except also writes the full traceback.

8.4

Log to the journal

Scripts run by systemd can simply log to stdout/stderr — the journal stores it. Or send to syslog.

syslog_log.py
import logging
import logging.handlers
 
log = logging.getLogger("diskcheck")
log.setLevel(logging.INFO)
log.addHandler(logging.handlers.SysLogHandler(address="/dev/log"))
log.info("diskcheck: / is at 71%")
journalctl -t python3 -n 5
journalctl -u diskcheck.service -n 20

8.5

Rotate log files

A long-running script rotates its own log, or leaves it to logrotate.

rotate.py
import logging
from logging.handlers import RotatingFileHandler
 
h = RotatingFileHandler("/var/log/app/app.log", maxBytes=5_000_000, backupCount=5)
logging.getLogger().addHandler(h)

كل الأوامر في هذا الفصل