From d77a1fc732c40fdf9717e5f3150463253d01a209 Mon Sep 17 00:00:00 2001 From: Thomas Meyer Date: Sat, 21 Dec 2024 17:07:29 +0100 Subject: [PATCH] mod_log_config: json formatter: Updated manual and some fixes from review --- changes-entries/mod_log_config.txt | 1 + docs/manual/mod/mod_log_config.xml | 14 +++++++++++++- modules/loggers/mod_log_config.c | 10 +++++----- 3 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 changes-entries/mod_log_config.txt diff --git a/changes-entries/mod_log_config.txt b/changes-entries/mod_log_config.txt new file mode 100644 index 00000000000..f9c817a090c --- /dev/null +++ b/changes-entries/mod_log_config.txt @@ -0,0 +1 @@ + *) mod_log_config: Add support for output log in JSON format [Thomas Meyer] diff --git a/docs/manual/mod/mod_log_config.xml b/docs/manual/mod/mod_log_config.xml index 053aadf86d6..421e88f6852 100644 --- a/docs/manual/mod/mod_log_config.xml +++ b/docs/manual/mod/mod_log_config.xml @@ -454,6 +454,7 @@ Sets filename and format of log file CustomLog file|pipe|provider format|nickname +[formatter=json[,short]] [env=[!]environment-variable| expr=expression] server configvirtual host @@ -525,7 +526,18 @@ CustomLog "logs/access_log" common CustomLog "logs/access_log" "%h %l %u %t \"%r\" %>s %b" -

The third argument is optional and controls whether or +

The third argument is optional and controls whether to use a special + formatter to create the log records. Currently only the formatter "json" is + supported which will create all log records according to RFC8259 and + each printed log record will be a JSON object. + The optional formatter option "short" make the json formatter output short + JSON key names. The JSON key names in short format are directly derived + from the format string, i.e. "%s" will result in a JSON of {"s":"304"}. + If the "short" option is not specified long JSON key names will be used for + better readability in JSON object, e.g. {"statusCode":"304"} +

+ +

The last argument is optional and controls whether or not to log a particular request. The condition can be the presence or absence (in the case of a 'env=!name' clause) of a particular variable in the server diff --git a/modules/loggers/mod_log_config.c b/modules/loggers/mod_log_config.c index f12a3e40213..09d47c21901 100644 --- a/modules/loggers/mod_log_config.c +++ b/modules/loggers/mod_log_config.c @@ -1585,7 +1585,7 @@ static config_log_state *open_config_log(server_rec *s, apr_pool_t *p, if (cls->log_writer_data == NULL) return NULL; - /* sort log_format_items array, to help json formatter in gruppenwechsel */ + /* sort log_format_items array, to help json formatter in control break */ if (cls->log_formatter == ap_json_log_formatter /* sort_log_format_items */ ) { /* no valid cls->format given in format string, use default_format, if any valid */ @@ -1883,7 +1883,7 @@ static ap_log_formatted_data * ap_json_log_formatter( request_rec *r, * * as multiple same tags with different arguments can exist, * the log_format_item is sorted before, so we can easily check for - * gruppenwechsel + * control break (https://en.wikipedia.org/wiki/Control_break) */ if(items[i].arg != NULL && strlen(items[i].arg) > 0) { /* start sub object */ @@ -1894,7 +1894,7 @@ static ap_log_formatted_data * ap_json_log_formatter( request_rec *r, continue; } - /* is gruppenwechsel */ + /* is control break */ if(j > i && strcmp(items[j].tag, items[i].tag) != 0) { break; } @@ -2025,7 +2025,7 @@ static ap_log_formatted_data * ap_json_log_formatter( request_rec *r, * * as multiple same tags with different arguments can exist, * the log_format_item is sorted before, so we can easily check for - * gruppenwechsel + * control break */ if(items[i].arg != NULL && strlen(items[i].arg) > 0) { /* TODO: or allocate once at the begining and use apr_array_clear here? */ @@ -2041,7 +2041,7 @@ static ap_log_formatted_data * ap_json_log_formatter( request_rec *r, continue; } - /* is gruppenwechsel */ + /* is control break */ if(j > i && strcmp(items[j].tag, items[i].tag) != 0) { break; }