HTTP: enhanced access log with conditional filtering.

This feature allows users to specify conditions to control if access log
should be recorded. The "if" option supports a string and JavaScript code.
If its value is empty, 0, false, null, or undefined, the logs will not be
recorded. And the '!' as a prefix inverses the condition.

Example 1: Only log requests that sent a session cookie.

    {
        "access_log": {
            "if": "$cookie_session",
            "path": "..."
        }
    }

Example 2: Do not log health check requests.

    {
        "access_log": {
            "if": "`${uri == '/health' ? false : true}`",
            "path": "..."
        }
    }

Example 3: Only log requests when the time is before 22:00.

    {
        "access_log": {
            "if": "`${new Date().getHours() < 22}`",
            "path": "..."
        }
    }

or

    {
        "access_log": {
            "if": "!`${new Date().getHours() >= 22}`",
            "path": "..."
        }
    }

Closes: https://github.com/nginx/unit/issues/594
This commit is contained in:
Zhidao HONG
2024-01-23 18:57:30 +08:00
parent 37abe2e463
commit 4c91bebb50
4 changed files with 112 additions and 6 deletions

View File

@@ -77,6 +77,8 @@ static nxt_int_t nxt_conf_vldt_error(nxt_conf_validation_t *vldt,
const char *fmt, ...); const char *fmt, ...);
static nxt_int_t nxt_conf_vldt_var(nxt_conf_validation_t *vldt, nxt_str_t *name, static nxt_int_t nxt_conf_vldt_var(nxt_conf_validation_t *vldt, nxt_str_t *name,
nxt_str_t *value); nxt_str_t *value);
static nxt_int_t nxt_conf_vldt_if(nxt_conf_validation_t *vldt,
nxt_conf_value_t *value, void *data);
nxt_inline nxt_int_t nxt_conf_vldt_unsupported(nxt_conf_validation_t *vldt, nxt_inline nxt_int_t nxt_conf_vldt_unsupported(nxt_conf_validation_t *vldt,
nxt_conf_value_t *value, void *data) nxt_conf_value_t *value, void *data)
NXT_MAYBE_UNUSED; NXT_MAYBE_UNUSED;
@@ -1369,6 +1371,10 @@ static nxt_conf_vldt_object_t nxt_conf_vldt_access_log_members[] = {
}, { }, {
.name = nxt_string("format"), .name = nxt_string("format"),
.type = NXT_CONF_VLDT_STRING, .type = NXT_CONF_VLDT_STRING,
}, {
.name = nxt_string("if"),
.type = NXT_CONF_VLDT_STRING,
.validator = nxt_conf_vldt_if,
}, },
NXT_CONF_VLDT_END NXT_CONF_VLDT_END
@@ -1538,6 +1544,37 @@ nxt_conf_vldt_var(nxt_conf_validation_t *vldt, nxt_str_t *name,
} }
static nxt_int_t
nxt_conf_vldt_if(nxt_conf_validation_t *vldt, nxt_conf_value_t *value,
void *data)
{
nxt_str_t str;
static nxt_str_t if_str = nxt_string("if");
if (nxt_conf_type(value) != NXT_CONF_STRING) {
return nxt_conf_vldt_error(vldt, "The \"if\" must be a string");
}
nxt_conf_get_string(value, &str);
if (str.length == 0) {
return NXT_OK;
}
if (str.start[0] == '!') {
str.start++;
str.length--;
}
if (nxt_is_tstr(&str)) {
return nxt_conf_vldt_var(vldt, &if_str, &str);
}
return NXT_OK;
}
typedef struct { typedef struct {
nxt_mp_t *pool; nxt_mp_t *pool;
nxt_str_t *type; nxt_str_t *type;

View File

@@ -24,8 +24,8 @@ static void nxt_http_request_proto_info(nxt_task_t *task,
static void nxt_http_request_mem_buf_completion(nxt_task_t *task, void *obj, static void nxt_http_request_mem_buf_completion(nxt_task_t *task, void *obj,
void *data); void *data);
static void nxt_http_request_done(nxt_task_t *task, void *obj, void *data); static void nxt_http_request_done(nxt_task_t *task, void *obj, void *data);
static void nxt_http_request_access_log(nxt_task_t *task, nxt_http_request_t *r, static nxt_int_t nxt_http_request_access_log(nxt_task_t *task,
nxt_router_conf_t *rtcf); nxt_http_request_t *r, nxt_router_conf_t *rtcf);
static u_char *nxt_http_date_cache_handler(u_char *buf, nxt_realtime_t *now, static u_char *nxt_http_date_cache_handler(u_char *buf, nxt_realtime_t *now,
struct tm *tm, size_t size, const char *format); struct tm *tm, size_t size, const char *format);
@@ -818,6 +818,7 @@ nxt_http_request_error_handler(nxt_task_t *task, void *obj, void *data)
void void
nxt_http_request_close_handler(nxt_task_t *task, void *obj, void *data) nxt_http_request_close_handler(nxt_task_t *task, void *obj, void *data)
{ {
nxt_int_t ret;
nxt_http_proto_t proto; nxt_http_proto_t proto;
nxt_router_conf_t *rtcf; nxt_router_conf_t *rtcf;
nxt_http_request_t *r; nxt_http_request_t *r;
@@ -834,10 +835,12 @@ nxt_http_request_close_handler(nxt_task_t *task, void *obj, void *data)
r->logged = 1; r->logged = 1;
if (rtcf->access_log != NULL) { if (rtcf->access_log != NULL) {
nxt_http_request_access_log(task, r, rtcf); ret = nxt_http_request_access_log(task, r, rtcf);
if (ret == NXT_OK) {
return; return;
} }
} }
}
nxt_debug(task, "http request close handler"); nxt_debug(task, "http request close handler");
@@ -865,15 +868,54 @@ nxt_http_request_close_handler(nxt_task_t *task, void *obj, void *data)
} }
static void static nxt_int_t
nxt_http_request_access_log(nxt_task_t *task, nxt_http_request_t *r, nxt_http_request_access_log(nxt_task_t *task, nxt_http_request_t *r,
nxt_router_conf_t *rtcf) nxt_router_conf_t *rtcf)
{ {
nxt_int_t ret;
nxt_str_t str;
nxt_bool_t expr;
nxt_router_access_log_t *access_log; nxt_router_access_log_t *access_log;
access_log = rtcf->access_log; access_log = rtcf->access_log;
expr = 1;
if (rtcf->log_expr != NULL) {
if (nxt_tstr_is_const(rtcf->log_expr)) {
nxt_tstr_str(rtcf->log_expr, &str);
} else {
ret = nxt_tstr_query_init(&r->tstr_query, rtcf->tstr_state,
&r->tstr_cache, r, r->mem_pool);
if (nxt_slow_path(ret != NXT_OK)) {
return NXT_DECLINED;
}
nxt_tstr_query(task, r->tstr_query, rtcf->log_expr, &str);
if (nxt_slow_path(nxt_tstr_query_failed(r->tstr_query))) {
return NXT_DECLINED;
}
}
if (str.length == 0
|| nxt_str_eq(&str, "0", 1)
|| nxt_str_eq(&str, "false", 5)
|| nxt_str_eq(&str, "null", 4)
|| nxt_str_eq(&str, "undefined", 9))
{
expr = 0;
}
}
if (rtcf->log_negate ^ expr) {
access_log->handler(task, r, access_log, rtcf->log_format); access_log->handler(task, r, access_log, rtcf->log_format);
return NXT_OK;
}
return NXT_DECLINED;
} }

View File

@@ -54,6 +54,8 @@ typedef struct {
nxt_router_access_log_t *access_log; nxt_router_access_log_t *access_log;
nxt_tstr_t *log_format; nxt_tstr_t *log_format;
nxt_tstr_t *log_expr;
uint8_t log_negate; /* 1 bit */
} nxt_router_conf_t; } nxt_router_conf_t;

