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