Path: blob/main/xml/en/docs/stream/ngx_stream_log_module.xml
1 views
<?xml version="1.0"?>12<!--3Copyright (C) Nginx, Inc.4-->56<!DOCTYPE module SYSTEM "../../../../dtd/module.dtd">78<module name="Module ngx_stream_log_module"9link="/en/docs/stream/ngx_stream_log_module.html"10lang="en"11rev="8">1213<section id="summary">1415<para>16The <literal>ngx_stream_log_module</literal> module (1.11.4) writes session logs17in the specified format.18</para>1920</section>212223<section id="example" name="Example Configuration">2425<para>26<example>27log_format basic '$remote_addr [$time_local] '28'$protocol $status $bytes_sent $bytes_received '29'$session_time';3031access_log /spool/logs/nginx-access.log basic buffer=32k;32</example>33</para>3435</section>363738<section id="directives" name="Directives">3940<directive name="access_log">41<syntax>42<value>path</value>43<value>format</value>44[<literal>buffer</literal>=<value>size</value>]45[<literal>gzip[=<value>level</value>]</literal>]46[<literal>flush</literal>=<value>time</value>]47[<literal>if</literal>=<value>condition</value>]</syntax>48<syntax><literal>off</literal></syntax>49<default>off</default>50<context>stream</context>51<context>server</context>5253<para>54Sets the path, <link id="log_format">format</link>,55and configuration for a buffered log write.56Several logs can be specified on the same configuration level.57Logging to <link doc="../syslog.xml">syslog</link>58can be configured by specifying59the “<literal>syslog:</literal>” prefix in the first parameter.60The special value <literal>off</literal> cancels all61<literal>access_log</literal> directives on the current level.62</para>6364<para>65If either the <literal>buffer</literal> or <literal>gzip</literal>66parameter is used, writes to log will be buffered.67<note>68The buffer size must not exceed the size of an atomic write to a disk file.69For FreeBSD this size is unlimited.70</note>71</para>7273<para>74When buffering is enabled, the data will be written to the file:75<list type="bullet">7677<listitem>78if the next log line does not fit into the buffer;79</listitem>8081<listitem>82if the buffered data is older than specified by the <literal>flush</literal>83parameter;84</listitem>8586<listitem>87when a worker process is <link doc="../control.xml">re-opening</link> log88files or is shutting down.89</listitem>9091</list>92</para>9394<para>95If the <literal>gzip</literal> parameter is used, then the buffered data will96be compressed before writing to the file.97The compression level can be set between 1 (fastest, less compression)98and 9 (slowest, best compression).99By default, the buffer size is equal to 64K bytes, and the compression level100is set to 1.101Since the data is compressed in atomic blocks, the log file can be decompressed102or read by “<literal>zcat</literal>” at any time.103</para>104105<para>106Example:107<example>108access_log /path/to/log.gz basic gzip flush=5m;109</example>110</para>111112<para>113<note>114For gzip compression to work, nginx must be built with the zlib library.115</note>116</para>117118<para>119The file path can contain variables,120but such logs have some constraints:121<list type="bullet">122123<listitem>124the <link doc="../ngx_core_module.xml" id="user"/>125whose credentials are used by worker processes should126have permissions to create files in a directory with127such logs;128</listitem>129130<listitem>131buffered writes do not work;132</listitem>133134<listitem>135the file is opened and closed for each log write.136However, since the descriptors of frequently used files can be stored137in a <link id="open_log_file_cache">cache</link>, writing to the old file138can continue during the time specified by the <link id="open_log_file_cache"/>139directive’s <literal>valid</literal> parameter140</listitem>141142</list>143</para>144145<para>146The <literal>if</literal> parameter enables conditional logging.147A session will not be logged if the <value>condition</value> evaluates to “0”148or an empty string.149</para>150151</directive>152153154<directive name="log_format">155<syntax>156<value>name</value>157[<literal>escape</literal>=<literal>default</literal>|<literal>json</literal>|<literal>none</literal>]158<value>string</value> ...</syntax>159<default></default>160<context>stream</context>161162<para>163Specifies the log format, for example:164<example>165log_format proxy '$remote_addr [$time_local] '166'$protocol $status $bytes_sent $bytes_received '167'$session_time "$upstream_addr" '168'"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';169</example>170</para>171172<para id="log_format_escape">173The <literal>escape</literal> parameter (1.11.8) allows setting174<literal>json</literal> or <literal>default</literal> characters escaping175in variables, by default, <literal>default</literal> escaping is used.176The <literal>none</literal> parameter (1.13.10) disables escaping.177</para>178179<para id="log_format_escape_default">180For <literal>default</literal> escaping,181characters “<literal>"</literal>”, “<literal>\</literal>”,182and other characters with values less than 32 or above 126183are escaped as “<literal>\xXX</literal>”.184If the variable value is not found,185a hyphen (“<literal>-</literal>”) will be logged.186</para>187188<para id="log_format_escape_json">189For <literal>json</literal> escaping,190all characters not allowed191in JSON <link url="https://datatracker.ietf.org/doc/html/rfc8259#section-7">strings</link>192will be escaped:193characters “<literal>"</literal>” and194“<literal>\</literal>” are escaped as195“<literal>\"</literal>” and “<literal>\\</literal>”,196characters with values less than 32 are escaped as197“<literal>\n</literal>”,198“<literal>\r</literal>”,199“<literal>\t</literal>”,200“<literal>\b</literal>”,201“<literal>\f</literal>”, or202“<literal>\u00XX</literal>”.203</para>204205</directive>206207208<directive name="open_log_file_cache">209210<syntax>211<literal>max</literal>=<value>N</value>212[<literal>inactive</literal>=<value>time</value>]213[<literal>min_uses</literal>=<value>N</value>]214[<literal>valid</literal>=<value>time</value>]</syntax>215<syntax><literal>off</literal></syntax>216<default>off</default>217<context>stream</context>218<context>server</context>219220<para>221Defines a cache that stores the file descriptors of frequently used logs222whose names contain variables.223The directive has the following parameters:224<list type="tag">225226<tag-name><literal>max</literal></tag-name>227<tag-desc>228sets the maximum number of descriptors in a cache;229if the cache becomes full the least recently used (LRU)230descriptors are closed231</tag-desc>232233<tag-name><literal>inactive</literal></tag-name>234<tag-desc>235sets the time after which the cached descriptor is closed236if there were no access during this time;237by default, 10 seconds238</tag-desc>239240<tag-name><literal>min_uses</literal></tag-name>241<tag-desc>242sets the minimum number of file uses during the time243defined by the <literal>inactive</literal> parameter244to let the descriptor stay open in a cache;245by default, 1246</tag-desc>247248<tag-name><literal>valid</literal></tag-name>249<tag-desc>250sets the time after which it should be checked that the file251still exists with the same name; by default, 60 seconds252</tag-desc>253254<tag-name><literal>off</literal></tag-name>255<tag-desc>256disables caching257</tag-desc>258259</list>260</para>261262<para>263Usage example:264<example>265open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;266</example>267</para>268269</directive>270271</section>272273</module>274275276