Is there a way to override the default logger that...
# ask-ai
m
Is there a way to override the default logger that is being used? I have a logging module that only logs in json format. I need to use that with livekit
t
Thanks for reaching out, AI. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You can route LiveKit SDK logs into your JSON-only logger using
setLogExtension()
in the JavaScript SDK — this captures all SDK log messages so you can format and emit them however you need. Example:
Copy code
import { setLogExtension } from "livekit-client";

setLogExtension((level, msg, context) => {
  yourJsonLogger.log({
    level,
    message: msg,
    context,
    timestamp: new Date().toISOString(),
  });
});
Notes: • This does not replace the internal logger object, but it forwards SDK log output so you can produce JSON-only logs. • After connection, context like room name/ID is included automatically; you can add extra metadata if desired. Would you like a version that filters levels or maps LiveKit levels to your logger's schema? Sources: How to Send Logs from the JavaScript SDK to Providers like Datadog
m
Im in python
t
You're using Python — good clarification. You can route LiveKit logs into your JSON-only logger by configuring Python's
logging
(LiveKit uses the standard logging system). Steps: 1. Create a JSON formatter:
Copy code
import json
import logging

class JsonFormatter(logging.Formatter):
    def format(self, record):
        entry = {
            "timestamp": self.formatTime(record, self.datefmt),
            "level": record.levelname,
            "module": record.module,
            "func": record.funcName,
            "line": record.lineno,
            "message": record.getMessage(),
        }
        if record.exc_info:
            entry["exception"] = self.formatException(record.exc_info)
        return json.dumps(entry)
1. Replace handlers for the root or LiveKit logger so SDK logs go through your formatter:
Copy code
logger = logging.getLogger()  # or logging.getLogger("livekit") if you want only SDK logs
logger.setLevel(logging.INFO)

# remove existing handlers
for h in logger.handlers[:]:
    logger.removeHandler(h)

# add JSON handler
handler = logging.StreamHandler()
handler.setFormatter(JsonFormatter())
logger.addHandler(handler)
1. (Optional) Add contextual fields like
room_name
or
worker_id
via extra or by injecting fields where appropriate. For agent/job contexts you can also use
JobContext.log_context_fields
to include metadata in logs. If you want, tell me whether you need: • only SDK logs converted or all application logs, • logs sent to stdout, a file, or a remote sink, and I’ll provide the exact code snippet for that. Sources: Log collection | LiveKit Docs | Job lifecycle | LiveKit docs
m
All the livekit logs that comesout when i use uv run main.py dev. Should use my logger
t
Got it — to route all LiveKit logs emitted when you run
uv run main.py dev
through your JSON logger, configure Python's root logger before LiveKit initializes so every SDK logger uses your handler/formatter. 1) Add this at the very top of your entrypoint (before importing or initializing LiveKit):
Copy code
import logging
import json

class JsonFormatter(logging.Formatter):
    def format(self, record):
        entry = {
            "timestamp": self.formatTime(record, self.datefmt),
            "level": record.levelname,
            "module": record.module,
            "func": record.funcName,
            "line": record.lineno,
            "message": record.getMessage(),
        }
        if record.exc_info:
            entry["exception"] = self.formatException(record.exc_info)
        return json.dumps(entry)

# Configure root logger so all libraries (including livekit) use it
root = logging.getLogger()
root.setLevel(logging.INFO)  # or DEBUG
for h in root.handlers[:]:
    root.removeHandler(h)

handler = logging.StreamHandler()  # stdout; use FileHandler or custom handler if needed
handler.setFormatter(JsonFormatter())
root.addHandler(handler)
2) If you only want LiveKit logs routed (not all app logs), attach the handler to the LiveKit logger specifically:
Copy code
lk_logger = logging.getLogger("livekit")
lk_logger.setLevel(logging.INFO)
lk_logger.handlers = []
lk_logger.addHandler(handler)
3) If you need extra context (room id, worker id), inject via
extra
where available or, for agent jobs, set
JobContext.log_context_fields
so those fields appear in logs. Place the config at program start (before
import livekit
or any LiveKit client/agent start). Want a version that sends logs to a file, stdout with timestamps in ISO, or maps LiveKit levels to your schema?