Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Traceback is being logged unexpectedly #1199

Open
Jakobovski opened this issue Sep 10, 2024 · 1 comment
Open

Traceback is being logged unexpectedly #1199

Jakobovski opened this issue Sep 10, 2024 · 1 comment
Labels
question Further information is requested

Comments

@Jakobovski
Copy link

Jakobovski commented Sep 10, 2024

I am trying to log with a custom JSON serialization and include the traceback and exception details in the JSON. I used patch() in order to do this.

Unexpectedly, unless i add the following line record['exception'] = None to patching() loguru will log the traceback in a non-JSON format even though I am using format="{extra[serialized]}"

See the code below

Am i doing something wrong or is this a bug?

import sys
import traceback

from loguru import logger


def serialize(record):
    error = record["exception"]
    if error:  # only set when exception.
        exception = {
            "type": error.type.__name__,
            "value": str(error.value),
            "traceback": "".join(traceback.format_exception(*error)),
        }
    else:
        exception = None

    subset = {
        "timestamp": record["time"].timestamp() * 1000,  # Should be in milliseconds for Datadog
        "message": record["message"],
        "level": record["level"].name,
        "status": str(record["level"].name),
        "logger": {"thread_name": str(record["thread"].name)},
        "file": f"{record['file']}",
        "function": f"{record['function']}",
        "line": f"{record['line']}",
        "module": f"{record['module']}",
        "name": f"{record['name']}",
        "process": f"{record['process']}",
        "thread": f"{record['thread']}",
        "exception": exception
    }
    return json.dumps(subset)


def patching(record):
    record["extra"]["serialized"] = serialize(record)
   # record['exception'] = None


logger.remove(0)  # Remove the original logger
logger = logger.patch(patching)
logger.add(sys.stdout, format="{extra[serialized]}")


def log_exception():
    try:
        open("fakefile.txt")
    except:
        logger.exception('')


log_exception()
@Delgan
Copy link
Owner

Delgan commented Sep 22, 2024

Hey @Jakobovski.

When format is a str, Loguru automatically appends "\n{exception}" to the format for convenience.

You can disable this by using a formatting function instead of a string:

logger.add(sys.stdout, format=lambda r: "{extra[serialized]}")

@Delgan Delgan added the question Further information is requested label Sep 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants