Skip to content

Commit 23704da

Browse files
committed
ngx-json-log: major rewrite to a single src file
- version 0.0.8 - correct request body and response headers handling - remove err_ logging - encoding operations - update documentation - travis migration - build with unbuntu focal
1 parent 56d33e6 commit 23704da

24 files changed

+3054
-4546
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ install:
3434
script:
3535
- cd nginx
3636
- auto/configure --with-debug --with-stream --with-ld-opt="-Wl,-E" --prefix=$NGINX_PREFIX --without-http_gzip_module --add-module=$PWD/.. > build.log 2>&1 || (cat build.log && exit 1)
37+
- cat objs/ngx_modules.c | awk '{ sub("\"ngx_http_json_log_module\",", ""); sub("\"ngx_http_header_filter_module\"", "\"ngx_http_json_log_module\", \"ngx_http_header_filter_module\""); print}' > objs/ngx_modules.c.dat && mv objs/ngx_modules.c.dat objs/ngx_modules.c
3738
- make -j$JOBS > build.log 2>&1 || (cat build.log && exit 1)
3839
- sudo make install > build.log 2>&1 || (cat build.log && exit 1)
3940
- cd ..

CHANGES

Lines changed: 0 additions & 68 deletions
This file was deleted.

README.md

Lines changed: 54 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# ngx-json-log [![Build Status](https://travis-ci.org/fooinha/nginx-json-log.svg?branch=master)](https://travis-ci.org/fooinha/nginx-json-log)
1+
# ngx-json-log [![Build Status](https://travis-ci.com/fooinha/nginx-json-log.svg?branch=master)](https://travis-ci.com/fooinha/nginx-json-log)
22

33
nginx modules for logging in custom json format - aka kasha (🍲)
44

@@ -20,7 +20,7 @@ It supports multiple output destinations with multiple formats for a location.
2020

2121
### Current version and limitations
2222

23-
Current version released is 0.0.7.
23+
Current version released is 0.0.8.
2424

2525
Stream logging is only available when using nginx (>= 1.11.2).
2626

@@ -80,17 +80,6 @@ For HTTP logging, if kafka output is used the value from $request_id nginx varia
8080

8181
The $request_id is only available for nginx (>=1.11.0).
8282

83-
84-
#### Log HTTP Bad Requests
85-
86-
Nginx will short circuit response if the client sends a Bad Request. In that case, the log handler will not be run.
87-
88-
In order to, capture and log these requests we can define at server level, a format and output location for this requests.
89-
90-
The directives json_err_log_* (same suffixes as the directives for location logging), should be used for this case.
91-
92-
Additionally, the variable $http_json_err_log_req will be set with a base64 encodede string for the data sent from the client, until the limit set by **http_json_log_req_body_limit** is reached.
93-
9483
#### Example Configuration
9584

9685

@@ -229,6 +218,50 @@ To ease reading, it's shown here formatted with newlines.
229218

