@@ -26,22 +26,54 @@ jsonselect <selector> {
26
26
27
27
### Selector
28
28
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 .
30
30
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:
32
34
33
35
``` caddyfile
34
36
{status} {logger}
35
37
```
36
38
37
- Or even more complex ones :
39
+ Or you can even select deeper in the JSON log entry :
38
40
39
41
``` caddyfile
40
- {request>host} {request>method} {request>headers>User-Agent>[0]}
42
+ {request>host} {request>method}
41
43
```
42
44
43
45
The resulting JSON will respect the hierarchy of the selector paths.
44
46
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
+
45
77
## Caddyfile
46
78
47
79
Log a JSON containing only the level, the timestamp, and the message of the log entry.
@@ -99,6 +131,24 @@ This outputs:
99
131
{"severity" :" ERROR" ,"timestamp" :" 2021-07-16T12:55:10Z" ,"logName" :" http.log.access.log0" }
100
132
```
101
133
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
+
102
152
## Try it out
103
153
104
154
From the root directoy of this project, run:
0 commit comments