-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add sample for TC400 (logging.exception)
- Loading branch information
1 parent
72859ad
commit a74d97b
Showing
3 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# `TC400` - Use logging '.exception' instead of 'error' | ||
|
||
## Why is it bad | ||
|
||
[Python docs](https://docs.python.org/3/library/logging.html#logging.Logger.exception) point out that you should use `exception` method inside an exception handler. It automatically add the stack trace and logs the message as `ERROR` level. | ||
|
||
## How it looks like | ||
|
||
```py | ||
def main_function(): | ||
try: | ||
process() | ||
handle() | ||
finish() | ||
except Exception as ex: | ||
logger.error("Context message here") | ||
``` | ||
|
||
## How it should be | ||
|
||
```py | ||
def main_function(): | ||
try: | ||
process() | ||
handle() | ||
finish() | ||
except Exception: | ||
logger.exception("Context message here") | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import logging | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def func(): | ||
try: | ||
a = 1 | ||
except Exception: | ||
logger.error("I'm using 'error', but should be using 'exception'") |