230219
```
231220

221+
#### Encoding operations
222+
223+
The value of key of type `string` can be encoded in 3 ways.
224+
To change the encoding output this should be configured per output key by specifying the encoding after a "|" character.
225+
For instance for `base64` output the key should be like this `key|base64`
226+
227+
The default encoding is *plain* when no encoding is supplied.
228+
229+
* plain
230+
* base64
231+
* hex
232+
* hexdump
233+
234+
Format example:
235+
```yaml
236+
237+
json_log_format body_log '
238+
http.req.body_plain $http_json_log_req_body;
239+
http.req.body_hex|hex $http_json_log_req_body;
240+
http.req.body_hexdump|hexdump $http_json_log_req_body;
241+
http.req.body_base64|base64 $http_json_log_req_body;
242+
';
243+
```
244+
245+
Note: use a different key name if the output with different encoding is wanted for the same $var .
246+
247+
Response:
248+
```json
249+
{
250+
"http": {
251+
"req": {
252+
"body_base64": "Zm9vPWJhcg==",
253+
"body_hex": "666f6f3d626172",
254+
"body_hexdump": [
255+
"66 6f 6f 3d 62 61 72 .. .. .. .. .. .. .. .. .. |foo=bar |"
256+
],
257+
"body_plain": "foo=bar"
258+
}
259+
}
260+
}
261+
262+
```
263+
264+
232265
### Directives
233266
234267
@@ -331,76 +364,6 @@ The format to use when writing to output destination.
331364
332365
---
333366
334-
* Syntax: **http_json_log_req_body_limit** _size_;
335-
* Default: 512
336-
* Context: http local
337-
338-
Limits the body size to log.
339-
Argument is a size string. May be 1k or 1M, but avoid this!
340-
341-
342-
#### Error Directives
343-
344-
* Syntax: **json_err_log_format** _format_name_ { _format_ } _if_=...;
345-
* Default: —
346-
* Context: http main
347-
348-
----
349-
350-
* Syntax: **json_err_log** _location_ _format_name_
351-
* Default: —
352-
* Context: http server
353-
354-
----
355-
356-
* Syntax: **json_err_log_kafka_brokers** list of brokers separated by spaces;
357-
* Default: —
358-
* Context: http main
359-
360-
----
361-
362-
* Syntax: **json_err_log_kafka_client_id** _id_;
363-
* Default: nginx
364-
* Context: http server
365-
366-
----
367-
368-
* Syntax: **json_err_log_kafka_compression** _compression_codec_;
369-
* Default: snappy
370-
* Context: http server
371-
372-
----
373-
374-
* Syntax: **json_err_log_kafka_max_retries** _numeric_;
375-
* Default: 0
376-
* Context: http main
377-
378-
379-
----
380-
381-
* Syntax: **json_err_log_kafka_log_level** _numeric_log_level_;
382-
* Default: 6
383-
* Context: http server
384-
385-
----
386-
387-
* Syntax: **json_err_log_kafka_buffer_max_messages** _numeric_;
388-
* Default: 100000
389-
* Context: http main
390-
391-
----
392-
393-
* Syntax: **json_err_log_kafka_backoff_ms** _numeric_;
394-
* Default: 10
395-
* Context: http main
396-
397-
398-
----
399-
400-
* Syntax: **json_err_log_kafka_partition** _partition_;
401-
* Default: RD_KAFKA_PARTITION_UA
402-
* Context: http main
403-
404367
405368
### Variables
406369
@@ -422,21 +385,17 @@ Example:
422385
423386
#### $http_json_log_req_body;
424387
425-
Log request body encoded as base64.
426-
It requires proxy_pass configuration at logging location.
388+
Log request body.
427389
428390
Example:
429391
430392
```
431393
"req": {
432-
"body": "Zm9v"
394+
"body": "foo=bar"
433395
}
434396
```
435397
436-
#### $http_json_log_req_body_hexdump;
437-
438398
Log request body encoded as hexdump array.
439-
It requires proxy_pass configuration at logging location.
440399
441400
Example:
442401
@@ -449,18 +408,14 @@ Example:
449408
450409
#### $http_json_log_resp_headers;
451410
452-
Creates a json object with available response headers.
411+
Creates a json string with all response headers.
453412
454413
Example:
455414
456415
```
457-
resp": {
458-
"headers": {
459-
"Last-Modified": "Sat, 01 Apr 2017 13:34:28 GMT",
460-
"ETag": "\"58dfac64-12\"",
461-
"X-Foo": "bar",
462-
"Accept-Ranges": "bytes"
463-
}
416+
resp": {
417+
"headers": "HTTP/1.1 204 No Content\r\nServer: nginx/1.21.2\r\nDate: Mon, 09 Aug 2021 15:24:39 GMT\r\nConnection: keep-alive\r\nX-Foo: bar\r\n\r\n",
418+
}
464419
```
465420
466421
### Logging mail proxies authentication
@@ -537,7 +492,6 @@ http {
537492
**Attention**: The Auth-Pass is not masked in any way, nor removed, so the client's value will be logged.
538493
539494
540-
541495
### Build
542496
543497
#### Dependencies
@@ -553,9 +507,11 @@ $ sudo apt-get install libjansson-dev librdkafka-dev
553507
```
554508

555509
Build as a common nginx module.
510+
After configure we need to modify the default order of the modules.
556511

