Is it possible to specify custom error log format on Nginx?

后端 未结 3 2163
悲&欢浪女
悲&欢浪女 2021-01-01 10:39

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?

相关标签:
3条回答
  • 2021-01-01 11:13

    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.

    0 讨论(0)
  • 2021-01-01 11:17

    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.

    0 讨论(0)
  • 2021-01-01 11:29

    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;
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题