Skip to content

Commit

Permalink
docs: add sample for TC400 (logging.exception)
Browse files Browse the repository at this point in the history
  • Loading branch information
guilatrova committed Jul 31, 2021
1 parent 72859ad commit a74d97b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
7 changes: 7 additions & 0 deletions docs/violations/README.md
Expand Up @@ -32,3 +32,10 @@
| ----------------- | --------------------------------- |
| [TC300](TC300.md) | Consider adding an `else` block |
| [TC301](TC301.md) | Avoid direct raises in `try` body |

## `TC4xx` - Logging usage


| Code | Description |
| ----------------- | ------------------------------------------- |
| [TC400](TC400.md) | Use logging '.exception' instead of 'error' |
29 changes: 29 additions & 0 deletions docs/violations/TC400.md
@@ -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")
```
10 changes: 10 additions & 0 deletions src/tests/samples/violations/log_error.py
@@ -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'")

0 comments on commit a74d97b

Please sign in to comment.