summaryrefslogtreecommitdiffstats
path: root/g4f/api/_logging.py
diff options
context:
space:
mode:
authorabc <98614666+xtekky@users.noreply.github.com>2023-10-20 20:04:13 +0200
committerabc <98614666+xtekky@users.noreply.github.com>2023-10-20 20:04:13 +0200
commit8e7e694d81e674db63049145a35972df8ad2e3fa (patch)
treed6000ee808385a20f85e03e3173e3aa16a605757 /g4f/api/_logging.py
parent~ (diff)
downloadgpt4free-8e7e694d81e674db63049145a35972df8ad2e3fa.tar
gpt4free-8e7e694d81e674db63049145a35972df8ad2e3fa.tar.gz
gpt4free-8e7e694d81e674db63049145a35972df8ad2e3fa.tar.bz2
gpt4free-8e7e694d81e674db63049145a35972df8ad2e3fa.tar.lz
gpt4free-8e7e694d81e674db63049145a35972df8ad2e3fa.tar.xz
gpt4free-8e7e694d81e674db63049145a35972df8ad2e3fa.tar.zst
gpt4free-8e7e694d81e674db63049145a35972df8ad2e3fa.zip
Diffstat (limited to 'g4f/api/_logging.py')
-rw-r--r--g4f/api/_logging.py32
1 files changed, 32 insertions, 0 deletions
diff --git a/g4f/api/_logging.py b/g4f/api/_logging.py
new file mode 100644
index 00000000..e91dff76
--- /dev/null
+++ b/g4f/api/_logging.py
@@ -0,0 +1,32 @@
+import sys,logging
+
+from loguru import logger
+
+def __exception_handle(e_type, e_value, e_traceback):
+ if issubclass(e_type, KeyboardInterrupt):
+ print('\nBye...')
+ sys.exit(0)
+
+ 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()
+ )
+
+def hook_except_handle():
+ sys.excepthook = __exception_handle
+
+def hook_logging(**kwargs):
+ logging.basicConfig(handlers=[__InterceptHandler()], **kwargs)