Coverage for chatgpt_proxy / log / log.py: 50%

18 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-03-12 16:19 +0000

1# MIT License 

2# 

3# Copyright (c) 2025 Tuomo Kriikkula 

4# 

5# Permission is hereby granted, free of charge, to any person obtaining a copy 

6# of this software and associated documentation files (the "Software"), to deal 

7# in the Software without restriction, including without limitation the rights 

8# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 

9# copies of the Software, and to permit persons to whom the Software is 

10# furnished to do so, subject to the following conditions: 

11# 

12# The above copyright notice and this permission notice shall be included in all 

13# copies or substantial portions of the Software. 

14# 

15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 

16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 

17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 

18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 

19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 

20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 

21# SOFTWARE. 

22 

23# TODO: set up the usual stuff, log levels, etc. 

24# TODO: we'll probably want to inject Sanic client IP/port in this logger too? 

25 

26import logging 

27import sys 

28 

29from loguru import logger 

30 

31logger.remove() 

32 

33# TODO: sync context with Sanic! 

34logger.add(sys.stdout, enqueue=True, context="spawn") 

35 

36logger = logger 

37 

38 

39class InterceptHandler(logging.Handler): 

40 """ 

41 Add logging handler to augment python stdlib logging. 

42 

43 Logs which would otherwise go to stdlib logging are redirected through 

44 loguru. 

45 """ 

46 

47 @logger.catch(default=True, onerror=lambda _: sys.exit(1)) 

48 def emit(self, record): 

49 try: 

50 level = logger.level(record.levelname).name 

51 except ValueError: 

52 level = record.levelno 

53 

54 # Find caller from where originated the logged message. 

55 frame, depth = sys._getframe(6), 6 

56 while frame and frame.f_code.co_filename == logging.__file__: 

57 frame = frame.f_back 

58 depth += 1 

59 

60 logger.opt(depth=depth, exception=record.exc_info).log(level, record.getMessage()) 

61 

62# TODO: come up with the best way of handling multiple library loggers. 

63# logging.basicConfig(handlers=[InterceptHandler()], level=0, force=True)