big-application-86058
12/23/2025, 3:59 AMtall-airline-16756
12/23/2025, 3:59 AMtall-airline-16756
12/23/2025, 3:59 AMbig-application-86058
12/23/2025, 4:00 AMtall-airline-16756
12/23/2025, 4:00 AMbig-application-86058
12/23/2025, 4:02 AMtall-airline-16756
12/23/2025, 4:02 AMbig-application-86058
12/23/2025, 4:03 AMtall-airline-16756
12/23/2025, 4:03 AMbig-application-86058
12/23/2025, 4:05 AMtall-airline-16756
12/23/2025, 4:06 AMlivekit_logger.py)
import logging
from logging.handlers import RotatingFileHandler
from datetime import datetime, timezone
ISO_FORMAT = "%Y-%m-%dT%H:%M:%S%z"
class ISOFormatter(logging.Formatter):
def formatTime(self, record, datefmt=None):
# Use UTC ISO 8601 with timezone Z
dt = datetime.fromtimestamp(record.created, tz=timezone.utc)
return dt.strftime("%Y-%m-%dT%H:%M:%SZ")
def setup_logger(name: str,
logfile: str = "livekit_client.log",
level=<http://logging.INFO|logging.INFO>,
max_bytes: int = 10 * 1024 * 1024,
backup_count: int = 3):
"""
Creates a logger that writes plain-text messages prefixed with ISO 8601 timestamps
to both console and a rotating file.
"""
logger = logging.getLogger(name)
logger.setLevel(level)
logger.propagate = False
fmt = "%(asctime)s %(levelname)s %(name)s: %(message)s"
formatter = ISOFormatter(fmt)
# Console handler
ch = logging.StreamHandler()
ch.setLevel(level)
ch.setFormatter(formatter)
# Rotating file handler
fh = RotatingFileHandler(logfile, maxBytes=max_bytes, backupCount=backup_count)
fh.setLevel(level)
fh.setFormatter(formatter)
# Clear existing handlers to avoid duplicates if called multiple times
if logger.hasHandlers():
logger.handlers.clear()
logger.addHandler(ch)
logger.addHandler(fh)
return logger
2) Example integration with the official LiveKit Python client
• This example assumes you are using the official LiveKit Python client and have an async client/room connection. Adjust event hooks to match the specific client API you use.
import asyncio
from livekit import Room, RoomOptions # adjust imports to match your LiveKit client package
from livekit_logger import setup_logger
logger = setup_logger("livekit_client", logfile="livekit_client.log")
async def on_connected():
<http://logger.info|logger.info>("Connected to LiveKit server")
async def on_disconnected():
<http://logger.info|logger.info>("Disconnected from LiveKit server")
async def on_joined_room(room_name: str, participant_id: str):
<http://logger.info|logger.info>(f"Joined room '{room_name}' as participant '{participant_id}'")
async def on_left_room(room_name: str, participant_id: str):
<http://logger.info|logger.info>(f"Left room '{room_name}' as participant '{participant_id}'")
async def on_local_track_published(track_id: str, kind: str):
<http://logger.info|logger.info>(f"Published local track id={track_id} kind={kind}")
async def on_local_track_unpublished(track_id: str, kind: str):
<http://logger.info|logger.info>(f"Unpublished local track id={track_id} kind={kind}")
async def on_error(exc: Exception):
logger.error(f"Error: {exc}", exc_info=True)
async def main():
# Create/connect your Room/client here (example is schematic)
room = Room() # replace with actual client constructor and connection logic
# Hook into events - adjust to match the client's event API
# Examples (pseudo-code, adapt to actual LiveKit Python client event names):
room.on("connected", lambda: asyncio.create_task(on_connected()))
room.on("disconnected", lambda: asyncio.create_task(on_disconnected()))
room.on("room_joined", lambda room_name, pid: asyncio.create_task(on_joined_room(room_name, pid)))
room.on("room_left", lambda room_name, pid: asyncio.create_task(on_left_room(room_name, pid)))
room.on("published", lambda track_id, kind: asyncio.create_task(on_local_track_published(track_id, kind)))
room.on("unpublished", lambda track_id, kind: asyncio.create_task(on_local_track_unpublished(track_id, kind)))
room.on("error", lambda exc: asyncio.create_task(on_error(exc)))
# Connect / run client per your app flow
await room.connect("<wss://your-livekit-server>", token="YOUR_TOKEN")
if __name__ == "__main__":
asyncio.run(main())
3) Notes and quick customization
• The logger writes plain-text lines like:
2025-12-23T150405Z INFO livekit_client: Joined room 'myroom' as participant 'user123'
• File rotation is enabled (default 10MB, 3 backups). Change max_bytes / backup_count in setup_logger.
• If you prefer local timezone instead of UTC, modify ISOFormatter.formatTime.
• Adapt the event names/hooks in the integration code to the exact official LiveKit Python client API you have — the example shows the pattern for wiring event callbacks to the logger.
If you’d like, I can:
• Provide the exact integration code for the specific LiveKit Python client version you’re using (tell me the package name and version).
• Add periodic stats logging (bitrate/packet loss) or redact PII before logging.
Which would you like next?