diff options
Diffstat (limited to 'g4f/api')
-rw-r--r-- | g4f/api/__init__.py | 8 | ||||
-rw-r--r-- | g4f/api/_logging.py | 36 |
2 files changed, 23 insertions, 21 deletions
diff --git a/g4f/api/__init__.py b/g4f/api/__init__.py index 754a48f1..3e29c5f8 100644 --- a/g4f/api/__init__.py +++ b/g4f/api/__init__.py @@ -22,6 +22,8 @@ from g4f.client import Client, ChatCompletion, ChatCompletionChunk, ImagesRespon from g4f.typing import Messages from g4f.cookies import read_cookie_files +logger = logging.getLogger(__name__) + def create_app(g4f_api_key: str = None): app = FastAPI() @@ -200,14 +202,14 @@ class Api: except GeneratorExit: pass except Exception as e: - logging.exception(e) + logger.exception(e) yield f'data: {format_exception(e, config)}\n\n' yield "data: [DONE]\n\n" return StreamingResponse(streaming(), media_type="text/event-stream") except Exception as e: - logging.exception(e) + logger.exception(e) return Response(content=format_exception(e, config), status_code=500, media_type="application/json") @self.app.post("/v1/images/generate") @@ -222,7 +224,7 @@ class Api: response_data = [image.to_dict() for image in response.data] return JSONResponse({"data": response_data}) except Exception as e: - logging.exception(e) + logger.exception(e) return Response(content=format_exception(e, config), status_code=500, media_type="application/json") @self.app.post("/v1/completions") diff --git a/g4f/api/_logging.py b/g4f/api/_logging.py index e91dff76..884d7529 100644 --- a/g4f/api/_logging.py +++ b/g4f/api/_logging.py @@ -1,6 +1,6 @@ import sys,logging -from loguru import logger +#from loguru import logger def __exception_handle(e_type, e_value, e_traceback): if issubclass(e_type, KeyboardInterrupt): @@ -9,24 +9,24 @@ def __exception_handle(e_type, e_value, e_traceback): sys.__excepthook__(e_type, e_value, e_traceback) -class __InterceptHandler(logging.Handler): - def emit(self, record): - try: - level = logger.level(record.levelname).name - except ValueError: - level = record.levelno - - frame, depth = logging.currentframe(), 2 - while 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() - ) +#class __InterceptHandler(logging.Handler): +# def emit(self, record): +# try: +# level = logger.level(record.levelname).name +# except ValueError: +# level = record.levelno +# +# frame, depth = logging.currentframe(), 2 +# while 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() +# ) def hook_except_handle(): sys.excepthook = __exception_handle -def hook_logging(**kwargs): - logging.basicConfig(handlers=[__InterceptHandler()], **kwargs) +#def hook_logging(**kwargs): +# logging.basicConfig(handlers=[__InterceptHandler()], **kwargs) |