Python structlog support out of the box?

anonymousPRO

10 months ago

I switched my Django application to use structured logging, but now the content is not showing up in the log explorer (only after folding out). I am using structlog (through django-structlog), which is the main Python structured logging package, which puts the main message into the key "event". Do I need to rename this to "message" for this to work? Would be great if Railway would support the structlog format out-of-the-box.

5 Replies

gownercodeHOBBY

10 months ago

Hey, I'm constructing my own structlog:

def log(self, message: str, level: Literal["debug", "info", "warn", "error"] = "debug", metadata: dict = None) -> None:
        structured_log = {
            "message": message,
            "level": level,
        }
        if metadata is None:
            metadata = {}
        if not metadata.get("timestamp"):
            metadata["timestamp"] = int(time.time())
        self.jdb.log_add(message, level, metadata, metadata["timestamp"])
        if os.getenv("JORVIS_ENV") == "dev":
            print(f"{COLORS[level]}[{level.upper()}][{metadata['timestamp']}] {message} | {metadata}{COLORS['end']}")
        else:
            print(json.dumps(structured_log))

But my logs have the same issue: The messages don't show up in the log observer, only when unfolded.


gownercodeHOBBY

10 months ago

Also tried print(json.dumps(structured_log).replace('\n', '')) to satisfy this constraint: "Note that the entire log must be emitted on a single line to be parsed correctly." Also does not solve the problem.


10 months ago

Yes your message text would need to be in either a msg or message attribute.


gownercodeHOBBY

10 months ago

Thank you Brody, that works perfectly!

Note that print() in python always adds a newline to whatever it is passed unless instructed not to, so what I did above only strips newlines inside of the json string itself.


10 months ago

Ah I felt like that's what you were doing, but thought i'd mention it just in case!Glad I was able to help!