Skip to content

Commit 3065431

Browse files
committed
docs: explain the syntax extension for reshaping the resulting JSON
Signed-off-by: Leonardo Di Donato <[email protected]>
1 parent 29fcb4a commit 3065431

File tree

1 file changed

+54
-4
lines changed

1 file changed

+54
-4
lines changed

README.md

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,54 @@ jsonselect <selector> {
2626

2727
### Selector
2828

29-
The syntax is heavily inspired by [buger/jsonparser](https://github.com/buger/jsonparser).
29+
A selector represents the JSON path of the log entry you want to select.
3030

31-
Thus, you can write a selector like the following one to only output the HTTP status code and the logger name:
31+
The syntax is heavily inspired by [buger/jsonparser](https://github.com/buger/jsonparser). With some additions...
32+
33+
So, you can write a selector like the following one to only output the HTTP status code and the logger name:
3234

3335
```caddyfile
3436
{status} {logger}
3537
```
3638

37-
Or even more complex ones:
39+
Or you can even select deeper in the JSON log entry:
3840

3941
```caddyfile
40-
{request>host} {request>method} {request>headers>User-Agent>[0]}
42+
{request>host} {request>method}
4143
```
4244

4345
The resulting JSON will respect the hierarchy of the selector paths.
4446

47+
Thus, for a selector like `{request>method}` the resulting JSON log entry will look like this:
48+
49+
```json
50+
{"request":{"method":"GET"}}
51+
```
52+
53+
Notice that the parsing of selectors happens at provisioning time to do not impact encoding performances.
54+
55+
Finally, I extended the syntax to support the reshaping of the resulting JSON keys.
56+
57+
To define a key for a given selector, you can use the following syntax:
58+
59+
```caddyfile
60+
{key:selector}
61+
```
62+
63+
For example, to store the status of a log entry in a a `httpRequest.responseStatus` JSON path you can write:
64+
65+
```caddyfile
66+
{httpResponse>responseStatus:status}
67+
```
68+
69+
Which will output the following JSON:
70+
71+
```json
72+
{"httpRequest":{"responseSize":17064}}
73+
```
74+
75+
This is particularly useful to adapt your log entries to different JSON structures like the Stackdriver one.
76+
4577
## Caddyfile
4678

4779
Log a JSON containing only the level, the timestamp, and the message of the log entry.
@@ -99,6 +131,24 @@ This outputs:
99131
{"severity":"ERROR","timestamp":"2021-07-16T12:55:10Z","logName":"http.log.access.log0"}
100132
```
101133

134+
Even more, you can define keys for the resulting output to better match the [Stackdriver log entry format](https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry).
135+
136+
Like this:
137+
138+
```caddyfile
139+
log {
140+
format jsonselect "{level} {timestamp:ts} {httpRequest>requestMethod:request>method} {httpRequest>protocol:request>proto} {httpRequest>status:status} {httpRequest>responseSize:size}" {
141+
time_format "rfc3339_nano"
142+
}
143+
}
144+
```
145+
146+
Which outputs:
147+
148+
```json
149+
{"level":"info","timestamp":"2021-07-19T14:48:56.262966Z","httpRequest":{"protocol":"HTTP/2.0","requestMethod":"GET","responseSize":17604,"status":200}}
150+
```
151+
102152
## Try it out
103153

104154
From the root directoy of this project, run:

0 commit comments

Comments
 (0)