]> granicus.if.org Git - apache/blob - docs/manual/logs.html.en
Updated XML plus transformations. This was the last one. Puhh.
[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       --><title>Log Files - Apache HTTP Server</title><link href="./style/css/manual-sbar-right-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="right sidebar - blue (font 100%)" /><link href="./style/css/manual-sbar-right-90pc.css" rel="alternate stylesheet" media="all" type="text/css" title="right sidebar - blue (font 90%)" /><link href="./style/css/manual-sbar-right-fix15.css" rel="stylesheet" media="all" type="text/css" title="right sidebar - blue (font fix 15)" /><link href="./style/css/manual-sbar-right-fix13.css" rel="alternate stylesheet" media="all" type="text/css" title="right sidebar - blue (font fix 13)" /><link href="./style/css/manual-sbar-right-100pc-b.css" rel="alternate stylesheet" media="all" type="text/css" title="right sidebar - black (font 100%)" /><link href="./style/css/manual-sbar-right-90pc-b.css" rel="alternate stylesheet" media="all" type="text/css" title="right sidebar - black (font 90%)" /><link href="./style/css/manual-sbar-right-fix15-b.css" rel="alternate stylesheet" media="all" type="text/css" title="right sidebar - black (font fix 15)" /><link href="./style/css/manual-sbar-right-fix13-b.css" rel="alternate stylesheet" media="all" type="text/css" title="right sidebar - black (font fix 13)" /><link href="./style/css/manual-sbar-left-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="left sidebar - blue (font 100%)" /><link href="./style/css/manual-sbar-left-90pc.css" rel="alternate stylesheet" media="all" type="text/css" title="left sidebar - blue (font 90%)" /><link href="./style/css/manual-sbar-left-fix15.css" rel="alternate stylesheet" media="all" type="text/css" title="left sidebar - blue (font fix 15)" /><link href="./style/css/manual-sbar-left-fix13.css" rel="alternate stylesheet" media="all" type="text/css" title="left sidebar - blue (font fix 13)" /><link href="./style/css/manual-sbar-left-100pc-b.css" rel="alternate stylesheet" media="all" type="text/css" title="left sidebar - black (font 100%)" /><link href="./style/css/manual-sbar-left-90pc-b.css" rel="alternate stylesheet" media="all" type="text/css" title="left sidebar - black (font 90%)" /><link href="./style/css/manual-sbar-left-fix15-b.css" rel="alternate stylesheet" media="all" type="text/css" title="left sidebar - black (font fix 15)" /><link href="./style/css/manual-sbar-left-fix13-b.css" rel="alternate stylesheet" media="all" type="text/css" title="left sidebar - black (font fix 13)" /><link href="./style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="loose style - blue (font 100%)" /><link href="./style/css/manual-loose-90pc.css" rel="alternate stylesheet" media="all" type="text/css" title="loose style - blue (font 90%)" /><link href="./style/css/manual-loose-fix15.css" rel="alternate stylesheet" media="all" type="text/css" title="loose style - blue (font fix 15)" /><link href="./style/css/manual-loose-fix13.css" rel="alternate stylesheet" media="all" type="text/css" title="loose style - blue (font fix 13)" /><link href="./style/css/manual-loose-100pc-b.css" rel="alternate stylesheet" media="all" type="text/css" title="loose style - black (font 100%)" /><link href="./style/css/manual-loose-90pc-b.css" rel="alternate stylesheet" media="all" type="text/css" title="loose style - black (font 90%)" /><link href="./style/css/manual-loose-fix15-b.css" rel="alternate stylesheet" media="all" type="text/css" title="loose style - black (font fix 15)" /><link href="./style/css/manual-loose-fix13-b.css" rel="alternate stylesheet" media="all" type="text/css" title="loose style - black (font fix 13)" /><link href="./images/favicon.ico" rel="shortcut icon" /></head><body id="manual-page"><div id="page-header"><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><p class="apache">Apache HTTP Server Version 2.0</p><img alt="" src="./images/feather.gif" /></div><div class="up"><a href="./"><img title="&lt;-" alt="&lt;-" src="./images/left.gif" /></a></div><div id="path"><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-project/">Documentation</a> &gt; <a href="./">Version 2.0</a></div><div id="page-content"><div id="preamble"><h1>Log Files</h1>
8     <p>In order to effectively manage a web server, it is necessary
9     to get feedback about the activity and performance of the
10     server as well as any problems that may be occuring. The Apache
11     HTTP Server provides very comprehensive and flexible logging
12     capabilities. This document describes how to configure its
13     logging capabilities, and how to understand what the logs
14     contain.</p>
15   </div><div id="quickview"><ul id="toc"><li><img alt="" src="./images/down.gif" /> <a href="#security">Security Warning</a></li><li><img alt="" src="./images/down.gif" /> <a href="#errorlog">Error Log</a></li><li><img alt="" src="./images/down.gif" /> <a href="#accesslog">Access Log</a></li><li><img alt="" src="./images/down.gif" /> <a href="#rotation">Rotation Logs</a></li><li><img alt="" src="./images/down.gif" /> <a href="#piped">Piped Logs</a></li><li><img alt="" src="./images/down.gif" /> <a href="#virtualhost">Virtual Hosts</a></li><li><img alt="" src="./images/down.gif" /> <a href="#other">Other Log Files</a></li></ul></div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div><div class="section"><h2><a name="security" id="security">Security Warning</a></h2>
16     
17     
18     <p>Anyone who can write to the directory where Apache is
19     writing a log file can almost certainly gain access to the uid
20     that the server is started as, which is normally root. Do
21     <em>NOT</em> give people write access to the directory the logs
22     are stored in without being aware of the consequences; see the
23     <a href="misc/security_tips.html">security tips</a> document
24     for details.</p>
25
26     <p>In addition, log files may contain information supplied
27     directly by the client, without escaping. Therefore, it is
28     possible for malicious clients to insert control-characters in
29     the log files, so care must be taken in dealing with raw
30     logs.</p>
31   </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div><div class="section"><h2><a name="errorlog" id="errorlog">Error Log</a></h2>
32     
33     
34     <table class="related"><tr><th>Related Modules</th><th>Related Directives</th></tr><tr><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#loglevel">LogLevel</a></code></li></ul></td></tr></table>
35     
36     <p>The server error log, whose name and location is set by the
37     <code class="directive"><a href="./mod/core.html#errorlog">ErrorLog</a></code> directive, is the
38     most important log file. This is the place where Apache httpd
39     will send diagnostic information and record any errors that it
40     encounters in processing requests. It is the first place to
41     look when a problem occurs with starting the server or with the
42     operation of the server, since it will often contain details of
43     what went wrong and how to fix it.</p>
44
45     <p>The error log is usually written to a file (typically
46     <code>error_log</code> on unix systems and
47     <code>error.log</code> on Windows and OS/2). On unix systems it
48     is also possible to have the server send errors to
49     <code>syslog</code> or <a href="#piped">pipe them to a
50     program</a>.</p>
51     
52     <p>The format of the error log is relatively free-form and
53     descriptive. But there is certain information that is contained
54     in most error log entries. For example, here is a typical
55     message.</p>
56     
57     <div class="example"><p><code>
58       [Wed Oct 11 14:32:52 2000] [error] [client 127.0.0.1]
59       client denied by server configuration:
60       /export/home/live/ap/htdocs/test
61     </code></p></div>
62     
63     <p>The first item in the log entry is the date and time of the
64     message. The second entry lists the severity of the error being
65     reported. The <code class="directive"><a href="./mod/core.html#loglevel">LogLevel</a></code>
66     directive is used to control the types of errors that are sent
67     to the error log by restricting the severity level. The third
68     entry gives the IP address of the client that generated the
69     error. Beyond that is the message itself, which in this case
70     indicates that the server has been configured to deny the
71     client access. The server reports the file-system path (as
72     opposed to the web path) of the requested document.</p>
73
74     <p>A very wide variety of different messages can appear in the
75     error log. Most look similar to the example above. The error
76     log will also contain debugging output from CGI scripts. Any
77     information written to <code>stderr</code> by a CGI script will
78     be copied directly to the error log.</p>
79
80     <p>It is not possible to customize the error log by adding or
81     removing information. However, error log entries dealing with
82     particular requests have corresponding entries in the <a href="#accesslog">access log</a>. For example, the above example
83     entry corresponds to an access log entry with status code 403.
84     Since it is possible to customize the access log, you can
85     obtain more information about error conditions using that log
86     file.</p>
87     
88     <p>During testing, it is often useful to continuously monitor
89     the error log for any problems. On unix systems, you can
90     accomplish this using:</p>
91     
92     <div class="example"><p><code>
93       tail -f error_log
94     </code></p></div>
95   </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div><div class="section"><h2><a name="accesslog" id="accesslog">Access Log</a></h2>
96     
97     
98     <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>
99     
100     <p>The server access log records all requests processed by the
101     server. The location and content of the access log are
102     controlled by the <code class="directive"><a href="./mod/mod_log_config.html#customlog">CustomLog</a></code>
103     directive. The <code class="directive"><a href="./mod/mod_log_config.html#logformat">LogFormat</a></code>
104     directive can be used to simplify the selection of 
105     the contents of the logs. This section describes how to configure the server
106     to record information in the access log.</p>
107
108     <p>Of course, storing the information in the access log is only
109     the start of log management. The next step is to analyze this
110     information to produce useful statistics. Log analysis in
111     general is beyond the scope of this document, and not really
112     part of the job of the web server itself. For more information
113     about this topic, and for applications which perform log
114     analysis, check the <a href="http://dmoz.org/Computers/Software/Internet/Site_Management/Log_analysis/">
115     Open Directory</a> or <a href="http://dir.yahoo.com/Computers_and_Internet/Software/Internet/World_Wide_Web/Servers/Log_Analysis_Tools/">
116     Yahoo</a>.</p>
117     
118     <p>Various versions of Apache httpd have used other modules and
119     directives to control access logging, including
120     mod_log_referer, mod_log_agent, and the
121     <code>TransferLog</code> directive. The <code class="directive"><a href="./mod/mod_log_config.html#customlog">CustomLog</a></code> directive now subsumes
122         the functionality of all the older directives.</p>
123
124     <p>The format of the access log is highly configurable. The
125     format is specified using a <code class="directive"><a href="./mod/mod_log_config.html#customlog">CustomLog</a></code> that
126     looks much like a C-style printf(1) format string. Some
127     examples are presented in the next sections. For a complete
128     list of the possible contents of the format string, see the <a href="mod/mod_log_config.html#formats">mod_log_config
129     documentation</a>.</p>
130     
131     <h3><a name="common" id="common">Common Log Format</a></h3>
132       
133     
134       <p>A typical configuration for the access log might look as
135       follows.</p>
136
137       <div class="example"><p><code>
138         LogFormat "%h %l %u %t \"%r\" %&gt;s %b" common<br />
139          CustomLog logs/access_log common
140       </code></p></div>
141     
142       <p>This defines the <em>nickname</em> <code>common</code> and
143       associates it with a particular log format string. The format
144       string consists of percent directives, each of which tell the
145       server to log a particular piece of information. Literal
146       characters may also be placed in the format string and will be
147       copied directly into the log output. The quote character
148       (<code>"</code>) must be escaped by placing a back-slash before
149       it to prevent it from being interpreted as the end of the
150       format string. The format string may also contain the special
151       control characters "<code>\n</code>" for new-line and
152       "<code>\t</code>" for tab.</p>
153
154       <p>The <code class="directive"><a href="./mod/mod_log_config.html#customlog">CustomLog</a></code>
155           directive sets up a new log file using the defined
156           <em>nickname</em>. The filename for the access log is relative to
157           the <code class="directive"><a href="./mod/core.html#serverroot">ServerRoot</a></code> unless it
158           begins with a slash.</p>
159     
160       <p>The above configuration will write log entries in a format
161       known as the Common Log Format (CLF). This standard format can
162       be produced by many different web servers and read by many log
163       analysis programs. The log file entries produced in CLF will
164       look something like this:</p>
165
166       <div class="example"><p><code>
167         127.0.0.1 - frank [10/Oct/2000:13:55:36 -0700] "GET
168         /apache_pb.gif HTTP/1.0" 200 2326
169       </code></p></div>
170     
171       <p>Each part of this log entry is described below.</p>
172     
173       <dl>
174         <dt><code>127.0.0.1</code> (<code>%h</code>)</dt>
175
176         <dd>This is the IP address of the client (remote host) which
177         made the request to the server. If <code class="directive"><a href="./mod/core.html#hostnamelookups">HostnameLookups</a></code> is
178         set to <code>On</code>, then the server will try to determine
179         the hostname and log it in place of the IP address. However,
180         this configuration is not recommended since it can
181         significantly slow the server. Instead, it is best to use a
182         log post-processor such as <a href="programs/logresolve.html">logresolve</a> to determine
183         the hostnames. The IP address reported here is not
184         necessarily the address of the machine at which the user is
185         sitting. If a proxy server exists between the user and the
186         server, this address will be the address of the proxy, rather
187         than the originating machine.</dd>
188
189         <dt><code>-</code> (<code>%l</code>)</dt>
190
191         <dd>The "hyphen" in the output indicates that the requested
192         piece of information is not available. In this case, the
193         information that is not available is the RFC 1413 identity of
194         the client determined by <code>identd</code> on the clients
195         machine. This information is highly unreliable and should
196         almost never be used except on tightly controlled internal
197         networks. Apache httpd will not even attempt to determine
198         this information unless <code class="directive"><a href="./mod/core.html#identitycheck">IdentityCheck</a></code> is set
199         to <code>On</code>.</dd>
200
201         <dt><code>frank</code> (<code>%u</code>)</dt>
202
203         <dd>This is the userid of the person requesting the document
204         as determined by HTTP authentication. The same value is
205         typically provided to CGI scripts in the
206         <code>REMOTE_USER</code> environment variable. If the status
207         code for the request (see below) is 401, then this value
208         should not be trusted because the user is not yet
209         authenticated. If the document is not password protected,
210         this entry will be "<code>-</code>" just like the previous
211         one.</dd>
212
213         <dt><code>[10/Oct/2000:13:55:36 -0700]</code>
214         (<code>%t</code>)</dt>
215
216         <dd>
217           The time that the server finished processing the request.
218           The format is: 
219
220           <p class="indent">
221             <code>[day/month/year:hour:minute:second zone]<br />
222              day = 2*digit<br />
223              month = 3*letter<br />
224              year = 4*digit<br />
225              hour = 2*digit<br />
226              minute = 2*digit<br />
227              second = 2*digit<br />
228              zone = (`+' | `-') 4*digit</code>
229           </p>
230           It is possible to have the time displayed in another format
231           by specifying <code>%{format}t</code> in the log format
232           string, where <code>format</code> is as in
233           <code>strftime(3)</code> from the C standard library.
234         </dd>
235
236         <dt><code>"GET /apache_pb.gif HTTP/1.0"</code>
237         (<code>\"%r\"</code>)</dt>
238
239         <dd>The request line from the client is given in double
240         quotes. The request line contains a great deal of useful
241         information. First, the method used by the client is
242         <code>GET</code>. Second, the client requested the resource
243         <code>/apache_pb.gif</code>, and third, the client used the
244         protocol <code>HTTP/1.0</code>. It is also possible to log
245         one or more parts of the request line independently. For
246         example, the format string "<code>%m %U%q %H</code>" will log
247         the method, path, query-string, and protocol, resulting in
248         exactly the same output as "<code>%r</code>".</dd>
249
250         <dt><code>200</code> (<code>%&gt;s</code>)</dt>
251
252         <dd>This is the status code that the server sends back to the
253         client. This information is very valuable, because it reveals
254         whether the request resulted in a successful response (codes
255         beginning in 2), a redirection (codes beginning in 3), an
256         error caused by the client (codes beginning in 4), or an
257         error in the server (codes beginning in 5). The full list of
258         possible status codes can be found in the <a href="http://www.w3.org/Protocols/rfc2616/rfc2616.txt">HTTP
259         specification</a> (RFC2616 section 10).</dd>
260
261         <dt><code>2326</code> (<code>%b</code>)</dt>
262
263         <dd>The last entry indicates the size of the object returned
264         to the client, not including the response headers. If no
265         content was returned to the client, this value will be
266         "<code>-</code>". To log "<code>0</code>" for no content, use
267         <code>%B</code> instead.</dd>
268       </dl>
269     
270     
271     <h3><a name="combined" id="combined">Combined Log Format</a></h3>
272       
273       
274       <p>Another commonly used format string is called the Combined
275       Log Format. It can be used as follows.</p>
276
277       <div class="example"><p><code>
278         LogFormat "%h %l %u %t \"%r\" %&gt;s %b \"%{Referer}i\"
279         \"%{User-agent}i\"" combined<br />
280          CustomLog log/acces_log combined
281       </code></p></div>
282
283       <p>This format is exactly the same as the Common Log Format,
284       with the addition of two more fields. Each of the additional
285       fields uses the percent-directive
286       <code>%{<em>header</em>}i</code>, where <em>header</em> can be
287       any HTTP request header. The access log under this format will
288       look like:</p>
289       
290       <div class="example"><p><code>
291         127.0.0.1 - frank [10/Oct/2000:13:55:36 -0700] "GET
292         /apache_pb.gif HTTP/1.0" 200 2326
293         "http://www.example.com/start.html" "Mozilla/4.08 [en]
294         (Win98; I ;Nav)"
295       </code></p></div>
296
297       <p>The additional fields are:</p>
298       
299       <dl>
300         <dt><code>"http://www.example.com/start.html"</code>
301         (<code>\"%{Referer}i\"</code>)</dt>
302
303         <dd>The "Referer" (sic) HTTP request header. This gives the
304         site that the client reports having been referred from. (This
305         should be the page that links to or includes
306         <code>/apache_pb.gif</code>).</dd>
307
308         <dt><code>"Mozilla/4.08 [en] (Win98; I ;Nav)"</code>
309         (<code>\"%{User-agent}i\"</code>)</dt>
310
311         <dd>The User-Agent HTTP request header. This is the
312         identifying information that the client browser reports about
313         itself.</dd>
314       </dl>
315     
316     
317     <h3><a name="multiple" id="multiple">Multiple Access Logs</a></h3>
318       
319       
320       <p>Multiple access logs can be created simply by specifying
321       multiple <code class="directive"><a href="./mod/mod_log_config.html#customlog">CustomLog</a></code> 
322       directives in the configuration
323       file. For example, the following directives will create three
324       access logs. The first contains the basic CLF information,
325       while the second and third contain referer and browser
326       information. The last two <code class="directive"><a href="./mod/mod_log_config.html#customlog">CustomLog</a></code> lines show how
327       to mimic the effects of the <code>ReferLog</code> and <code>AgentLog</code> directives.</p>
328
329       <div class="example"><p><code>
330         LogFormat "%h %l %u %t \"%r\" %&gt;s %b" common<br />
331         CustomLog logs/access_log common<br />
332         CustomLog logs/referer_log "%{Referer}i -&gt; %U"<br />
333         CustomLog logs/agent_log "%{User-agent}i"
334       </code></p></div>
335
336       <p>This example also shows that it is not necessary to define a
337       nickname with the <code class="directive"><a href="./mod/mod_log_config.html#logformat">LogFormat</a></code> directive. Instead,
338       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>
339     
340     
341     <h3><a name="conditional" id="conditional">Conditional Logs</a></h3>
342       
343       
344       <p>There are times when it is convenient to exclude certain
345       entries from the access logs based on characteristics of the
346       client request. This is easily accomplished with the help of <a href="env.html">environment variables</a>. First, an
347       environment variable must be set to indicate that the request
348       meets certain conditions. This is usually accomplished with 
349       <code class="directive"><a href="./mod/mod_setenvif.html#setenvif">SetEnvIf</a></code>. Then the
350       <code>env=</code> clause of the <code class="directive"><a href="./mod/mod_log_config.html#customlog">CustomLog</a></code> directive is used to
351           include or exclude requests where the environment variable is
352           set. Some examples:</p>
353
354       <div class="example"><p><code>
355         # Mark requests from the loop-back interface<br />
356         SetEnvIf Remote_Addr "127\.0\.0\.1" dontlog<br />
357         # Mark requests for the robots.txt file<br />
358         SetEnvIf Request_URI "^/robots\.txt$" dontlog<br />
359         # Log what remains<br />
360         CustomLog logs/access_log common env=!dontlog
361       </code></p></div>
362
363       <p>As another example, consider logging requests from
364       english-speakers to one log file, and non-english speakers to a
365       different log file.</p>
366
367       <div class="example"><p><code>
368         SetEnvIf Accept-Language "en" english<br />
369         CustomLog logs/english_log common env=english<br />
370         CustomLog logs/non_english_log common env=!english
371       </code></p></div>
372
373       <p>Although we have just shown that conditional logging is very
374       powerful and flexibly, it is not the only way to control the
375       contents of the logs. Log files are more useful when they
376       contain a complete record of server activity. It is often
377       easier to simply post-process the log files to remove requests
378       that you do not want to consider.</p>
379     
380   </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div><div class="section"><h2><a name="rotation" id="rotation">Rotation Logs</a></h2>
381     
382     
383     <p>On even a moderately busy server, the quantity of
384     information stored in the log files is very large. The access
385     log file typically grows 1 MB or more per 10,000 requests. It
386     will consequently be necessary to periodically rotate the log
387     files by moving or deleting the existing logs. This cannot be
388     done while the server is running, because Apache will continue
389     writing to the old log file as long as it holds the file open.
390     Instead, the server must be <a href="stopping.html">restarted</a> after the log files are
391     moved or deleted so that it will open new log files.</p>
392
393     <p>By using a <em>graceful</em> restart, the server can be
394     instructed to open new log files without losing any existing or
395     pending connections from clients. However, in order to
396     accomplish this, the server must continue to write to the old
397     log files while it finishes serving old requests. It is
398     therefore necessary to wait for some time after the restart
399     before doing any processing on the log files. A typical
400     scenario that simply rotates the logs and compresses the old
401     logs to save space is:</p>
402
403     <div class="example"><p><code>
404       mv access_log access_log.old<br />
405       mv error_log error_log.old<br />
406       apachectl graceful<br />
407       sleep 600<br />
408       gzip access_log.old error_log.old
409     </code></p></div>
410
411     <p>Another way to perform log rotation is using <a href="#piped">piped logs</a> as discussed in the next
412     section.</p>
413   </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div><div class="section"><h2><a name="piped" id="piped">Piped Logs</a></h2>
414     
415     
416     <p>Apache httpd is capable of writing error and access log
417     files through a pipe to another process, rather than directly
418     to a file. This capability dramatically increases the
419     flexibility of logging, without adding code to the main server.
420     In order to write logs to a pipe, simply replace the filename
421     with the pipe character "<code>|</code>", followed by the name
422     of the executable which should accept log entries on its
423     standard input. Apache will start the piped-log process when
424     the server starts, and will restart it if it crashes while the
425     server is running. (This last feature is why we can refer to
426     this technique as "reliable piped logging".)</p>
427
428     <p>Piped log processes are spawned by the parent Apache httpd
429     process, and inherit the userid of that process. This means
430     that piped log programs usually run as root. It is therefore
431     very important to keep the programs simple and secure.</p>
432
433     <p>Some simple examples using piped logs:</p>
434
435     <div class="example"><p><code>
436       # compressed logs<br />
437       CustomLog "|/usr/bin/gzip -c &gt;&gt;
438       /var/log/access_log.gz" common<br />
439       # almost-real-time name resolution<br />
440       CustomLog "|/usr/local/apache/bin/logresolve &gt;&gt;
441       /var/log/access_log" common
442     </code></p></div>
443
444     <p>Notice that quotes are used to enclose the entire command
445     that will be called for the pipe. Although these examples are
446     for the access log, the same technique can be used for the
447     error log.</p>
448
449     <p>One important use of piped logs is to allow log rotation
450     without having to restart the server. The Apache HTTP Server
451     includes a simple program called <a href="programs/rotatelogs.html">rotatelogs</a> for this
452     purpose. For example, to rotate the logs every 24 hours, you
453     can use:</p>
454
455     <div class="example"><p><code>
456       CustomLog "|/usr/local/apache/bin/rotatelogs
457       /var/log/access_log 86400" common
458     </code></p></div>
459     
460     <p>A similar, but much more flexible log rotation program
461     called <a href="http://www.cronolog.org/">cronolog</a>
462     is available at an external site.</p>
463
464     <p>As with conditional logging, piped logs are a very powerful
465     tool, but they should not be used where a simpler solution like
466     off-line post-processing is available.</p>
467   </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div><div class="section"><h2><a name="virtualhost" id="virtualhost">Virtual Hosts</a></h2>
468     
469     
470     <p>When running a server with many <a href="vhosts/">virtual
471     hosts</a>, there are several options for dealing with log
472     files. First, it is possible to use logs exactly as in a
473     single-host server. Simply by placing the logging directives
474     outside the <code class="directive"><a href="./mod/core.html#virtualhost">&lt;VirtualHost&gt;</a></code> sections in the
475     main server context, it is possible to log all requests in the
476     same access log and error log. This technique does not allow
477     for easy collection of statistics on individual virtual
478     hosts.</p>
479
480     <p>If <code class="directive"><a href="./mod/mod_log_config.html#customlog">CustomLog</a></code> 
481     or <code class="directive"><a href="./mod/core.html#errorlog">ErrorLog</a></code>
482     directives are placed inside a
483     <code class="directive"><a href="./mod/core.html#virtualhost">&lt;VirtualHost&gt;</a></code>
484     section, all requests or errors for that virtual host will be
485     logged only to the specified file. Any virtual host which does
486     not have logging directives will still have its requests sent
487     to the main server logs. This technique is very useful for a
488     small number of virtual hosts, but if the number of hosts is
489     very large, it can be complicated to manage. In addition, it
490     can often create problems with <a href="vhosts/fd-limits.html">insufficient file
491     descriptors</a>.</p>
492
493     <p>For the access log, there is a very good compromise. By
494     adding information on the virtual host to the log format
495     string, it is possible to log all hosts to the same log, and
496     later split the log into individual files. For example,
497     consider the following directives.</p>
498
499     <div class="example"><p><code>
500       LogFormat "%v %l %u %t \"%r\" %&gt;s %b"
501       comonvhost<br />
502       CustomLog logs/access_log comonvhost
503     </code></p></div>
504
505     <p>The <code>%v</code> is used to log the name of the virtual
506     host that is serving the request. Then a program like <a href="programs/other.html">split-logfile</a> can be used to
507     post-process the access log in order to split it into one file
508     per virtual host.</p>
509   </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div><div class="section"><h2><a name="other" id="other">Other Log Files</a></h2>
510     
511     
512     <table class="related"><tr><th>Related Modules</th><th>Related Directives</th></tr><tr><td><ul><li><code class="module"><a href="./mod/mod_cgi.html">mod_cgi</a></code></li><li><code class="module"><a href="./mod/mod_rewrite.html">mod_rewrite</a></code></li></ul></td><td><ul><li><code class="directive"><a href="./mod/mpm_common.html#pidfile">PidFile</a></code></li><li><code class="directive"><a href="./mod/mod_rewrite.html#rewritelog">RewriteLog</a></code></li><li><code class="directive"><a href="./mod/mod_rewrite.html#rewriteloglevel">RewriteLogLevel</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>
513     
514     <h3><a name="pidfile" id="pidfile">PID File</a></h3>
515       
516     
517       <p>On startup, Apache httpd saves the process id of the parent
518       httpd process to the file <code>logs/httpd.pid</code>. This
519       filename can be changed with the <code class="directive"><a href="./mod/mpm_common.html#pidfile">PidFile</a></code> directive. The
520       process-id is for use by the administrator in restarting and
521       terminating the daemon by sending signals to the parent
522       process; on Windows, use the -k command line option instead.
523       For more information see the <a href="stopping.html">Stopping
524       and Restarting</a> page.</p>    
525     
526     
527     <h3><a name="scriptlog" id="scriptlog">Script Log</a></h3>
528       
529       
530       <p>In order to aid in debugging, the
531       <code class="directive"><a href="./mod/mod_cgi.html#scriptlog">ScriptLog</a></code> directive
532       allows you to record the input to and output from CGI scripts.
533       This should only be used in testing - not for live servers.
534       More information is available in the <a href="mod/mod_cgi.html">mod_cgi</a> documentation.</p>
535     
536     
537     <h3><a name="rewritelog" id="rewritelog">Rewrite Log</a></h3>
538       
539       
540       <p>When using the powerful and complex features of <a href="mod/mod_rewrite.html">mod_rewrite</a>, it is almost
541       always necessary to use the <code class="directive"><a href="./mod/mod_rewrite.html#rewritelog">RewriteLog</a></code> to help
542       in debugging. This log file produces a detailed analysis of how
543       the rewriting engine transforms requests. The level of detail
544       is controlled by the <code class="directive"><a href="./mod/mod_rewrite.html#rewriteloglevel">RewriteLogLevel</a></code> directive.</p>
545     
546   </div></div><div id="footer"><p class="apache">Maintained by the <a href="http://httpd.apache.org/docs-project/">Apache HTTP Server Documentation Project</a></p><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></body></html>