التسجيل المنظم ومعرّفات الارتباط
تعلّم إنتاج سجلات JSON منظمة قابلة للبحث، وتتبع الطلب عبر عدة وظائف باستخدام معرّفات الارتباط.
التسجيل المنظم ومعرّفات الارتباط درس مجاني في Serverless AWS Lambda Development على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Serverless AWS Lambda Development، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Serverless AWS Lambda Development 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Plain Logs Do Not Scale
Free-text log lines are hard to filter at scale. Structured logging writes each entry as JSON with consistent fields you can query.
Anatomy of a Structured Log
A good log entry has a level, a message, and contextual fields like request id and user id.
{
"level": "INFO",
"message": "order placed",
"orderId": 42,
"requestId": "abc-123"
}Emitting JSON Logs
Serialize a dictionary to JSON and print it. CloudWatch Logs Insights can then query individual fields.
import json, sys
def log(level, message, **fields):
entry = {'level': level, 'message': message}
entry.update(fields)
print(json.dumps(entry), file=sys.stdout)
log('INFO', 'order placed', orderId=42)Querying with Logs Insights
Logs Insights lets you filter and aggregate JSON fields with a query language.
fields @timestamp, orderId
| filter level = 'ERROR'
| sort @timestamp desc
| limit 20The Correlation ID
A correlation ID is a unique value attached to a request and passed to every downstream service, so you can trace one request end to end.
Generating One
Create the ID at the entry point if the incoming request does not already carry one.
import uuid
def get_correlation_id(event):
headers = event.get('headers') or {}
return headers.get('x-correlation-id') or str(uuid.uuid4())Propagating It
Pass the correlation ID forward in message attributes, HTTP headers, or event payloads so the next function logs the same ID.
sns.publish(
TopicArn=topic,
Message=body,
MessageAttributes={
'correlationId': {'DataType': 'String', 'StringValue': cid}
}
)Log Levels
Use levels (DEBUG, INFO, WARN, ERROR) and make the threshold configurable via an environment variable so production stays quiet but debuggable.
Never Log Secrets
Structured logs are searchable, which makes accidental secret logging dangerous. Redact tokens, passwords, and PII before logging.
Control Log Retention
Logs cost money to store. Set a retention period on each log group so old logs expire automatically.
aws logs put-retention-policy \
--log-group-name /aws/lambda/orders \
--retention-in-days 30Tie Logs to Traces
Include the X-Ray trace id in your structured logs so you can jump from a log line straight to the distributed trace for that request.
Quick Check
Test your logging knowledge.
Recap
You learned structured JSON logging, querying with Logs Insights, threading correlation IDs across services, redacting secrets, and setting retention.
الأسئلة الشائعة
هل درس «التسجيل المنظم ومعرّفات الارتباط» مجاني؟
نعم — نص درس «التسجيل المنظم ومعرّفات الارتباط» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Serverless AWS Lambda Development، انتقل إلى CoddyKit PRO. تتضمن دورة Serverless AWS Lambda Development 4 دروس في المجموع.
ماذا ستتعلم في «التسجيل المنظم ومعرّفات الارتباط»؟
تعلّم إنتاج سجلات JSON منظمة قابلة للبحث، وتتبع الطلب عبر عدة وظائف باستخدام معرّفات الارتباط. تتمرن على Serverless AWS Lambda Development مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Serverless AWS Lambda Development؟
لا تُشترط خبرة سابقة. Serverless AWS Lambda Development على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «التسجيل المنظم ومعرّفات الارتباط»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Serverless AWS Lambda Development هذا؟
نعم. كل درس في Serverless AWS Lambda Development يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- سياسات IAM وأذوناتها المتقدمة
- إدارة الأسرار باستخدام AWS Secrets Manager
- التتبّع الموزّع باستخدام AWS X-Ray
- التسجيل المنظم ومعرّفات الارتباط