Skip to content

Commit

Permalink
http: JSON format access logging support
Browse files Browse the repository at this point in the history
  • Loading branch information
hongzhidao committed Oct 25, 2024
1 parent ebd02c6 commit 996b9c6
Show file tree
Hide file tree
Showing 5 changed files with 292 additions and 59 deletions.
51 changes: 50 additions & 1 deletion src/nxt_conf_validation.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@ static nxt_int_t nxt_conf_vldt_server_weight(nxt_conf_validation_t *vldt,
nxt_conf_value_t *value, void *data);
static nxt_int_t nxt_conf_vldt_access_log(nxt_conf_validation_t *vldt,
nxt_conf_value_t *value, void *data);
static nxt_int_t nxt_conf_vldt_access_log_format(nxt_conf_validation_t *vldt,
nxt_conf_value_t *value, void *data);
static nxt_int_t nxt_conf_vldt_access_log_format_field(
nxt_conf_validation_t *vldt, const nxt_str_t *name,
nxt_conf_value_t *value);

static nxt_int_t nxt_conf_vldt_isolation(nxt_conf_validation_t *vldt,
nxt_conf_value_t *value, void *data);
Expand Down Expand Up @@ -1418,7 +1423,8 @@ static nxt_conf_vldt_object_t nxt_conf_vldt_access_log_members[] = {
.type = NXT_CONF_VLDT_STRING,
}, {
.name = nxt_string("format"),
.type = NXT_CONF_VLDT_STRING,
.type = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_OBJECT,
.validator = nxt_conf_vldt_access_log_format,
}, {
.name = nxt_string("if"),
.type = NXT_CONF_VLDT_STRING,
Expand Down Expand Up @@ -3523,3 +3529,46 @@ nxt_conf_vldt_access_log(nxt_conf_validation_t *vldt, nxt_conf_value_t *value,

return NXT_OK;
}


static nxt_int_t
nxt_conf_vldt_access_log_format(nxt_conf_validation_t *vldt,
nxt_conf_value_t *value, void *data)
{
static const nxt_str_t format = nxt_string("format");

if (nxt_conf_type(value) == NXT_CONF_OBJECT) {
return nxt_conf_vldt_object_iterator(vldt, value,
nxt_conf_vldt_access_log_format_field);
}

/* NXT_CONF_STRING */

return nxt_conf_vldt_access_log_format_field(vldt, &format, value);
}


static nxt_int_t
nxt_conf_vldt_access_log_format_field(nxt_conf_validation_t *vldt,
const nxt_str_t *name, nxt_conf_value_t *value)
{
nxt_str_t str;

if (name->length == 0) {
return nxt_conf_vldt_error(vldt, "In the access log format, the name "
"must not be empty.");
}

if (nxt_conf_type(value) != NXT_CONF_STRING) {
return nxt_conf_vldt_error(vldt, "In the access log format, the value "
"must be only string.");
}

nxt_conf_get_string(value, &str);

if (nxt_is_tstr(&str)) {
return nxt_conf_vldt_var(vldt, name, &str);
}

return NXT_OK;
}
39 changes: 20 additions & 19 deletions src/nxt_router.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ typedef struct nxt_http_request_s nxt_http_request_t;
#include <nxt_application.h>


typedef struct nxt_http_action_s nxt_http_action_t;
typedef struct nxt_http_routes_s nxt_http_routes_t;
typedef struct nxt_http_forward_s nxt_http_forward_t;
typedef struct nxt_upstream_s nxt_upstream_t;
typedef struct nxt_upstreams_s nxt_upstreams_t;
typedef struct nxt_router_access_log_s nxt_router_access_log_t;
typedef struct nxt_http_action_s nxt_http_action_t;
typedef struct nxt_http_routes_s nxt_http_routes_t;
typedef struct nxt_http_forward_s nxt_http_forward_t;
typedef struct nxt_upstream_s nxt_upstream_t;
typedef struct nxt_upstreams_s nxt_upstreams_t;
typedef struct nxt_router_access_log_s nxt_router_access_log_t;
typedef struct nxt_router_access_log_format_s nxt_router_access_log_format_t;


#define NXT_HTTP_ACTION_ERROR ((nxt_http_action_t *) -1)
Expand All @@ -39,22 +40,22 @@ typedef struct {


typedef struct {
uint32_t count;
uint32_t threads;
uint32_t count;
uint32_t threads;

nxt_mp_t *mem_pool;
nxt_tstr_state_t *tstr_state;
nxt_mp_t *mem_pool;
nxt_tstr_state_t *tstr_state;

nxt_router_t *router;
nxt_http_routes_t *routes;
nxt_upstreams_t *upstreams;
nxt_router_t *router;
nxt_http_routes_t *routes;
nxt_upstreams_t *upstreams;

nxt_lvlhsh_t mtypes_hash;
nxt_lvlhsh_t apps_hash;
nxt_lvlhsh_t mtypes_hash;
nxt_lvlhsh_t apps_hash;

nxt_router_access_log_t *access_log;
nxt_tstr_t *log_format;
nxt_tstr_cond_t log_cond;
nxt_tstr_cond_t log_cond;
nxt_router_access_log_t *access_log;
nxt_router_access_log_format_t *log_format;
} nxt_router_conf_t;


Expand Down Expand Up @@ -235,7 +236,7 @@ typedef struct {
struct nxt_router_access_log_s {
void (*handler)(nxt_task_t *task, nxt_http_request_t *r,
nxt_router_access_log_t *access_log,
nxt_tstr_t *format);
nxt_router_access_log_format_t *format);
nxt_fd_t fd;
nxt_str_t path;
uint32_t count;
Expand Down
Loading

0 comments on commit 996b9c6

Please sign in to comment.