error_log_write
The `error_log_write` directive allows conditional logging of error messages based on specified criteria in NGINX configurations.
Description
The error_log_write directive is part of the ngx_http_error_log_write_module and enables the writing of custom error log entries conditionally based on the specified parameters. This directive supports various contexts, including http, server, and location, providing flexibility in configuring error logging at different levels of the server hierarchy.
The directive accepts up to three parameters: level, message, and optional if condition. The level parameter specifies the severity of the log entry (e.g., info, warn), while the message parameter contains the text to be logged. The if condition determines when the log should be written. If the condition is met (the result evaluates to true), NGINX logs the specified message at the defined log level. If the condition is not met and the negative flag is set, the message will not be logged.
The directive is primarily useful for creating granular logging strategies based on runtime conditions, helping developers to manage log noise and focus on important events relevant to specific operational contexts.
Config Example
server {
listen 127.0.0.1:80;
server_name localhost;
error_log_write message="server test log" if=$arg_test;
location / {
error_log_write level=warn message="auth required" if!=$http_authorization;
auth_basic "auth required";
auth_basic_user_file conf/htpasswd;
proxy_pass http://example.upstream.com;
}
}Ensure that the if condition evaluates correctly to avoid unintended log omissions.
Check NGINX's log level settings to ensure the specified log level captures the messages as intended.
Revisit the matching conditions regularly, especially if conditions depend on changes in application logic or request parameters.