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

QGIS Python脚本无法生成日志文件问题求助

QGIS Python日志文件输出失败的排查与解决

可能的问题点及修复方案

1. 目录权限或存在性问题

  • 先确认C:\temp目录是否存在,不存在则先创建:
    import os
    log_dir = r'c:\temp'
    if not os.path.exists(log_dir):
        os.makedirs(log_dir)
    
  • 检查QGIS是否有该目录的写入权限,可尝试右键QGIS快捷方式选择「以管理员身份运行」测试,或者将日志路径切换到用户专属目录(比如r'C:\Users\你的用户名\Documents\qgis_logs\example.log')

2. QGIS日志系统覆盖默认配置

QGIS启动时已初始化logging模块,直接调用logging.basicConfig()可能无法生效,需手动创建日志处理器:

import logging
import os

# 确保日志目录存在
log_dir = r'c:\temp'
if not os.path.exists(log_dir):
    os.makedirs(log_dir)
log_path = os.path.join(log_dir, 'example.log')

# 创建专属logger,避免干扰QGIS原有日志
logger = logging.getLogger('custom_qgis_logger')
logger.setLevel(logging.INFO)

# 防止重复添加处理器
if not logger.handlers:
    file_handler = logging.FileHandler(log_path)
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    file_handler.setFormatter(formatter)
    logger.addHandler(file_handler)

# 写入日志
logger.info("This logging info text goes into the file")
# 若需记录debug信息,将logger.setLevel改为logging.DEBUG
logger.debug("This debug text requires DEBUG level to be logged")

3. 日志级别匹配问题

你当前设置的日志级别是INFO,logging.debug()的内容因级别低于INFO不会被写入。如果需要记录debug信息,需将logging.basicConfig里的level参数改为logging.DEBUG,或者在自定义logger里设置logger.setLevel(logging.DEBUG)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 09:31:33