-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger_setup.py
63 lines (49 loc) · 1.48 KB
/
logger_setup.py
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
56
57
58
59
60
61
62
63
import logging
import logging.config
import os
from pathlib import Path
from sys import getdefaultencoding
import yaml
class HealthCheckFilter(logging.Filter):
list_filtered_endpoints = [
"GET / ",
"GET /docs/openapi.json ",
"GET /health ",
"GET /status ",
"GET /versions ",
]
def filter(self, record):
if record.name != "uvicorn.access":
return True
for filtered_endpoint in self.list_filtered_endpoints:
if record.getMessage().find(filtered_endpoint) != -1:
return False
return True
class StdErrFilter(logging.Filter):
"""
Filter for the stderr stream
Doesn't print records below ERROR to stderr to avoid dupes
"""
def filter(self, record):
return record.levelno >= logging.ERROR
class StdOutFilter(logging.Filter):
"""
Filter for the stdout stream
Doesn't print records at ERROR or above to stdout to avoid dupes
"""
def filter(self, record):
return record.levelno < logging.ERROR
def setup_logging():
"""
Setup logging configuration
"""
default_path = (
"prod_logging.yaml"
if os.getenv("DEBUG", "False").upper() == "FALSE"
else "dev_logging.yaml"
)
path = Path(__file__).parent / default_path
with open(path, "rt", encoding=getdefaultencoding()) as f:
config = yaml.safe_load(f)
logging.config.dictConfig(config)
logging.info("Loaded logging config from file")