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

Add stack traces to logging #105

Open
plbertrand opened this issue Mar 26, 2022 · 0 comments · May be fixed by #106
Open

Add stack traces to logging #105

plbertrand opened this issue Mar 26, 2022 · 0 comments · May be fixed by #106

Comments

@plbertrand
Copy link
Contributor

Also, small tip (which applies to amqtt in general too, I see the pattern there in lots of places):

            try:
                # … etc …
                pass
            except ConnectException as ce:
                logger.error("Connection failed: %s" % ce)

Not the best way to log an exception for two reasons:

  1. % always formats the string, whether logger is configured to emit logs at error level or not
  2. you miss out on the stack trace, which can be real handy for troubleshooting

logger.error("Connection failed: %s", ce) addresses (1) whilst ignoring (2). The logging module has the exc_info keyword argument, which pretty much will do this for you… I'd write the above as:

            try:
                # … etc …
                pass
            except ConnectException:
                logger.error("Connection failed", exc_info=True)

Or, for error, there's a shortcut:

            try:
                # … etc …
                pass
            except ConnectException:
                logger.exception("Connection failed")

Originally posted by @sjlongland in #75 (comment)

plbertrand added a commit to plbertrand/amqtt that referenced this issue Mar 26, 2022
@plbertrand plbertrand linked a pull request Mar 26, 2022 that will close this issue
plbertrand added a commit to plbertrand/amqtt that referenced this issue Apr 15, 2022
plbertrand added a commit to plbertrand/amqtt that referenced this issue Apr 15, 2022
plbertrand added a commit to plbertrand/amqtt that referenced this issue Apr 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant