Skip to content

Commit

Permalink
Merge pull request #384 from ikun-moxiaofei/develop_for_v1.3.1
Browse files Browse the repository at this point in the history
 Optimize console log output
  • Loading branch information
xixihahaliu authored Jun 5, 2024
2 parents 349825a + ccba6bc commit 2ae7e20
Showing 1 changed file with 53 additions and 1 deletion.
54 changes: 53 additions & 1 deletion qanything_kernel/utils/custom_log.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
import logging
import sys

from concurrent_log_handler import ConcurrentRotatingFileHandler
import time
import os

# 创建自定义过滤器
class FilterOutMessages(logging.Filter):
def filter(self, record):

exclude_keywords = [
"local doc search retrieval_documents",
"prompt: 参考信息:",
"[debug] event_text =",
"retrieval_documents: [Document(page_content",
"reranked retrieval_documents: [Document(page_content=",
"'role': 'user'",
]

# 检查日志消息中是否包含要过滤掉的关键词
for keyword in exclude_keywords:
if keyword in record.msg:
return False # 如果包含关键词,则不记录该日志消息

if isinstance(record.msg, list):
return False

return True # 如果日志消息中不包含任何要过滤掉的关键词,则记录该消息



class CustomConcurrentRotatingFileHandler(ConcurrentRotatingFileHandler):
def doRollover(self):
Expand All @@ -24,6 +50,7 @@ def doRollover(self):
if not self.delay:
self.stream = self._open()


# 这是LogHandler的代码,用于将日志写入文件
# 获取当前时间作为日志文件名的一部分
current_time = time.strftime("%Y%m%d_%H%M%S")
Expand Down Expand Up @@ -73,7 +100,32 @@ def doRollover(self):

# 将 handler 添加到 logger 中,这样 logger 就可以使用这个 handler 来记录日志了
qa_logger.addHandler(qa_handler)
print(debug_logger, qa_logger)


''''''
# filter_out_messages = FilterOutMessages()
# 添加控制台日志处理器
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setLevel(logging.INFO) # 可以根据需要设置适当的日志级别
console_handler.setFormatter(formatter) # 使用与文件日志相同的格式
console_handler.addFilter(FilterOutMessages())
# 添加过滤器到处理器
debug_handler.addFilter(FilterOutMessages())
qa_handler.addFilter(FilterOutMessages())
# 将控制台处理器添加到日志记录器
# qa_logger.addHandler(console_handler)
debug_logger.addHandler(console_handler)



# 原来的 print,但是好像不能输出日志
# print(debug_logger, qa_logger)


# 测试日志输出
# debug_logger.info("This is a debug message")
# qa_logger.info("This is a QA message")


qa_logger.propagate = False # 关闭日志传播
debug_logger.propagate = False # 关闭日志传播

0 comments on commit 2ae7e20

Please sign in to comment.