Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: 修复控制台日志输出emoji时报错 #923

Closed
the-lazy-me opened this issue Nov 17, 2024 · 3 comments
Closed

[Bug]: 修复控制台日志输出emoji时报错 #923

the-lazy-me opened this issue Nov 17, 2024 · 3 comments
Labels
bug? Bug或Bug修复相关

Comments

@the-lazy-me
Copy link

消息平台适配器

None

运行环境

Win

LangBot 版本

v3.4

异常情况

[11-17 22:03:24.741] app.py (170) - [INFO] : 🤯 WebUI 仍处于 Beta 测试阶段,如有问题或建议请反馈到 https://github.com/RockChinQ/LangBot/issues
--- Logging error ---
Traceback (most recent call last):
  File "C:\Python310\lib\logging\__init__.py", line 1103, in emit
    stream.write(msg + self.terminator)
UnicodeEncodeError: 'gbk' codec can't encode character '\U0001f92f' in position 50: illegal multibyte sequence
Call stack:
  File "F:\langbot-v3.4.0-all\main.py", line 83, in <module>
    loop.run_until_complete(main_entry(loop))
  File "C:\Python310\lib\asyncio\base_events.py", line 636, in run_until_complete
    self.run_forever()
  File "C:\Python310\lib\asyncio\windows_events.py", line 321, in run_forever
    super().run_forever()
  File "C:\Python310\lib\asyncio\base_events.py", line 603, in run_forever
    self._run_once()
  File "C:\Python310\lib\asyncio\base_events.py", line 1906, in _run_once
    handle._run()
  File "C:\Python310\lib\asyncio\events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
  File "F:\langbot-v3.4.0-all\main.py", line 53, in main_entry
    await boot.main(loop)
  File "F:\langbot-v3.4.0-all\pkg\core\boot.py", line 67, in main
    await app_inst.run()
  File "F:\langbot-v3.4.0-all\pkg\core\app.py", line 131, in run
    await self.print_web_access_info()
  File "F:\langbot-v3.4.0-all\pkg\core\app.py", line 170, in print_web_access_info
    self.logger.info(line)
Message: '🤯 WebUI 仍处于 Beta 测试阶段,如有问题或建议请反馈到 https://github.com/RockChinQ/LangBot/issues'
Arguments: ()
[11-17 22:03:24.742] app.py (170) - [INFO] : =======================================

修改建议?
修改后的 pkg/core/bootutils/log.py

import logging
import os
import sys
import time

import colorlog

from ...utils import constants


log_colors_config = {
    "DEBUG": "green",  # cyan white
    "INFO": "white",
    "WARNING": "yellow",
    "ERROR": "red",
    "CRITICAL": "cyan",
}


async def init_logging(extra_handlers: list[logging.Handler] = None) -> logging.Logger:
    # 删除所有现有的logger
    for handler in logging.root.handlers[:]:
        logging.root.removeHandler(handler)

    level = logging.INFO

    if constants.debug_mode:
        level = logging.DEBUG

    log_file_name = "data/logs/langbot-%s.log" % time.strftime(
        "%Y-%m-%d", time.localtime()
    )

    qcg_logger = logging.getLogger("qcg")

    qcg_logger.setLevel(level)

    color_formatter = colorlog.ColoredFormatter(
        fmt="%(log_color)s[%(asctime)s.%(msecs)03d] %(filename)s (%(lineno)d) - [%(levelname)s] : %(message)s",
        datefmt="%m-%d %H:%M:%S",
        log_colors=log_colors_config,
    )

    stream_handler = logging.StreamHandler(sys.stdout)
    stream_handler.setLevel(level)
    stream_handler.setFormatter(color_formatter)
    stream_handler.stream = open(sys.stdout.fileno(), mode='w', encoding='utf-8', buffering=1)

    log_handlers: list[logging.Handler] = [stream_handler, logging.FileHandler(log_file_name, encoding='utf-8')]
    log_handlers += extra_handlers if extra_handlers is not None else []

    for handler in log_handlers:
        handler.setLevel(level)
        handler.setFormatter(color_formatter)
        qcg_logger.addHandler(handler)

    qcg_logger.debug("日志初始化完成,日志级别:%s" % level)
    logging.basicConfig(
        level=logging.CRITICAL,  # 设置日志输出格式
        format="[DEPR][%(asctime)s.%(msecs)03d] %(pathname)s (%(lineno)d) - [%(levelname)s] :\n%(message)s",
        # 日志输出的格式
        # -8表示占位符,让输出左对齐,输出长度都为8位
        datefmt="%Y-%m-%d %H:%M:%S",  # 时间输出的格式
        handlers=[logging.NullHandler()],
    )

    return qcg_logger

启用的插件

No response

@the-lazy-me the-lazy-me added the bug? Bug或Bug修复相关 label Nov 17, 2024
@RockChinQ
Copy link
Owner

你pr一下吧,我已经躺床上了。

@the-lazy-me
Copy link
Author

the-lazy-me commented Nov 17, 2024

我再看看别的,似乎消息适配器配置页有bug,默认情况下,这些点不动(包括其他的),不能修改内容
image

@RockChinQ
Copy link
Owner

已修复。编辑消息平台适配器那里,要点击折叠面板右上角一个笔的按钮才能编辑。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug? Bug或Bug修复相关
Projects
None yet
Development

No branches or pull requests

2 participants