feat: InterceptHandler with no depth on no backtrace
This commit is contained in:
		
							parent
							
								
									04995f1934
								
							
						
					
					
						commit
						fdfebc87d8
					
				|  | @ -11,9 +11,8 @@ from joint_teapot.config import settings | |||
| 
 | ||||
| # recipe from https://loguru.readthedocs.io/en/stable/overview.html#entirely-compatible-with-standard-logging | ||||
| class InterceptHandler(logging.Handler): | ||||
|     def __init__(self, diagnose: bool = True, backtrace: bool = True): | ||||
|     def __init__(self, backtrace: bool = True): | ||||
|         super().__init__() | ||||
|         self.diagnose = diagnose | ||||
|         self.backtrace = backtrace | ||||
| 
 | ||||
|     def emit(self, record: logging.LogRecord) -> None: | ||||
|  | @ -22,16 +21,16 @@ class InterceptHandler(logging.Handler): | |||
|         except ValueError: | ||||
|             level = record.levelno | ||||
| 
 | ||||
|         logger_opt = logger.opt(depth=0) | ||||
|         if self.backtrace: | ||||
|             # Find caller from where originated the logged message | ||||
|             frame: Optional[FrameType] = sys._getframe(6) | ||||
|             depth = 6 | ||||
|             while frame and frame.f_code.co_filename == logging.__file__: | ||||
|                 frame = frame.f_back | ||||
|                 depth += 1 | ||||
| 
 | ||||
|         logger.opt(depth=depth, exception=record.exc_info).log( | ||||
|             level, record.getMessage(), diagnose=self.diagnose, backtrace=self.backtrace | ||||
|         ) | ||||
|             logger_opt = logger.opt(depth=depth, exception=record.exc_info) | ||||
|         logger_opt.log(level, record.getMessage()) | ||||
| 
 | ||||
| 
 | ||||
| def set_logger( | ||||
|  | @ -40,9 +39,7 @@ def set_logger( | |||
|     diagnose: bool = True, | ||||
|     backtrace: bool = True, | ||||
| ) -> None: | ||||
|     logging.basicConfig( | ||||
|         handlers=[InterceptHandler(diagnose, backtrace)], level=0, force=True | ||||
|     ) | ||||
|     logging.basicConfig(handlers=[InterceptHandler(backtrace)], level=0, force=True) | ||||
|     logger.remove() | ||||
|     logger.add( | ||||
|         stderr, | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	Block a user