是否可以在 Nginx 上指定自定义错误日志格式?

发布于 2024-10-04 03:23:40 字数 105 浏览 0 评论 0原文

我可以在 Nginx 上为 access_log 指定自定义日志格式,但它不适用于 error_log

有办法实现这个目标吗?

I can specify custom log format for access_log on Nginx, but it doesn't work for error_log.

Is there anyway to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

还给你自由 2024-10-11 03:23:40

你不能指定自己的格式,但在 nginx 中内置了多个级别的 error_log-ing。

语法:error_log file [ debug |信息 |通知 |警告|错误| crit ]

默认值:${prefix}/logs/error.log

指定记录服务器(和 fastcgi)错误的文件。

错误级别的默认值:

  1. 在主要部分中 -
  2. HTTP 部分中的错误 -
  3. 服务器部分中的 crit - crit

在我的 error_log 中,时间始终以 int 形式出现在日志中每个错误字符串的开头。

You can't specify your own format, but in nginx build-in several level's of error_log-ing.

Syntax: error_log file [ debug | info | notice | warn | error | crit ]

Default: ${prefix}/logs/error.log

Specifies the file where server (and fastcgi) errors are logged.

Default values for the error level:

  1. in the main section - error
  2. in the HTTP section - crit
  3. in the server section - crit

In my error_log, time always presented int begin of each error string in log.

秋意浓 2024-10-11 03:23:40

有一个技巧可以解决这个问题。

我们知道我们可以自定义访问日志格式,但不能自定义错误日志格式。因此,对于自定义错误日志,我们的技巧是,仅在发生错误时才生成访问日志。

这可以使用 error_page 指令来完成。

http {
...
  log_format custom_combined "...";
  server {
    ...
    error_page 50x @create_custom_error50x;
    ...
    location @create_custom_error50x {
      access_log path custom_combined;
      return 50x;
    }
  }
}

There is a hack for that.

We know that we can customize the access log format but not error log format. So the hack is, for customized error log, we generate access log only when error occurs.

This can be done using error_page directive.

http {
...
  log_format custom_combined "...";
  server {
    ...
    error_page 50x @create_custom_error50x;
    ...
    location @create_custom_error50x {
      access_log path custom_combined;
      return 50x;
    }
  }
}
心凉 2024-10-11 03:23:40

当我想要更改 nginx 错误日志的格式时(在本例中,当使用 Lua 中的 openresty 的 ngx.log 方法发送我自己的日志时),我使用了一个肮脏的技巧,即在我自己的日志消息中添加前缀足够的 \b(退格)字符来删除运行 tail -f error.log 时我不感兴趣查看的所有信息。

A dirty trick I used when I wanted to change the format of the nginx error log (in this case when sending my own logs with openresty's ngx.log method from Lua) was to prefix my own log message with enough \b (backspace) characters to delete all the information I wasn't interested in viewing when running a tail -f error.log.

傲鸠 2024-10-11 03:23:40

@Satys 上面的回答非常有启发性。然而,他的例子可能会让您相信您必须提前选择一个特定的返回代码(例如502),然后在该段的末尾返回502。这进一步意味着,如果您想处理第二个返回代码(例如,404),您需要在 nginx.conf 中创建第二个类似的段>。

使用 nginx v1.20.0,我可以像这样组合它们:

    log_format my_format ...

    server {
        error_page 404 /404.html;
        location = /404.html {
            access_log /var/log/nginx/access_4xx_5xx.log my_format;
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            access_log /var/log/nginx/access_4xx_5xx.log my_format;
        }
        ...
    }

上面的示例完成以下任务:

  1. error_page 404 映射到 HTML 页面 (/404.html)这与 error_page 500 502 503 504 映射到 (/50x.html) 的内容不同;这部分与开箱即用的默认 nginx.conf 相同。这允许您根据不同的状态代码呈现不同的用户友好消息。

  2. 以上两个段都记录到同一个自定义文件access_4xx_5xx.log(并且都采用my_format)。这使您可以将这些自定义日志合并到一个文件中,而不是拥有大量日志文件。

  3. 每个段末尾没有return 50x。 Nginx 将只返回原始状态代码。

@Satys's answer above is pretty enlightening. However, his example might lead you to believe that you have to pick one specific return code (e.g., 502) in advance and then return 502 at the end of that segment. And that would further imply that, if you want to handle a second return code (e.g., 404), you'd need to create a second, similar segment in nginx.conf.

Using nginx v1.20.0, I can combine them like this:

    log_format my_format ...

    server {
        error_page 404 /404.html;
        location = /404.html {
            access_log /var/log/nginx/access_4xx_5xx.log my_format;
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            access_log /var/log/nginx/access_4xx_5xx.log my_format;
        }
        ...
    }

The above example accomplishes the following:

  1. error_page 404 maps to an HTML page (/404.html) that is different from what error_page 500 502 503 504 maps to (/50x.html); This part is the same as the out-of-the-box default nginx.conf. This allows you to present different user-friendly messages based on different status codes.

  2. Both segments above log to the same custom file access_4xx_5xx.log (and both in my_format). This allows you to consolidate those custom logs into one file rather than having a proliferation of log files.

  3. There is no return 50x at the end of each segment. Nginx will just return the original status code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文