You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用logging.basicConfig传入encoding参数触发ValueError的原因咨询

问题原因及解决办法

核心原因

你使用的是Python 3.8版本,而encoding参数是Python 3.9才新增到logging.basicConfig()中的,3.8及更早版本的basicConfig并不支持这个参数,所以会抛出"未识别参数"的错误。

解决办法

有两种可行方案:

  • 升级Python版本到3.9及以上:直接保留你原有的代码即可正常运行。
  • 手动创建FileHandler指定编码(兼容3.8):
    如果无法升级版本,可以绕过basicConfig,手动配置日志处理器:
    import logging
    
    logger = logging.getLogger()
    logger.setLevel(logging.DEBUG)
    # 创建FileHandler并指定编码
    handler = logging.FileHandler('example.log', encoding='utf-8')
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    

内容的提问来源于stack exchange,提问作者prat2194

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 21:07:00