ARCHIVED from builddistributedsystem.com on 2026-04-28 — URL: https://builddistributedsystem.com/tracks/messenger/tasks/task-1-3-2-envelope-logger
TASK

Implementation

In production distributed systems, message tracing is critical for debugging. When something goes wrong, you need to answer: "What messages did this node send and receive, and when?"

Your task is to add a message envelope logger to your node:

  1. Every received message should be logged with: timestamp, direction (RECV), src, dest, body type
  2. Every sent message should be logged with: timestamp, direction (SENT), src, dest, body type
  3. Logs are stored in an in-memory buffer (most recent 100 entries)
  4. Implement a get_log message type that returns the log entries
Request:  {"type": "get_log", "msg_id": 1, "count": 5}
Response: {"type": "get_log_ok", "in_reply_to": 1, "entries": [
    {"ts": "2024-01-01T00:00:00", "dir": "RECV", "src": "c0", "dest": "n1", "msg_type": "init"},
    {"ts": "2024-01-01T00:00:00", "dir": "SENT", "src": "n1", "dest": "c0", "msg_type": "init_ok"}
]}

The count field specifies how many recent entries to return. If fewer entries exist, return all of them.

Sample Test Cases

Init and echo produce correct outputTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"echo","msg_id":2,"echo":"log-test"}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
{"src": "n1", "dest": "c1", "body": {"type": "echo_ok", "echo": "log-test", "in_reply_to": 2, "msg_id": 1}}
Get log returns entries after initTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"get_log","msg_id":2,"count":10}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}

Hints

Hint 1
Log each message as a single line with a timestamp prefix
Hint 2
Include direction (SENT or RECV), src, dest, and body type
Hint 3
Use ISO 8601 format for timestamps
Hint 4
Log to stderr so it does not interfere with stdout message passing
Hint 5
Implement a get_log message type that returns recent log entries
OVERVIEW

Theoretical Hub

Concept overview coming soon

Key Concepts

loggingobservabilitymessage tracingtimestamps
main.py
python
Add Message Envelope Logger with Timestamps - The Messenger | Build Distributed Systems