View File

@@ -13,6 +13,7 @@
typedef struct { typedef struct {
nxt_str_t path; nxt_str_t path;
nxt_str_t format; nxt_str_t format;
nxt_conf_value_t *expr;
} nxt_router_access_log_conf_t; } nxt_router_access_log_conf_t;
@@ -53,6 +54,12 @@ static nxt_conf_map_t nxt_router_access_log_conf[] = {
NXT_CONF_MAP_STR, NXT_CONF_MAP_STR,
offsetof(nxt_router_access_log_conf_t, format), offsetof(nxt_router_access_log_conf_t, format),
}, },
{
nxt_string("if"),
NXT_CONF_MAP_PTR,
offsetof(nxt_router_access_log_conf_t, expr),
},
}; };
@@ -72,6 +79,8 @@ nxt_router_access_log_create(nxt_task_t *task, nxt_router_conf_t *rtcf,
"[$time_local] \"$request_line\" $status $body_bytes_sent " "[$time_local] \"$request_line\" $status $body_bytes_sent "
"\"$header_referer\" \"$header_user_agent\""); "\"$header_referer\" \"$header_user_agent\"");
nxt_memzero(&alcf, sizeof(nxt_router_access_log_conf_t));
alcf.format = log_format_str; alcf.format = log_format_str;
if (nxt_conf_type(value) == NXT_CONF_STRING) { if (nxt_conf_type(value) == NXT_CONF_STRING) {
@@ -133,6 +142,22 @@ nxt_router_access_log_create(nxt_task_t *task, nxt_router_conf_t *rtcf,
rtcf->access_log = access_log; rtcf->access_log = access_log;
rtcf->log_format = format; rtcf->log_format = format;
if (alcf.expr != NULL) {
nxt_conf_get_string(alcf.expr, &str);
if (str.length > 0 && str.start[0] == '!') {
rtcf->log_negate = 1;
str.start++;
str.length--;
}
rtcf->log_expr = nxt_tstr_compile(rtcf->tstr_state, &str, 0);
if (nxt_slow_path(rtcf->log_expr == NULL)) {
return NXT_ERROR;
}
}
return NXT_OK; return NXT_OK;
} }