Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
nginx
GitHub Repository: nginx/nginx.org
Path: blob/main/xml/en/docs/stream/ngx_stream_log_module.xml
1 views
1
<?xml version="1.0"?>
2
3
<!--
4
Copyright (C) Nginx, Inc.
5
-->
6
7
<!DOCTYPE module SYSTEM "../../../../dtd/module.dtd">
8
9
<module name="Module ngx_stream_log_module"
10
link="/en/docs/stream/ngx_stream_log_module.html"
11
lang="en"
12
rev="8">
13
14
<section id="summary">
15
16
<para>
17
The <literal>ngx_stream_log_module</literal> module (1.11.4) writes session logs
18
in the specified format.
19
</para>
20
21
</section>
22
23
24
<section id="example" name="Example Configuration">
25
26
<para>
27
<example>
28
log_format basic '$remote_addr [$time_local] '
29
'$protocol $status $bytes_sent $bytes_received '
30
'$session_time';
31
32
access_log /spool/logs/nginx-access.log basic buffer=32k;
33
</example>
34
</para>
35
36
</section>
37
38
39
<section id="directives" name="Directives">
40
41
<directive name="access_log">
42
<syntax>
43
<value>path</value>
44
<value>format</value>
45
[<literal>buffer</literal>=<value>size</value>]
46
[<literal>gzip[=<value>level</value>]</literal>]
47
[<literal>flush</literal>=<value>time</value>]
48
[<literal>if</literal>=<value>condition</value>]</syntax>
49
<syntax><literal>off</literal></syntax>
50
<default>off</default>
51
<context>stream</context>
52
<context>server</context>
53
54
<para>
55
Sets the path, <link id="log_format">format</link>,
56
and configuration for a buffered log write.
57
Several logs can be specified on the same configuration level.
58
Logging to <link doc="../syslog.xml">syslog</link>
59
can be configured by specifying
60
the “<literal>syslog:</literal>” prefix in the first parameter.
61
The special value <literal>off</literal> cancels all
62
<literal>access_log</literal> directives on the current level.
63
</para>
64
65
<para>
66
If either the <literal>buffer</literal> or <literal>gzip</literal>
67
parameter is used, writes to log will be buffered.
68
<note>
69
The buffer size must not exceed the size of an atomic write to a disk file.
70
For FreeBSD this size is unlimited.
71
</note>
72
</para>
73
74
<para>
75
When buffering is enabled, the data will be written to the file:
76
<list type="bullet">
77
78
<listitem>
79
if the next log line does not fit into the buffer;
80
</listitem>
81
82
<listitem>
83
if the buffered data is older than specified by the <literal>flush</literal>
84
parameter;
85
</listitem>
86
87
<listitem>
88
when a worker process is <link doc="../control.xml">re-opening</link> log
89
files or is shutting down.
90
</listitem>
91
92
</list>
93
</para>
94
95
<para>
96
If the <literal>gzip</literal> parameter is used, then the buffered data will
97
be compressed before writing to the file.
98
The compression level can be set between 1 (fastest, less compression)
99
and 9 (slowest, best compression).
100
By default, the buffer size is equal to 64K bytes, and the compression level
101
is set to 1.
102
Since the data is compressed in atomic blocks, the log file can be decompressed
103
or read by “<literal>zcat</literal>” at any time.
104
</para>
105
106
<para>
107
Example:
108
<example>
109
access_log /path/to/log.gz basic gzip flush=5m;
110
</example>
111
</para>
112
113
<para>
114
<note>
115
For gzip compression to work, nginx must be built with the zlib library.
116
</note>
117
</para>
118
119
<para>
120
The file path can contain variables,
121
but such logs have some constraints:
122
<list type="bullet">
123
124
<listitem>
125
the <link doc="../ngx_core_module.xml" id="user"/>
126
whose credentials are used by worker processes should
127
have permissions to create files in a directory with
128
such logs;
129
</listitem>
130
131
<listitem>
132
buffered writes do not work;
133
</listitem>
134
135
<listitem>
136
the file is opened and closed for each log write.
137
However, since the descriptors of frequently used files can be stored
138
in a <link id="open_log_file_cache">cache</link>, writing to the old file
139
can continue during the time specified by the <link id="open_log_file_cache"/>
140
directive’s <literal>valid</literal> parameter
141
</listitem>
142
143
</list>
144
</para>
145
146
<para>
147
The <literal>if</literal> parameter enables conditional logging.
148
A session will not be logged if the <value>condition</value> evaluates to “0”
149
or an empty string.
150
</para>
151
152
</directive>
153
154
155
<directive name="log_format">
156
<syntax>
157
<value>name</value>
158
[<literal>escape</literal>=<literal>default</literal>|<literal>json</literal>|<literal>none</literal>]
159
<value>string</value> ...</syntax>
160
<default></default>
161
<context>stream</context>
162
163
<para>
164
Specifies the log format, for example:
165
<example>
166
log_format proxy '$remote_addr [$time_local] '
167
'$protocol $status $bytes_sent $bytes_received '
168
'$session_time "$upstream_addr" '
169
'"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';
170
</example>
171
</para>
172
173
<para id="log_format_escape">
174
The <literal>escape</literal> parameter (1.11.8) allows setting
175
<literal>json</literal> or <literal>default</literal> characters escaping
176
in variables, by default, <literal>default</literal> escaping is used.
177
The <literal>none</literal> parameter (1.13.10) disables escaping.
178
</para>
179
180
<para id="log_format_escape_default">
181
For <literal>default</literal> escaping,
182
characters “<literal>"</literal>”, “<literal>\</literal>”,
183
and other characters with values less than 32 or above 126
184
are escaped as “<literal>\xXX</literal>”.
185
If the variable value is not found,
186
a hyphen (“<literal>-</literal>”) will be logged.
187
</para>
188
189
<para id="log_format_escape_json">
190
For <literal>json</literal> escaping,
191
all characters not allowed
192
in JSON <link url="https://datatracker.ietf.org/doc/html/rfc8259#section-7">strings</link>
193
will be escaped:
194
characters “<literal>"</literal>” and
195
<literal>\</literal>” are escaped as
196
<literal>\"</literal>” and “<literal>\\</literal>”,
197
characters with values less than 32 are escaped as
198
<literal>\n</literal>”,
199
<literal>\r</literal>”,
200
<literal>\t</literal>”,
201
<literal>\b</literal>”,
202
<literal>\f</literal>”, or
203
<literal>\u00XX</literal>”.
204
</para>
205
206
</directive>
207
208
209
<directive name="open_log_file_cache">
210
211
<syntax>
212
<literal>max</literal>=<value>N</value>
213
[<literal>inactive</literal>=<value>time</value>]
214
[<literal>min_uses</literal>=<value>N</value>]
215
[<literal>valid</literal>=<value>time</value>]</syntax>
216
<syntax><literal>off</literal></syntax>
217
<default>off</default>
218
<context>stream</context>
219
<context>server</context>
220
221
<para>
222
Defines a cache that stores the file descriptors of frequently used logs
223
whose names contain variables.
224
The directive has the following parameters:
225
<list type="tag">
226
227
<tag-name><literal>max</literal></tag-name>
228
<tag-desc>
229
sets the maximum number of descriptors in a cache;
230
if the cache becomes full the least recently used (LRU)
231
descriptors are closed
232
</tag-desc>
233
234
<tag-name><literal>inactive</literal></tag-name>
235
<tag-desc>
236
sets the time after which the cached descriptor is closed
237
if there were no access during this time;
238
by default, 10 seconds
239
</tag-desc>
240
241
<tag-name><literal>min_uses</literal></tag-name>
242
<tag-desc>
243
sets the minimum number of file uses during the time
244
defined by the <literal>inactive</literal> parameter
245
to let the descriptor stay open in a cache;
246
by default, 1
247
</tag-desc>
248
249
<tag-name><literal>valid</literal></tag-name>
250
<tag-desc>
251
sets the time after which it should be checked that the file
252
still exists with the same name; by default, 60 seconds
253
</tag-desc>
254
255
<tag-name><literal>off</literal></tag-name>
256
<tag-desc>
257
disables caching
258
</tag-desc>
259
260
</list>
261
</para>
262
263
<para>
264
Usage example:
265
<example>
266
open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;
267
</example>
268
</para>
269
270
</directive>
271
272
</section>
273
274
</module>
275
276