البرمجيات الوسيطة لتسجيل السجلات وقياس الأداء
أضف قابلية الرصد إلى API في tRPC باستخدام برمجية وسيطة تسجّل كل استدعاء وتقيس وقت تنفيذه.
البرمجيات الوسيطة لتسجيل السجلات وقياس الأداء درس مجاني في tRPC End-to-End Type Safe APIs على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في tRPC End-to-End Type Safe APIs، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة tRPC End-to-End Type Safe APIs 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Why Observe Every Call?
You already use middleware for auth. Middleware is also perfect for cross-cutting concerns like logging and timing that should apply to many procedures.
Middleware Recap
A tRPC middleware wraps a procedure: it runs code, calls next(), then can run code after the result.
const mw = t.middleware(async (opts) => {
// before
const result = await opts.next();
// after
return result;
});A Simple Logger
Log the procedure type and path on every call.
const logger = t.middleware(async ({ path, type, next }) => {
console.log("-> " + type + " " + path);
return next();
});Measuring Duration
Capture the time before and after next() to compute how long the call took.
const timing = t.middleware(async ({ path, next }) => {
const start = Date.now();
const result = await next();
const ms = Date.now() - start;
console.log(path + " took " + ms + "ms");
return result;
});Inspecting the Result
The result tells you whether the call succeeded, useful for logging error rates.
const result = await next();
console.log(result.ok ? "ok" : "error");
return result;Attaching to a Procedure
Use .use() to apply middleware when defining a base procedure.
const loggedProcedure = t.procedure.use(logger).use(timing);Reusing Across Routers
Export the enhanced procedure and build all routers from it so logging applies everywhere automatically.
export const appRouter = router({
ping: loggedProcedure.query(() => "pong"),
});Adding Request IDs
Generate a unique id per call to correlate logs across services.
const withId = t.middleware(async ({ next, ctx }) => {
const reqId = crypto.randomUUID();
return next({ ctx: { ...ctx, reqId } });
});Structured Logging
Emit JSON instead of plain text so logs are searchable in tools like Datadog or ELK.
console.log(JSON.stringify({ path, type, ms, level: "info" }));Order of Middleware
Middleware runs in the order added. Put timing outermost so it includes the work of inner middleware.
Sampling Logs
In high-traffic APIs, logging every call is costly. Sample a percentage of requests so you keep visibility without overwhelming your log pipeline.
if (Math.random() < 0.1) console.log(path);Quick Check
Test your middleware knowledge.
Recap
You added observability with middleware:
- Middleware runs code before and after next()
- Use it for logging, timing, and request IDs
- Build a shared base procedure so it applies everywhere
Good observability makes a tRPC API far easier to debug in production.
الأسئلة الشائعة
هل درس «البرمجيات الوسيطة لتسجيل السجلات وقياس الأداء» مجاني؟
نعم — نص درس «البرمجيات الوسيطة لتسجيل السجلات وقياس الأداء» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة tRPC End-to-End Type Safe APIs، انتقل إلى CoddyKit PRO. تتضمن دورة tRPC End-to-End Type Safe APIs 4 دروس في المجموع.
ماذا ستتعلم في «البرمجيات الوسيطة لتسجيل السجلات وقياس الأداء»؟
أضف قابلية الرصد إلى API في tRPC باستخدام برمجية وسيطة تسجّل كل استدعاء وتقيس وقت تنفيذه. تتمرن على tRPC End-to-End Type Safe APIs مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ tRPC End-to-End Type Safe APIs؟
لا تُشترط خبرة سابقة. tRPC End-to-End Type Safe APIs على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «البرمجيات الوسيطة لتسجيل السجلات وقياس الأداء»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس tRPC End-to-End Type Safe APIs هذا؟
نعم. كل درس في tRPC End-to-End Type Safe APIs يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- إنشاء سياق tRPC
- البرمجيات الوسيطة للمصادقة
- سلاسل البرمجيات الوسيطة المخصّصة
- البرمجيات الوسيطة لتسجيل السجلات وقياس الأداء