Lucas Hansen commited on
Commit
99210cc
1 Parent(s): 16fff61

Create loggers.py

Browse files
Files changed (1) hide show
  1. loggers.py +103 -0
loggers.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+ from logging.config import dictConfig
3
+
4
+ from contextvars import ContextVar
5
+
6
+ request_id: ContextVar[str] = ContextVar('request_id', default='')
7
+ request_session: ContextVar[dict] = ContextVar('request_session', default={})
8
+
9
+
10
+ # import logging
11
+
12
+ class RequestIDFilter(logging.Filter):
13
+
14
+ def filter(self, record: logging.LogRecord) -> bool:
15
+ record.request_id = request_id.get()
16
+ return True
17
+
18
+
19
+ log_config = {
20
+ "disable_existing_loggers": False,
21
+ 'version': 1,
22
+ 'formatters': {
23
+ 'default': {
24
+ 'format': '[%(asctime)s] %(levelname)s %(pathname)s %(module)s %(lineno)d -> %(message)s',
25
+ 'datefmt': '%Y-%m-%d %H:%M:%S %z'
26
+ }},
27
+ 'handlers': {'console': {
28
+ 'class': 'logging.StreamHandler',
29
+ 'formatter': 'default',
30
+ "stream": "ext://sys.stdout"
31
+ }},
32
+ "loggers": {
33
+ 'root': {
34
+ 'level': 'INFO',
35
+ 'handlers': ['console']
36
+ }
37
+ }
38
+ }
39
+
40
+ log_config = {
41
+ "version": 1,
42
+ "disable_existing_loggers": False,
43
+ 'filters': {
44
+ 'request_id_filter': {
45
+ "()": RequestIDFilter
46
+ }
47
+ },
48
+ "formatters": {
49
+ 'access': {
50
+ '()': 'uvicorn.logging.AccessFormatter',
51
+ 'fmt': '[%(asctime)s] [%(request_id)s] %(levelprefix)s - %(client_addr)s - "%(request_line)s" %(status_code)s',
52
+ "datefmt": "%Y-%m-%d %H:%M:%S %z",
53
+ "use_colors": True
54
+ },
55
+ "default": {
56
+ "()": "uvicorn.logging.DefaultFormatter",
57
+ 'fmt': '[%(asctime)s] [%(request_id)s] %(levelname)s %(pathname)s %(module)s %(lineno)d -> %(message)s',
58
+ # 'fmt': '[%(asctime)s] %(levelname)s %(pathname)s %(module)s %(lineno)d -> %(message)s',
59
+ 'datefmt': '%Y-%m-%d %H:%M:%S %z',
60
+ "use_colors": True
61
+ },
62
+ },
63
+ "handlers": {
64
+ 'access': {
65
+ 'class': 'logging.StreamHandler',
66
+ 'formatter': 'access',
67
+ 'stream': 'ext://sys.stdout',
68
+ "filters": ['request_id_filter']
69
+
70
+ },
71
+ "default": {
72
+ "formatter": "default",
73
+ "class": "logging.StreamHandler",
74
+ "stream": "ext://sys.stdout",
75
+ "filters": ['request_id_filter']
76
+ },
77
+ },
78
+ "loggers": {
79
+ "log": {
80
+ "handlers": ["default"],
81
+ "level": "INFO",
82
+ "propagate": False
83
+ },
84
+ "uvicorn": {
85
+ "handlers": ["default"],
86
+ "level": "INFO",
87
+ "propagate": True
88
+ },
89
+ 'uvicorn.access': {
90
+ 'handlers': ['access'],
91
+ 'level': 'INFO',
92
+ 'propagate': False
93
+ },
94
+ 'uvicorn.error': {
95
+ 'level': 'INFO',
96
+ 'propagate': False
97
+ }
98
+ },
99
+ }
100
+
101
+ dictConfig(log_config)
102
+
103
+ logger = logging.getLogger("log")