]> granicus.if.org Git - apache/blob - docs/manual/logs.html.en
Update transformations.
[apache] / docs / manual / logs.html.en
1 <?xml version="1.0" encoding="ISO-8859-1"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><!--
4         XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
5               This file is generated from xml source: DO NOT EDIT
6         XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
7       -->
8 <title>Log Files - Apache HTTP Server</title>
9 <link href="./style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
10 <link href="./style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
11 <link href="./style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" />
12 <link href="./images/favicon.ico" rel="shortcut icon" /></head>
13 <body id="manual-page"><div id="page-header">
14 <p class="menu"><a href="./mod/">Modules</a> | <a href="./mod/directives.html">Directives</a> | <a href="./faq/">FAQ</a> | <a href="./glossary.html">Glossary</a> | <a href="./sitemap.html">Sitemap</a></p>
15 <p class="apache">Apache HTTP Server Version 2.3</p>
16 <img alt="" src="./images/feather.gif" /></div>
17 <div class="up"><a href="./"><img title="&lt;-" alt="&lt;-" src="./images/left.gif" /></a></div>
18 <div id="path">
19 <a href="http://www.apache.org/">Apache</a> &gt; <a href="http://httpd.apache.org/">HTTP Server</a> &gt; <a href="http://httpd.apache.org/docs/">Documentation</a> &gt; <a href="./">Version 2.3</a></div><div id="page-content"><div id="preamble"><h1>Log Files</h1>
20 <div class="toplang">
21 <p><span>Available Languages: </span><a href="./en/logs.html" title="English">&nbsp;en&nbsp;</a> |
22 <a href="./fr/logs.html" hreflang="fr" rel="alternate" title="Français">&nbsp;fr&nbsp;</a> |
23 <a href="./ja/logs.html" hreflang="ja" rel="alternate" title="Japanese">&nbsp;ja&nbsp;</a> |
24 <a href="./ko/logs.html" hreflang="ko" rel="alternate" title="Korean">&nbsp;ko&nbsp;</a> |
25 <a href="./tr/logs.html" hreflang="tr" rel="alternate" title="Türkçe">&nbsp;tr&nbsp;</a> |
26 <a href="./zh-cn/logs.html" hreflang="zh-cn" rel="alternate" title="Simplified Chinese">&nbsp;zh-cn&nbsp;</a></p>
27 </div>
28
29     <p>In order to effectively manage a web server, it is necessary
30     to get feedback about the activity and performance of the
31     server as well as any problems that may be occurring. The Apache HTTP Server
32     provides very comprehensive and flexible logging
33     capabilities. This document describes how to configure its
34     logging capabilities, and how to understand what the logs
35     contain.</p>
36   </div>
37 <div id="quickview"><ul id="toc"><li><img alt="" src="./images/down.gif" /> <a href="#overview">Overview</a></li>
38 <li><img alt="" src="./images/down.gif" /> <a href="#security">Security Warning</a></li>
39 <li><img alt="" src="./images/down.gif" /> <a href="#errorlog">Error Log</a></li>
40 <li><img alt="" src="./images/down.gif" /> <a href="#permodule">Per-module logging</a></li>
41 <li><img alt="" src="./images/down.gif" /> <a href="#accesslog">Access Log</a></li>
42 <li><img alt="" src="./images/down.gif" /> <a href="#rotation">Log Rotation</a></li>
43 <li><img alt="" src="./images/down.gif" /> <a href="#piped">Piped Logs</a></li>
44 <li><img alt="" src="./images/down.gif" /> <a href="#virtualhost">Virtual Hosts</a></li>
45 <li><img alt="" src="./images/down.gif" /> <a href="#other">Other Log Files</a></li>
46 </ul></div>
47 <div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
48 <div class="section">
49 <h2><a name="overview" id="overview">Overview</a></h2>
50     
51
52   <table class="related"><tr><th>Related Modules</th><th>Related Directives</th></tr><tr><td><ul><li><code class="module"><a href="./mod/mod_log_config.html">mod_log_config</a></code></li><li><code class="module"><a href="./mod/mod_log_forensic.html">mod_log_forensic</a></code></li><li><code class="module"><a href="./mod/mod_logio.html">mod_logio</a></code></li><li><code class="module"><a href="./mod/mod_cgi.html">mod_cgi</a></code></li></ul></td><td /></tr></table>
53
54   <p>
55   The Apache HTTP Server provides a variety of different mechanisms for
56   logging everything that happens on your server, from the initial
57   request, through the URL mapping process, to the final resolution of
58   the connection, including any errors that may have occurred in the
59   process. In addition to this, third-party modules may provide logging
60   capabilities, or inject entries into the existing log files, and
61   applications such as CGI programs, or PHP scripts, or other handlers,
62   may send messages to the server error log.
63   </p>
64
65   <p>
66   In this document we discuss the logging modules that are a standard
67   part of the http server.
68   </p>
69
70   </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
71 <div class="section">
72 <h2><a name="security" id="security">Security Warning</a></h2>
73     
74
75     <p>Anyone who can write to the directory where Apache httpd is
76     writing a log file can almost certainly gain access to the uid
77     that the server is started as, which is normally root. Do
78     <em>NOT</em> give people write access to the directory the logs
79     are stored in without being aware of the consequences; see the
80     <a href="misc/security_tips.html">security tips</a> document
81     for details.</p>
82
83     <p>In addition, log files may contain information supplied
84     directly by the client, without escaping. Therefore, it is
85     possible for malicious clients to insert control-characters in
86     the log files, so care must be taken in dealing with raw
87     logs.</p>
88   </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
89 <div class="section">
90 <h2><a name="errorlog" id="errorlog">Error Log</a></h2>
91     
92
93     <table class="related"><tr><th>Related Modules</th><th>Related Directives</th></tr><tr><td><ul><li><code class="module"><a href="./mod/core.html">core</a></code></li></ul></td><td><ul><li><code class="directive"><a href="./mod/core.html#errorlog">ErrorLog</a></code></li><li><code class="directive"><a href="./mod/core.html#errorlogformat">ErrorLogFormat</a></code></li><li><code class="directive"><a href="./mod/core.html#loglevel">LogLevel</a></code></li></ul></td></tr></table>
94
95     <p>The server error log, whose name and location is set by the
96     <code class="directive"><a href="./mod/core.html#errorlog">ErrorLog</a></code> directive, is the
97     most important log file. This is the place where Apache httpd
98     will send diagnostic information and record any errors that it
99     encounters in processing requests. It is the first place to
100     look when a problem occurs with starting the server or with the
101     operation of the server, since it will often contain details of
102     what went wrong and how to fix it.</p>
103
104     <p>The error log is usually written to a file (typically
105     <code>error_log</code> on Unix systems and
106     <code>error.log</code> on Windows and OS/2). On Unix systems it
107     is also possible to have the server send errors to
108     <code>syslog</code> or <a href="#piped">pipe them to a
109     program</a>.</p>
110
111     <p>The format of the error log is defined by the <code class="directive"><a href="./mod/core.html#errorlogformat">ErrorLogFormat</a></code> directive, with which you
112     can customize what values are logged. A default is format defined
113     if you don't specify one. A typical log message follows:</p>
114
115     <div class="example"><p><code>
116     [Fri Sep 09 10:42:29.902022 2011] [core:error] [pid 35708:tid 4328636416]
117     [client 72.15.99.187] File does not exist: /usr/local/apache2/htdocs/favicon.ico
118     </code></p></div>
119
120     <p>The first item in the log entry is the date and time of the
121     message. The next is the module producing the message (core, in this
122     case) and the severity level of that message. This is followed by
123     the process ID and, if appropriate, the thread ID, of the process
124     that experienced the condition. Next, we have the client address
125     that made the request. And finally is the detailed error message,
126     which in this case indicates a request for a file that did not
127     exist.</p>
128
129     <p>A very wide variety of different messages can appear in the
130     error log. Most look similar to the example above. The error
131     log will also contain debugging output from CGI scripts. Any
132     information written to <code>stderr</code> by a CGI script will
133     be copied directly to the error log.</p>
134
135     <p>Putting a <code>%L</code> token in both the error log and the access
136     log will produce a log entry ID with which you can correlate the entry
137     in the error log with the entry in the access log. If
138     <code class="module"><a href="./mod/mod_unique_id.html">mod_unique_id</a></code> is loaded, its unique request ID will be
139     used as the log entry ID, too.</p>
140
141     <p>During testing, it is often useful to continuously monitor
142     the error log for any problems. On Unix systems, you can
143     accomplish this using:</p>
144
145     <div class="example"><p><code>
146       tail -f error_log
147     </code></p></div>
148   </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
149 <div class="section">
150 <h2><a name="permodule" id="permodule">Per-module logging</a></h2>
151     
152
153     <p>The <code class="directive"><a href="./mod/core.html#loglevel">LogLevel</a></code> directive
154     allows you to specify a log severity level on a per-module basis. In
155     this way, if you are troubleshooting a problem with just one
156     particular module, you can turn up its logging volume without also
157     getting the details of other modules that you're not interested in.
158     This is particularly useful for modules such as
159     <code class="module"><a href="./mod/mod_proxy.html">mod_proxy</a></code> or <code class="module"><a href="./mod/mod_rewrite.html">mod_rewrite</a></code> where you
160     want to know details about what it's trying to do.</p>
161
162     <p>Do this by specifying the name of the module in your
163     <code class="directive">LogLevel</code> directive:</p>
164
165     <div class="example"><p><code>
166     LogLevel info rewrite:trace5
167     </code></p></div>
168
169     <p>This sets the main <code class="directive">LogLevel</code> to info, but
170     turns it up to <code>trace5</code> for
171     <code class="module"><a href="./mod/mod_rewrite.html">mod_rewrite</a></code>.</p>
172
173     <div class="note">This replaces the per-module logging directives, such as
174     <code>RewriteLog</code>, that were present in earlier versions of
175     the server.</div>
176   </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
177 <div class="section">
178 <h2><a name="accesslog" id="accesslog">Access Log</a></h2>
179     
180
181     <table class="related"><tr><th>Related Modules</th><th>Related Directives</th></tr><tr><td><ul><li><code class="module"><a href="./mod/mod_log_config.html">mod_log_config</a></code></li><li><code class="module"><a href="./mod/mod_setenvif.html">mod_setenvif</a></code></li></ul></td><td><ul><li><code class="directive"><a href="./mod/mod_log_config.html#customlog">CustomLog</a></code></li><li><code class="directive"><a href="./mod/mod_log_config.html#logformat">LogFormat</a></code></li><li><code class="directive"><a href="./mod/mod_setenvif.html#setenvif">SetEnvIf</a></code></li></ul></td></tr></table>
182
183     <p>The server access log records all requests processed by the
184     server. The location and content of the access log are
185     controlled by the <code class="directive"><a href="./mod/mod_log_config.html#customlog">CustomLog</a></code>
186     directive. The <code class="directive"><a href="./mod/mod_log_config.html#logformat">LogFormat</a></code>
187     directive can be used to simplify the selection of
188     the contents of the logs. This section describes how to configure the server
189     to record information in the access log.</p>
190
191     <p>Of course, storing the information in the access log is only
192     the start of log management. The next step is to analyze this
193     information to produce useful statistics. Log analysis in
194     general is beyond the scope of this document, and not really
195     part of the job of the web server itself. For more information
196     about this topic, and for applications which perform log
197     analysis, check the <a href="http://dmoz.org/Computers/Software/Internet/Site_Management/Log_analysis/">
198     Open Directory</a> or <a href="http://dir.yahoo.com/Computers_and_Internet/Software/Internet/World_Wide_Web/Servers/Log_Analysis_Tools/">
199     Yahoo</a>.</p>
200
201     <p>Various versions of Apache httpd have used other modules and
202     directives to control access logging, including
203     mod_log_referer, mod_log_agent, and the
204     <code>TransferLog</code> directive. The <code class="directive"><a href="./mod/mod_log_config.html#customlog">CustomLog</a></code> directive now subsumes
205     the functionality of all the older directives.</p>
206
207     <p>The format of the access log is highly configurable. The format
208     is specified using a format string that looks much like a C-style
209     printf(1) format string. Some examples are presented in the next
210     sections. For a complete list of the possible contents of the
211     format string, see the <code class="module"><a href="./mod/mod_log_config.html">mod_log_config</a></code> <a href="mod/mod_log_config.html#formats">format strings</a>.</p>
212
213     <h3><a name="common" id="common">Common Log Format</a></h3>
214       
215
216       <p>A typical configuration for the access log might look as
217       follows.</p>
218
219       <div class="example"><p><code>
220         LogFormat "%h %l %u %t \"%r\" %&gt;s %b" common<br />
221          CustomLog logs/access_log common
222       </code></p></div>
223
224       <p>This defines the <em>nickname</em> <code>common</code> and
225       associates it with a particular log format string. The format
226       string consists of percent directives, each of which tell the
227       server to log a particular piece of information. Literal
228       characters may also be placed in the format string and will be
229       copied directly into the log output. The quote character
230       (<code>"</code>) must be escaped by placing a backslash before
231       it to prevent it from being interpreted as the end of the
232       format string. The format string may also contain the special
233       control characters "<code>\n</code>" for new-line and
234       "<code>\t</code>" for tab.</p>
235
236       <p>The <code class="directive"><a href="./mod/mod_log_config.html#customlog">CustomLog</a></code>
237       directive sets up a new log file using the defined
238       <em>nickname</em>. The filename for the access log is relative to
239       the <code class="directive"><a href="./mod/core.html#serverroot">ServerRoot</a></code> unless it
240       begins with a slash.</p>
241
242       <p>The above configuration will write log entries in a format
243       known as the Common Log Format (CLF). This standard format can
244       be produced by many different web servers and read by many log
245       analysis programs. The log file entries produced in CLF will
246       look something like this:</p>
247
248       <div class="example"><p><code>
249         127.0.0.1 - frank [10/Oct/2000:13:55:36 -0700] "GET
250         /apache_pb.gif HTTP/1.0" 200 2326
251       </code></p></div>
252
253       <p>Each part of this log entry is described below.</p>
254
255       <dl>
256         <dt><code>127.0.0.1</code> (<code>%h</code>)</dt>
257
258         <dd>This is the IP address of the client (remote host) which
259         made the request to the server. If <code class="directive"><a href="./mod/core.html#hostnamelookups">HostnameLookups</a></code> is
260         set to <code>On</code>, then the server will try to determine
261         the hostname and log it in place of the IP address. However,
262         this configuration is not recommended since it can
263         significantly slow the server. Instead, it is best to use a
264         log post-processor such as <code class="program"><a href="./programs/logresolve.html">logresolve</a></code> to determine
265         the hostnames. The IP address reported here is not
266         necessarily the address of the machine at which the user is
267         sitting. If a proxy server exists between the user and the
268         server, this address will be the address of the proxy, rather
269         than the originating machine.</dd>
270
271         <dt><code>-</code> (<code>%l</code>)</dt>
272
273         <dd>The "hyphen" in the output indicates that the requested
274         piece of information is not available. In this case, the
275         information that is not available is the RFC 1413 identity of
276         the client determined by <code>identd</code> on the clients
277         machine. This information is highly unreliable and should
278         almost never be used except on tightly controlled internal
279         networks. Apache httpd will not even attempt to determine
280         this information unless <code class="directive"><a href="./mod/core.html#identitycheck">IdentityCheck</a></code> is set
281         to <code>On</code>.</dd>
282
283         <dt><code>frank</code> (<code>%u</code>)</dt>
284
285         <dd>This is the userid of the person requesting the document
286         as determined by HTTP authentication. The same value is
287         typically provided to CGI scripts in the
288         <code>REMOTE_USER</code> environment variable. If the status
289         code for the request (see below) is 401, then this value
290         should not be trusted because the user is not yet
291         authenticated. If the document is not password protected,
292         this part will be "<code>-</code>" just like the previous
293         one.</dd>
294
295         <dt><code>[10/Oct/2000:13:55:36 -0700]</code>
296         (<code>%t</code>)</dt>
297
298         <dd>
299           The time that the request was received.
300           The format is:
301
302           <p class="indent">
303             <code>[day/month/year:hour:minute:second zone]<br />
304              day = 2*digit<br />
305              month = 3*letter<br />
306              year = 4*digit<br />
307              hour = 2*digit<br />
308              minute = 2*digit<br />
309              second = 2*digit<br />
310              zone = (`+' | `-') 4*digit</code>
311           </p>
312           <p>It is possible to have the time displayed in another format
313           by specifying <code>%{format}t</code> in the log format
314           string, where <code>format</code> is either as in
315           <code>strftime(3)</code> from the C standard library,
316           or one of the supported special tokens. For details see
317           the <code class="module"><a href="./mod/mod_log_config.html">mod_log_config</a></code> <a href="mod/mod_log_config.html#formats">format strings</a>.</p>
318         </dd>
319
320         <dt><code>"GET /apache_pb.gif HTTP/1.0"</code>
321         (<code>\"%r\"</code>)</dt>
322
323         <dd>The request line from the client is given in double
324         quotes. The request line contains a great deal of useful
325         information. First, the method used by the client is
326         <code>GET</code>. Second, the client requested the resource
327         <code>/apache_pb.gif</code>, and third, the client used the
328         protocol <code>HTTP/1.0</code>. It is also possible to log
329         one or more parts of the request line independently. For
330         example, the format string "<code>%m %U%q %H</code>" will log
331         the method, path, query-string, and protocol, resulting in
332         exactly the same output as "<code>%r</code>".</dd>
333
334         <dt><code>200</code> (<code>%&gt;s</code>)</dt>
335
336         <dd>This is the status code that the server sends back to the
337         client. This information is very valuable, because it reveals
338         whether the request resulted in a successful response (codes
339         beginning in 2), a redirection (codes beginning in 3), an
340         error caused by the client (codes beginning in 4), or an
341         error in the server (codes beginning in 5). The full list of
342         possible status codes can be found in the <a href="http://www.w3.org/Protocols/rfc2616/rfc2616.txt">HTTP
343         specification</a> (RFC2616 section 10).</dd>
344
345         <dt><code>2326</code> (<code>%b</code>)</dt>
346
347         <dd>The last part indicates the size of the object returned
348         to the client, not including the response headers. If no
349         content was returned to the client, this value will be
350         "<code>-</code>". To log "<code>0</code>" for no content, use
351         <code>%B</code> instead.</dd>
352       </dl>
353     
354
355     <h3><a name="combined" id="combined">Combined Log Format</a></h3>
356       
357
358       <p>Another commonly used format string is called the Combined
359       Log Format. It can be used as follows.</p>
360
361       <div class="example"><p><code>
362         LogFormat "%h %l %u %t \"%r\" %&gt;s %b \"%{Referer}i\"
363         \"%{User-agent}i\"" combined<br />
364          CustomLog log/access_log combined
365       </code></p></div>
366
367       <p>This format is exactly the same as the Common Log Format,
368       with the addition of two more fields. Each of the additional
369       fields uses the percent-directive
370       <code>%{<em>header</em>}i</code>, where <em>header</em> can be
371       any HTTP request header. The access log under this format will
372       look like:</p>
373
374       <div class="example"><p><code>
375         127.0.0.1 - frank [10/Oct/2000:13:55:36 -0700] "GET
376         /apache_pb.gif HTTP/1.0" 200 2326
377         "http://www.example.com/start.html" "Mozilla/4.08 [en]
378         (Win98; I ;Nav)"
379       </code></p></div>
380
381       <p>The additional fields are:</p>
382
383       <dl>
384         <dt><code>"http://www.example.com/start.html"</code>
385         (<code>\"%{Referer}i\"</code>)</dt>
386
387         <dd>The "Referer" (sic) HTTP request header. This gives the
388         site that the client reports having been referred from. (This
389         should be the page that links to or includes
390         <code>/apache_pb.gif</code>).</dd>
391
392         <dt><code>"Mozilla/4.08 [en] (Win98; I ;Nav)"</code>
393         (<code>\"%{User-agent}i\"</code>)</dt>
394
395         <dd>The User-Agent HTTP request header. This is the
396         identifying information that the client browser reports about
397         itself.</dd>
398       </dl>
399     
400
401     <h3><a name="multiple" id="multiple">Multiple Access Logs</a></h3>
402       
403
404       <p>Multiple access logs can be created simply by specifying
405       multiple <code class="directive"><a href="./mod/mod_log_config.html#customlog">CustomLog</a></code>
406       directives in the configuration
407       file. For example, the following directives will create three
408       access logs. The first contains the basic CLF information,
409       while the second and third contain referer and browser
410       information. The last two <code class="directive"><a href="./mod/mod_log_config.html#customlog">CustomLog</a></code> lines show how
411       to mimic the effects of the <code>ReferLog</code> and <code>AgentLog</code> directives.</p>
412
413       <div class="example"><p><code>
414         LogFormat "%h %l %u %t \"%r\" %&gt;s %b" common<br />
415         CustomLog logs/access_log common<br />
416         CustomLog logs/referer_log "%{Referer}i -&gt; %U"<br />
417         CustomLog logs/agent_log "%{User-agent}i"
418       </code></p></div>
419
420       <p>This example also shows that it is not necessary to define a
421       nickname with the <code class="directive"><a href="./mod/mod_log_config.html#logformat">LogFormat</a></code> directive. Instead,
422       the log format can be specified directly in the <code class="directive"><a href="./mod/mod_log_config.html#customlog">CustomLog</a></code> directive.</p>
423     
424
425     <h3><a name="conditional" id="conditional">Conditional Logs</a></h3>
426       
427
428       <p>There are times when it is convenient to exclude certain
429       entries from the access logs based on characteristics of the
430       client request. This is easily accomplished with the help of <a href="env.html">environment variables</a>. First, an
431       environment variable must be set to indicate that the request
432       meets certain conditions. This is usually accomplished with
433       <code class="directive"><a href="./mod/mod_setenvif.html#setenvif">SetEnvIf</a></code>. Then the
434       <code>env=</code> clause of the <code class="directive"><a href="./mod/mod_log_config.html#customlog">CustomLog</a></code> directive is used to
435       include or exclude requests where the environment variable is
436       set. Some examples:</p>
437
438       <div class="example"><p><code>
439         # Mark requests from the loop-back interface<br />
440         SetEnvIf Remote_Addr "127\.0\.0\.1" dontlog<br />
441         # Mark requests for the robots.txt file<br />
442         SetEnvIf Request_URI "^/robots\.txt$" dontlog<br />
443         # Log what remains<br />
444         CustomLog logs/access_log common env=!dontlog
445       </code></p></div>
446
447       <p>As another example, consider logging requests from
448       english-speakers to one log file, and non-english speakers to a
449       different log file.</p>
450
451       <div class="example"><p><code>
452         SetEnvIf Accept-Language "en" english<br />
453         CustomLog logs/english_log common env=english<br />
454         CustomLog logs/non_english_log common env=!english
455       </code></p></div>
456
457       <p>In a caching scenario one would want to know about
458       the efficiency of the cache. A very simple method to
459       find this out would be:</p>
460
461       <div class="example"><p><code>
462         SetEnv CACHE_MISS 1<br />
463         LogFormat "%h %l %u %t "%r " %&gt;s %b %{CACHE_MISS}e" common-cache<br />
464         CustomLog logs/access_log common-cache
465       </code></p></div>
466
467       <p><code class="module"><a href="./mod/mod_cache.html">mod_cache</a></code> will run before
468       <code class="module"><a href="./mod/mod_env.html">mod_env</a></code> and when successfull will deliver the
469       content without it. In that case a cache hit will log
470       <code>-</code>, while a cache miss will log <code>1</code>.</p>
471
472       <p>In addition to the <code>env=</code> syntax, <code class="directive"><a href="./mod/mod_log_config.html#logformat">LogFormat</a></code> supports logging values
473       conditional upon the HTTP response code:</p>
474
475       <div class="example"><p><code>
476       LogFormat "%400,501{User-agent}i" browserlog<br />
477       LogFormat "%!200,304,302{Referer}i" refererlog
478       </code></p></div>
479
480       <p>In the first example, the <code>User-agent</code> will be
481       logged if the HTTP status code is 400 or 501. In other cases, a
482       literal "-" will be logged instead. Likewise, in the second
483       example, the <code>Referer</code> will be logged if the HTTP
484       status code is <strong>not</strong> 200, 204, or 302. (Note the
485       "!" before the status codes.</p>
486
487       <p>Although we have just shown that conditional logging is very
488       powerful and flexible, it is not the only way to control the
489       contents of the logs. Log files are more useful when they
490       contain a complete record of server activity. It is often
491       easier to simply post-process the log files to remove requests
492       that you do not want to consider.</p>
493     
494   </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
495 <div class="section">
496 <h2><a name="rotation" id="rotation">Log Rotation</a></h2>
497     
498
499     <p>On even a moderately busy server, the quantity of
500     information stored in the log files is very large. The access
501     log file typically grows 1 MB or more per 10,000 requests. It
502     will consequently be necessary to periodically rotate the log
503     files by moving or deleting the existing logs. This cannot be
504     done while the server is running, because Apache httpd will continue
505     writing to the old log file as long as it holds the file open.
506     Instead, the server must be <a href="stopping.html">restarted</a> after the log files are
507     moved or deleted so that it will open new log files.</p>
508
509     <p>By using a <em>graceful</em> restart, the server can be
510     instructed to open new log files without losing any existing or
511     pending connections from clients. However, in order to
512     accomplish this, the server must continue to write to the old
513     log files while it finishes serving old requests. It is
514     therefore necessary to wait for some time after the restart
515     before doing any processing on the log files. A typical
516     scenario that simply rotates the logs and compresses the old
517     logs to save space is:</p>
518
519     <div class="example"><p><code>
520       mv access_log access_log.old<br />
521       mv error_log error_log.old<br />
522       apachectl graceful<br />
523       sleep 600<br />
524       gzip access_log.old error_log.old
525     </code></p></div>
526
527     <p>Another way to perform log rotation is using <a href="#piped">piped logs</a> as discussed in the next
528     section.</p>
529   </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
530 <div class="section">
531 <h2><a name="piped" id="piped">Piped Logs</a></h2>
532     
533
534     <p>Apache httpd is capable of writing error and access log
535     files through a pipe to another process, rather than directly
536     to a file. This capability dramatically increases the
537     flexibility of logging, without adding code to the main server.
538     In order to write logs to a pipe, simply replace the filename
539     with the pipe character "<code>|</code>", followed by the name
540     of the executable which should accept log entries on its
541     standard input. The server will start the piped-log process when
542     the server starts, and will restart it if it crashes while the
543     server is running. (This last feature is why we can refer to
544     this technique as "reliable piped logging".)</p>
545
546     <p>Piped log processes are spawned by the parent Apache httpd
547     process, and inherit the userid of that process. This means
548     that piped log programs usually run as root. It is therefore
549     very important to keep the programs simple and secure.</p>
550
551     <p>One important use of piped logs is to allow log rotation
552     without having to restart the server. The Apache HTTP Server
553     includes a simple program called <code class="program"><a href="./programs/rotatelogs.html">rotatelogs</a></code>
554     for this purpose. For example, to rotate the logs every 24 hours, you
555     can use:</p>
556
557     <div class="example"><p><code>
558       CustomLog "|/usr/local/apache/bin/rotatelogs
559       /var/log/access_log 86400" common
560     </code></p></div>
561
562     <p>Notice that quotes are used to enclose the entire command
563     that will be called for the pipe. Although these examples are
564     for the access log, the same technique can be used for the
565     error log.</p>
566
567     <p>A similar but much more flexible log rotation program
568     called <a href="http://www.cronolog.org/">cronolog</a>
569     is available at an external site.</p>
570
571     <p>As with conditional logging, piped logs are a very powerful
572     tool, but they should not be used where a simpler solution like
573     off-line post-processing is available.</p>
574
575     <p>By default the piped log process is spawned without invoking
576     a shell. Use "<code>|$</code>" instead of "<code>|</code>"
577     to spawn using a shell (usually with <code>/bin/sh -c</code>):</p>
578
579     <div class="example"><p><code>
580       # Invoke "rotatelogs" using a shell<br />
581       CustomLog "|$/usr/local/apache/bin/rotatelogs
582       /var/log/access_log 86400" common
583     </code></p></div>
584
585     <p>This was the default behaviour for Apache 2.2.
586     Depending on the shell specifics this might lead to
587     an additional shell process for the lifetime of the logging
588     pipe program and signal handling problems during restart.
589     For compatibility reasons with Apache 2.2 the notation
590     "<code>||</code>" is also supported and equivalent to using
591     "<code>|</code>".</p>
592   </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
593 <div class="section">
594 <h2><a name="virtualhost" id="virtualhost">Virtual Hosts</a></h2>
595     
596
597     <p>When running a server with many <a href="vhosts/">virtual
598     hosts</a>, there are several options for dealing with log
599     files. First, it is possible to use logs exactly as in a
600     single-host server. Simply by placing the logging directives
601     outside the <code class="directive"><a href="./mod/core.html#virtualhost">&lt;VirtualHost&gt;</a></code> sections in the
602     main server context, it is possible to log all requests in the
603     same access log and error log. This technique does not allow
604     for easy collection of statistics on individual virtual
605     hosts.</p>
606
607     <p>If <code class="directive"><a href="./mod/mod_log_config.html#customlog">CustomLog</a></code>
608     or <code class="directive"><a href="./mod/core.html#errorlog">ErrorLog</a></code>
609     directives are placed inside a
610     <code class="directive"><a href="./mod/core.html#virtualhost">&lt;VirtualHost&gt;</a></code>
611     section, all requests or errors for that virtual host will be
612     logged only to the specified file. Any virtual host which does
613     not have logging directives will still have its requests sent
614     to the main server logs. This technique is very useful for a
615     small number of virtual hosts, but if the number of hosts is
616     very large, it can be complicated to manage. In addition, it
617     can often create problems with <a href="vhosts/fd-limits.html">insufficient file
618     descriptors</a>.</p>
619
620     <p>For the access log, there is a very good compromise. By
621     adding information on the virtual host to the log format
622     string, it is possible to log all hosts to the same log, and
623     later split the log into individual files. For example,
624     consider the following directives.</p>
625
626     <div class="example"><p><code>
627       LogFormat "%v %l %u %t \"%r\" %&gt;s %b"
628       comonvhost<br />
629       CustomLog logs/access_log comonvhost
630     </code></p></div>
631
632     <p>The <code>%v</code> is used to log the name of the virtual
633     host that is serving the request. Then a program like <a href="programs/other.html">split-logfile</a> can be used to
634     post-process the access log in order to split it into one file
635     per virtual host.</p>
636   </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
637 <div class="section">
638 <h2><a name="other" id="other">Other Log Files</a></h2>
639     
640
641     <table class="related"><tr><th>Related Modules</th><th>Related Directives</th></tr><tr><td><ul><li><code class="module"><a href="./mod/mod_logio.html">mod_logio</a></code></li><li><code class="module"><a href="./mod/mod_log_config.html">mod_log_config</a></code></li><li><code class="module"><a href="./mod/mod_log_forensic.html">mod_log_forensic</a></code></li><li><code class="module"><a href="./mod/mod_cgi.html">mod_cgi</a></code></li></ul></td><td><ul><li><code class="directive"><a href="./mod/mod_log_config.html#logformat">LogFormat</a></code></li><li><code class="directive"><a href="./mod/mod_log_config.html#bufferedlogs">BufferedLogs</a></code></li><li><code class="directive"><a href="./mod/mod_log_forensic.html#forensiclog">ForensicLog</a></code></li><li><code class="directive"><a href="./mod/mpm_common.html#pidfile">PidFile</a></code></li><li><code class="directive"><a href="./mod/mod_cgi.html#scriptlog">ScriptLog</a></code></li><li><code class="directive"><a href="./mod/mod_cgi.html#scriptlogbuffer">ScriptLogBuffer</a></code></li><li><code class="directive"><a href="./mod/mod_cgi.html#scriptloglength">ScriptLogLength</a></code></li></ul></td></tr></table>
642
643     <h3>Logging actual bytes sent and received</h3>
644       
645
646       <p><code class="module"><a href="./mod/mod_logio.html">mod_logio</a></code> adds in two additional
647          <code class="directive"><a href="./mod/mod_log_config.html#logformat">LogFormat</a></code> fields
648          (%I and %O) that log the actual number of bytes received and sent
649          on the network.</p>
650     
651
652     <h3>Forensic Logging</h3>
653       
654
655       <p><code class="module"><a href="./mod/mod_log_forensic.html">mod_log_forensic</a></code> provides for forensic logging of
656          client requests. Logging is done before and after processing a
657          request, so the forensic log contains two log lines for each
658          request. The forensic logger is very strict with no customizations.
659          It can be an invaluable debugging and security tool.</p>
660     
661
662     <h3><a name="pidfile" id="pidfile">PID File</a></h3>
663       
664
665       <p>On startup, Apache httpd saves the process id of the parent
666       httpd process to the file <code>logs/httpd.pid</code>. This
667       filename can be changed with the <code class="directive"><a href="./mod/mpm_common.html#pidfile">PidFile</a></code> directive. The
668       process-id is for use by the administrator in restarting and
669       terminating the daemon by sending signals to the parent
670       process; on Windows, use the -k command line option instead.
671       For more information see the <a href="stopping.html">Stopping
672       and Restarting</a> page.</p>
673     
674
675     <h3><a name="scriptlog" id="scriptlog">Script Log</a></h3>
676       
677
678       <p>In order to aid in debugging, the
679       <code class="directive"><a href="./mod/mod_cgi.html#scriptlog">ScriptLog</a></code> directive
680       allows you to record the input to and output from CGI scripts.
681       This should only be used in testing - not for live servers.
682       More information is available in the <a href="mod/mod_cgi.html">mod_cgi</a> documentation.</p>
683     
684
685   </div></div>
686 <div class="bottomlang">
687 <p><span>Available Languages: </span><a href="./en/logs.html" title="English">&nbsp;en&nbsp;</a> |
688 <a href="./fr/logs.html" hreflang="fr" rel="alternate" title="Français">&nbsp;fr&nbsp;</a> |
689 <a href="./ja/logs.html" hreflang="ja" rel="alternate" title="Japanese">&nbsp;ja&nbsp;</a> |
690 <a href="./ko/logs.html" hreflang="ko" rel="alternate" title="Korean">&nbsp;ko&nbsp;</a> |
691 <a href="./tr/logs.html" hreflang="tr" rel="alternate" title="Türkçe">&nbsp;tr&nbsp;</a> |
692 <a href="./zh-cn/logs.html" hreflang="zh-cn" rel="alternate" title="Simplified Chinese">&nbsp;zh-cn&nbsp;</a></p>
693 </div><div id="footer">
694 <p class="apache">Copyright 2011 The Apache Software Foundation.<br />Licensed under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>.</p>
695 <p class="menu"><a href="./mod/">Modules</a> | <a href="./mod/directives.html">Directives</a> | <a href="./faq/">FAQ</a> | <a href="./glossary.html">Glossary</a> | <a href="./sitemap.html">Sitemap</a></p></div>
696 </body></html>