انتقل إلى المحتوى
أمين أبوزيد
JNR-006

التسجيل (Logging)

  • 7 درس
  • 76 دقيقة قراءة
  • 2 ساعة تدريب (تقدير)
في هذه الصفحة 8 موضوعات

نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.

نظرة عامة

الموضوعات

  • SLF4J
  • Logback
  • مستويات السجل
  • الرسائل المهيكلة
  • تجنب البيانات الحساسة

تعلّم أولًا

76 دقيقة قراءة · 2 ساعة تدريب (تقدير)

مخرجات التعلم

  1. Log through SLF4J with parameterized messages and choose the correct level.
  2. Configure Logback appenders, patterns, and per-package levels.
  3. Log exceptions with their stack traces exactly once.
  4. Identify and remove sensitive data (passwords, tokens, personal data) from logs.
  5. Explain structured (JSON) logging and why machines read logs too.

الدروس

  1. 01لماذا System.out ليس تسجيلاً11 د قراءة
  2. 02SLF4J وLogback11 د قراءة
  3. 03اختيار مستويات السجل11 د قراءة
  4. 04تسجيل الاستثناءات بشكل جيد11 د قراءة
  5. 05البيانات الحساسة في السجلات11 د قراءة
  6. 06التسجيل المهيكل (Structured logging)10 د قراءة
  7. 07عرض عملي: من println إلى سجلات JSON مهيكلة11 د قراءة

المختبر والتمارين

Lab

JNR-006-LAB1 · Observable Shop

Outcomes: 1–5 · Time: ~1.5 h Add SLF4J logging to Shop Core and the CLI. Switch to JSON output by configuration only. Show a masked card number in the output. Acceptance: no secrets in logs; no duplicate stack traces; levels chosen correctly; JSON output enabled without code changes.

Exercises (assessment: review a log file and a logging config for problems, plus six exercises)

IDOutcomeTask
JNR-006-EX011Convert System.out.println("Order " + id + " saved") to an SLF4J call with a placeholder and the right level.
JNR-006-EX022Write a logback.xml with a console appender, a pattern, root INFO, and com.shop at DEBUG.
JNR-006-EX031Choose a level: payment declined, database down, order placed, loop variable value, retry succeeded.
JNR-006-EX043Fix a catch block that does log.error(e.getMessage()); throw e;.
JNR-006-EX054Write mask(String card) that returns only the last four digits, and list five values never to log.
JNR-006-EX065Explain in three sentences why JSON logs help, and show one example line.
أظهر الحلول
  • EX01 log.info("Order {} saved", id);
  • EX02 <logger name="com.shop" level="DEBUG"/> and <root level="INFO"><appender-ref ref="STDOUT"/></root> with <pattern>%d %-5level %logger{20} - %msg%n</pattern>.
  • EX03 WARN; ERROR; INFO; DEBUG; INFO or WARN (team choice, justified).
  • EX04 Either log with the exception and handle: log.error("Payment failed", e); or just throw e and log at the top level, not both.
  • EX05 "**** **** **** " + card.substring(card.length() - 4). Never log: passwords, tokens, API keys, full card numbers, national IDs.
  • EX06 Machines can filter by field, count errors, and follow one request; people still read the message field.
  • Outcome 2 and 5 are also covered by the lab and quiz Q02, Q09–Q10.

الاختبار

Outcomes: O1 SLF4J and levels · O2 Logback config · O3 exceptions · O4 sensitive data · O5 structured logging.

اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.

  1. 1SLF4J is:O1
  2. 2Which is the best log call?O1
  3. 3Which level for an expected "user not found"?O1
  4. 4In Logback, an appender defines:O2
  5. 5You can set com.shop to DEBUG and the rest to INFO using:O2
  6. 6How do you log an exception with its stack trace?O3
  7. 7Log-and-rethrow is a problem because:O3
  8. 8Which must never be logged?O4
  9. 9A good way to log a card number is:O4
  10. 10JSON logs help because:O5

الجزء العملي

Six exercises JNR-006-EX01 to EX06 plus the lab; the assessment reviews a log file and a logging config for problems. Pass: at least 8 of 10 quiz answers, five exercises correct, and a lab with no secrets and no duplicate stack traces in the logs.

أخطاء شائعة

String concatenation in log calls; log-and-rethrow; ERROR for expected situations.

التقييم

Review a log file and a logging config for problems.

الصلة بالمسار الوظيفي

Troubleshooting (JNR-054) and observability (MID-047).

اكتب للبحث في المستويات والمراحل والوحدات والدروس.