Coverage for api/logging_config.py: 88%

20 statements  

« prev     ^ index     » next       coverage.py v7.6.2, created at 2024-10-10 03:02 +0300

1import logging 

2import os 

3 

4import config 

5 

6FORMAT = logging.Formatter( 

7 "%(asctime)s.%(msecs)03d; %(levelname)s; %(processName)s; %(name)s; %(funcName)s=%(lineno)d; %(message)s", 

8 "%Y-%m-%d %H:%M:%S", 

9) 

10DEV_FORMAT = logging.Formatter( 

11 "%(asctime)s.%(msecs)03d; %(levelname)s; SOTRANS_MAIN_BACKEND; module=%(name)s; %(funcName)s=%(lineno)d; msg=%(message)s", 

12 "%Y-%m-%d %H:%M:%S", 

13) 

14 

15if not os.path.exists("logs"): 15 ↛ 16line 15 didn't jump to line 16 because the condition on line 15 was never true

16 os.mkdir("logs") 

17 

18LOGFILE = "logs/short.log" 

19DEV_LOGFILE = "logs/long.log" 

20 

21# logger = logging.getLogger("uvicorn.access") 

22logger = logging.getLogger("" if config.TESTING else "uvicorn") 

23logger.setLevel(config.LOG_LEVEL) # *default* output level 

24 

25# StreamHandler sends to stderr by default 

26h = logging.StreamHandler() 

27h.setLevel(config.LOG_LEVEL) # output level for *this handler* 

28h.setFormatter(FORMAT) 

29 

30if config.ENVIRONMENT == "local": 30 ↛ 39line 30 didn't jump to line 39 because the condition on line 30 was always true

31 # outputs *everything* to a seperate file, good for debugging during dev 

32 h3 = logging.FileHandler(DEV_LOGFILE) 

33 # not neccessary as the root logger outputs at DEBUG 

34 h3.setLevel(logging.DEBUG) 

35 # for dev you might want a friendlier logging format 

36 h3.setFormatter(DEV_FORMAT) 

37 logger.addHandler(h3) 

38 

39logger.addHandler(h)