Code Monkey home page Code Monkey logo

Comments (11)

wbolster avatar wbolster commented on May 19, 2024

Fwiw, the stdlib's Logger also logs the original exception message, regardless of the message passed to the .exception(...) method.

from structlog.

wbolster avatar wbolster commented on May 19, 2024

Okay, it seems this is already (mostly) addressed by #22.

The stdlib's .exception() also includes the original exception message; I can't quickly see whether structlog's emulation does that as well?

from structlog.

hynek avatar hynek commented on May 19, 2024

Please try and file a bug if it doesn’t. (respectively transform this ticket into that in that case to make use of it :)).

from structlog.

hynek avatar hynek commented on May 19, 2024

(I’m not 100% sure what you mean so I figure it’s faster if you just test it before I start asking clarification questions and test it then to tell you)

from structlog.

wbolster avatar wbolster commented on May 19, 2024

This Python file:

logging.basicConfig()
logger = logging.getLogger(__name__)

try:
    raise ValueError("This is the exception message.")
except ValueError:
    logger.exception("This is the log message.")

shows this output:

ERROR:__main__:This is the log message.
Traceback (most recent call last):
  File "blah.py", line 8, in <module>
    raise ValueError("This is the exception message.")
ValueError: This is the exception message.

As you can see, both the log message and the exception message show up in the log. A similar setup with structlog loses the exception message.

from structlog.

wbolster avatar wbolster commented on May 19, 2024

The structlog configuration is from the docs:

    structlog.configure(
        processors=[
            structlog.stdlib.filter_by_level,
            structlog.processors.StackInfoRenderer(),
            structlog.processors.format_exc_info,
            structlog.processors.JSONRenderer(),
        ],
        context_class=dict,
        logger_factory=structlog.stdlib.LoggerFactory(),
        wrapper_class=structlog.stdlib.BoundLogger,
        cache_logger_on_first_use=True,
    )

from structlog.

hynek avatar hynek commented on May 19, 2024

So, I’ve tested it with

import logging

import structlog


logging.basicConfig()

structlog.configure(
    processors=[
        structlog.stdlib.filter_by_level,
        structlog.processors.StackInfoRenderer(),
        structlog.processors.format_exc_info,
        structlog.processors.JSONRenderer(),
    ],
    context_class=dict,
    logger_factory=structlog.stdlib.LoggerFactory(),
    wrapper_class=structlog.stdlib.BoundLogger,
    cache_logger_on_first_use=True,
)

logger = structlog.get_logger()

try:
    raise ValueError("This is the exception message.")
except ValueError:
    logger.exception("This is the log message.")

And the reformatted output I’m getting is:

{
  "exception": "Traceback (most recent call last):\n  File \"t2.py\", line 22, in <module>\n    raise ValueError(\"This is the exception message.\")\nValueError: This is the exception message.",
  "event": "This is the log message."
}

So the error message clearly is there (the stdlib output is completely within the formatted string), but what you’re saying is that you’d like the exception data in a more structured form?

Right now, format_exc_info really just formats the traceback: https://github.com/hynek/structlog/blob/master/structlog/processors.py#L169

It’s totally thinkable to pull data out of the exception I guess? I’m removing the milestone though because this is a nice but not too important feature (and you can implement it for yourself for now if you want to).

from structlog.

wbolster avatar wbolster commented on May 19, 2024

Okay maybe I was just being blind. The original message is part of the formatted traceback, right?

from structlog.

hynek avatar hynek commented on May 19, 2024

Yes, it should. Scroll to the right above to verify. :) The job of format_exc_info is to emulate the output you would have gotten on the console.

from structlog.

wbolster avatar wbolster commented on May 19, 2024

In that case I must've missed
it the first time in the huge json spaghetti on my screen. I'm sorry for the noise.

Hynek Schlawack [email protected] schreef op 10 januari 2015 09:40:00 CET:

Yes, it should. Scroll to the right above to verify. :) The job of
format_exc_info is to emulate the output you would have gotten on the
console.


Reply to this email directly or view it on GitHub:
https://github.com/hynek/structlog/issues/31#issuecomment-69448676

Sent from my Android device with K-9 Mail. Please excuse my brevity.

from structlog.

wbolster avatar wbolster commented on May 19, 2024

I think extracting the actual error message from a stack trace is not that useful anyway. The message will likely not be that useful without the additional info from the stack trace, so I guess the current output is fine.

Closing this issue.

from structlog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.