Open NobleMathews opened 5 months ago
To address the issue of creating a custom logger in the loguru
library that outputs structured JSON logs with different formats for logger.error
and logger.exception
, follow these steps:
record["exception"]
is present.Here is the updated serialize
function:
import json
import sys
import traceback
from loguru import logger
def serialize(record):
error = record["exception"]
level = record["level"].name
if error:
exception = {
"type": error.type.__name__,
"value": str(error.value),
"traceback": "".join(traceback.format_exception(*error))
}
else:
exception = {
"type": None,
"value": None,
"traceback": None
}
to_serialize = {
"level": level,
"message": record["message"],
"time": record["time"].strftime("%d-%m-%YT%H:%M:%SZ"),
"exception": exception,
}
# Remove traceback if the log level is 'ERROR' and no exception is present
if level == "ERROR" and not error:
to_serialize["exception"]["traceback"] = None
return json.dumps(to_serialize)
logger.add(sys.stderr, format=serialize)
This setup ensures that:
logger.error
produces JSON output without traceback.logger.exception
produces JSON output with the complete traceback.Usage:
logger.error("Error occurred")
logger.exception("Exception occurred")
loguru/_handler.py:204-280 | The _serialize_record function in this snippet is crucial for understanding how records are serialized, which is directly related to the custom serialization logic needed for logger.error and logger.exception.
loguru/_recattrs.py:1-79 | Defines the RecordException class and other record attributes, which are necessary for understanding how to handle and serialize exceptions in log records.
💡 To rerun Mayil, comment mayil-ai rerun
. Mayil will incorporate any new context added to the ticket. Include details in your rerun comment to guide Mayil!
I'm trying to create a structured logger which outputs JSON, something using mentioned here:
My problem is I would like to have
logger.error("some error")
to result meerror = record["exception"]
withBut with
logger.exception("something completely unknown")
to result:I know there is
logger.opt()
but I would like to have something like above for ergonomic code.My current idea is to do something like this: