Posts Decorator and Structlog
Post
Cancel

Decorator and Structlog

During the Python web summit organized by Geekle.us, I listened to an interesting talk about Structlog library. I decided to play a bit with it and with decorators, I implemented a small project about logging.

Goal of the project

Create a decorator that will use structlog to add a logging system to each function of a class.

One of the goal is to have a trace_id to follow the code and to get easily information about the context when the function is called. For instance, the parameters provided to the function and the error message …

Different types of output

Development

1
2
3
4
5
6
7
8
9
10
11
/home/dianedelallee/.local/share/virtualenvs/decoratorStructlog-eT6Dw5f0/bin/python /home/dianedelallee/Desktop/Diane/decoratorStructlog/main.py
2021-02-23T14:20:34.415087Z [info     ] Before calling the function    args=() function_name=models.Player.get_name kwargs={} trace_id=d3b9b48e-ce0e-4cc2-a48c-66cbfd6c0743
2021-02-23T14:20:34.415249Z [info     ] trying to get the name of the user trace_id=d3b9b48e-ce0e-4cc2-a48c-66cbfd6c0743
2021-02-23T14:20:34.415300Z [info     ] After calling the function     function_name=models.Player.get_name result=Diane trace_id=d3b9b48e-ce0e-4cc2-a48c-66cbfd6c0743
2021-02-23T14:20:34.415380Z [info     ] Before calling the function    args=() function_name=models.Player.get_age_in_cat_live kwargs={} trace_id=659685aa-4e90-4de5-bb22-44f32ac4fb57
2021-02-23T14:20:34.415418Z [info     ] trying to get the aage in cat referencial trace_id=659685aa-4e90-4de5-bb22-44f32ac4fb57
2021-02-23T14:20:34.415442Z [info     ] get cat referential            trace_id=659685aa-4e90-4de5-bb22-44f32ac4fb57
2021-02-23T14:20:34.415461Z [info     ] After calling the function     function_name=models.Player.get_age_in_cat_live result=210 trace_id=659685aa-4e90-4de5-bb22-44f32ac4fb57

Process finished with exit code 0

Production

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/home/dianedelallee/.local/share/virtualenvs/decoratorStructlog-eT6Dw5f0/bin/python /home/dianedelallee/Desktop/Diane/decoratorStructlog/main.py
{
  "args": [],
  "event": "Before calling the function",
  "function_name": "models.Player.get_name",
  "kwargs": {},
  "level": "info",
  "timestamp": "2021-02-23T14:21:41.464028Z",
  "trace_id": "8fdbd416-d109-4671-8b57-cc28a9ebbbe6"
}
{
  "event": "trying to get the name of the user",
  "level": "info",
  "timestamp": "2021-02-23T14:21:41.464145Z",
  "trace_id": "8fdbd416-d109-4671-8b57-cc28a9ebbbe6"
}
{
  "event": "After calling the function",
  "function_name": "models.Player.get_name",
  "level": "info",
  "result": "Diane",
  "timestamp": "2021-02-23T14:21:41.464183Z",
  "trace_id": "8fdbd416-d109-4671-8b57-cc28a9ebbbe6"
}
{
  "args": [],
  "event": "Before calling the function",
  "function_name": "models.Player.get_age_in_cat_live",
  "kwargs": {},
  "level": "info",
  "timestamp": "2021-02-23T14:21:41.464245Z",
  "trace_id": "1f18db5d-c8dc-4886-8639-2a3baf96ce45"
}
{
  "event": "trying to get the aage in cat referencial",
  "level": "info",
  "timestamp": "2021-02-23T14:21:41.464285Z",
  "trace_id": "1f18db5d-c8dc-4886-8639-2a3baf96ce45"
}
{
  "event": "get cat referential",
  "level": "info",
  "timestamp": "2021-02-23T14:21:41.464313Z",
  "trace_id": "1f18db5d-c8dc-4886-8639-2a3baf96ce45"
}
{
  "event": "After calling the function",
  "function_name": "models.Player.get_age_in_cat_live",
  "level": "info",
  "result": 210,
  "timestamp": "2021-02-23T14:21:41.464336Z",
  "trace_id": "1f18db5d-c8dc-4886-8639-2a3baf96ce45"
}

Process finished with exit code 0

References

Full project

In this Github repo

This post is licensed under CC BY 4.0 by the author.