557512
```bash
558513
$ ./configure --add-module=/build/ngx_json_log
514+
$ cat objs/ngx_modules.c | awk '{ sub("\"ngx_http_json_log_module\",", ""); sub("\"ngx_http_header_filter_module\"", "\"ngx_http_json_log_module\", \"ngx_http_header_filter_module\""); print}' > objs/ngx_modules.c.dat && mv objs/ngx_modules.c.dat objs/ngx_modules.c
559515
$ make && make install
560516

561517
```

config

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,12 @@ ngx_module_incs=$ngx_addon_dir/src
33

44
NGINX_VERSION=`grep version src/core/nginx.h | sed 's/#define nginx_version *//;'`
55

6-
if [ ! -z "${NGINX_VERSION}" ]
7-
then
8-
if [ $NGINX_VERSION -gt 1011002 ]
9-
then
10-
STREAM_MODULES="$STREAM_MODULES ngx_stream_json_log_module"
11-
STREAM_MODULES="$STREAM_MODULES ngx_stream_json_log_preread_module"
12-
NGX_ADDON_SRCS="$NGX_ADDON_SRCS \
13-
$ngx_addon_dir/src/ngx_stream_json_log_preread_module.c \
14-
$ngx_addon_dir/src/ngx_stream_json_log_module.c"
15-
echo " + ngx_json_log: stream support"
16-
fi
17-
fi
18-
19-
HTTP_MODULES="$HTTP_MODULES ngx_http_json_log_module"
20-
HTTP_FILTER_MODULES="$HTTP_FILTER_MODULES ngx_http_json_log_filter_module"
21-
226
CORE_INCS="$CORE_INCS $ngx_module_incs"
237

24-
NGX_ADDON_SRCS="$NGX_ADDON_SRCS \
25-
$ngx_addon_dir/src/ngx_json_log_str.c \
26-
$ngx_addon_dir/src/ngx_json_log_text.c \
27-
$ngx_addon_dir/src/ngx_json_log_output.c \
28-
$ngx_addon_dir/src/ngx_http_json_log_variables.c \
29-
$ngx_addon_dir/src/ngx_http_json_log_filter_module.c \
30-
$ngx_addon_dir/src/ngx_http_json_log_module.c \
31-
"
8+
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/src/ngx_http_json_log_module.c"
9+
10+
STREAM_MODULES="$STREAM_MODULES ngx_stream_json_log_module"
11+
HTTP_AUX_FILTER_MODULES="$HTTP_AUX_FILTER_MODULES ngx_http_json_log_module"
3212

3313
#
3414
# rdkafka
@@ -44,10 +24,20 @@ ngx_feature_test="rd_kafka_new(RD_KAFKA_PRODUCER, NULL, NULL, 0);"
4424

4525
if [ $ngx_found = yes ]; then
4626
echo " + ngx_json_log: kafka support"
47-
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/src/ngx_json_log_kafka.c"
4827
CORE_LIBS="$CORE_LIBS $ngx_feature_libs"
4928
fi
5029

51-
52-
5330
CORE_LIBS="$CORE_LIBS -ljansson"
31+
32+
have_stream="0"
33+
case $NGX_CONFIGURE in
34+
*"--with-stream"*)
35+
STREAM_MODULES="$STREAM_MODULES ngx_stream_json_log_module";
36+
echo " + ngx_json_log: stream support"
37+
have_stream="1"
38+
;;
39+
esac
40+
41+
have="NGX_HAVE_STREAM"
42+
value=$have_stream
43+
. auto/define

docker/alpine-nginx-http-log-json/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ RUN apk add --no-cache zlib-dev
4141
RUN apk add --no-cache perl-dev
4242

4343
RUN auto/configure --with-cc=/usr/bin/gcc --add-module=/build/nginx-json-log --with-debug --with-mail --with-stream --with-ld-opt="-Wl,-E"
44+
RUN cat objs/ngx_modules.c | awk '{ sub("\"ngx_http_json_log_module\",", ""); sub("\"ngx_http_header_filter_module\"", "\"ngx_http_json_log_module\", \"ngx_http_header_filter_module\""); print}' > objs/ngx_modules.c.dat && mv objs/ngx_modules.c.dat objs/ngx_modules.c
4445
RUN make install
4546

4647
COPY nginx.conf /usr/local/nginx/conf/nginx.conf

0 commit comments

Comments
 (0)