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

如何在Gatsby中关闭函数的执行日志?

Gatsby 函数执行日志关闭方法

单独关闭单个函数的日志

在对应函数的导出项中添加disableLogs: true配置即可,仅对当前函数生效:

// src/api/[你的函数名].js
// 顶部添加这行配置
exports.disableLogs = true

export default function handler(req, res) {
  // 原有业务逻辑
  res.status(200).json({ data: 'your response' })
}

批量关闭所有/api路径下的函数日志

如果需要统一过滤所有接口的执行日志,可在项目根目录的gatsby-node.js中添加钩子拦截日志输出:

// gatsby-node.js
exports.onCreateDevServer = ({ reporter }) => {
  const originalInfoLog = reporter.info
  reporter.info = (...logArgs) => {
    const logContent = logArgs[0] || ''
    // 过滤所有匹配/api路径的函数执行日志
    if (!logContent.startsWith('Executed function "/api/')) {
      originalInfoLog(...logArgs)
    }
  }
}

如果需要生产环境也关闭这类日志,启动服务时添加日志级别参数即可:
gatsby serve --log-level warn

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 19:45:09