Apache Log Optimization

Source: Internet
Author: User
Tags apache log

The Apache runtime records information about all requests that it processes. In the meantime, information about the abnormal conditions that occurred during processing is also logged.

The server can record the activity information related to the request in multiple log files in multiple formats, but only one copy of the error message is logged.

With respect to the activity record, the log content is not written until the request has been processed. such as uploading a large file, it is written to the log after the file has been uploaded. The error message is written when the error occurs. Of course, mistakes happen for a moment.

There is a common way to display logs: CLF (common Log format). The format is:

"%h%l%u%t \"%r\ "%>s%b"

But with the development, the content of this format is not enough, people have organized another format:

"%h%l%u%t \"%r\ "%>s%b \"%{referer}i\ "\"%{user-agent}i\ ""

It can remember the requested browser version and other client software names and versions.

Apache comes with a standard log module: Mod_log_config. In addition, Apache 2.0 comes with an additional module: Mod_logio. It is a reinforced version of the former. and in http:// There are other third-party modules available in the Modules.apache.org module registration area.

For example, my Apache httpd.conf file has the following configuration:

Errorlog "Logs/error.log"

LogLevel warn

<ifmodule log_config_module>

Logformat "%h%l%u%t \"%r\ "%>s%b \"%{referer}i\ "\"%{user-agent}i\ "" combined

Logformat "%h%l%u%t \"%r\ "%>s%b" common

<ifmodule logio_module>

Logformat "%h%l%u%t \"%r\ "%>s%b \"%{referer}i\ "\"%{user-agent}i\ "%I%O" Combinedio

</IfModule>

Customlog "Logs/access.log" common

</IfModule>

There are two files in the logs directory under my Apache installation directory, which is the Error.log and Access.log configured above

The above configuration first defines the log output of the Apache error message. and defines the level of log output. There are several levels:

Rank name Introduced
Emerg An emergency; The server is not available
Alert Must take action immediately
Crit Critical situation
Error Error occurred
Warn Warning
Notice Normal, but there is a case to note
Info General Information
Debug Debug level information, including module run status
When a level is set, it is recorded in the log file, along with the logs of its rank above. However, regardless of the value set, notice level information is always recorded.

format string in log:

format string Description
%% Percent semicolon ( Apache2.0.44 or later version )
%a Remote IP Address
%A Native IP Address
%B Number of bytes transferred except HTTP headers
%b The number of bytes transmitted in the CLF format except for the HTTP header, which means "instead of 0" when no byte is transmitted - .
%{Foobar}C The content of the cookie that is passed to the server in the request Foobar . can go to the value in the cookie
%D The time, in micro units, that the server takes to process this request.
%{FOOBAR}e FOOBARthe value of the environment variable
%f Filename
%h Remote host
%H protocol used by the request
%{Foobar}i The content of the request header that is sent to the server Foobar: .
%l Remote login name (from Identd, if supported), unless IdentityCheck is set to " On ", you will get a "-".
%m Method of Request
%{Foobar}n The contents of the annotations from another module Foobar .
%{Foobar}o Foobar:the contents of the answer header.
%p The server serves the standard port for the request.
%P The PID of the child process that served this request.
%{format}P PID or TID (thread ID) that serves the request, with format a value range of: pid and tid ( 2.0.46 and later versions ) and hextid (requires APR1.2.0 and above)
%q Query string (if present, by a " ? " boot, otherwise return an empty string)
%r First line of request (Request URL and method)
%s State. For internal redirection requests, this state refers to the state of the original request,---refers to the state of the %>s last request.
%t Time, in normal log time format (Standard English format)
%{format}t Time, expressed in the strftime(3) specified format. (localized format by default)
%T The time, in seconds, that the request took to finish processing.
%u Remote user name (based on authentication information, if return status ( %s ) is 401, may be false)
%U The URL path of the request, which does not contain a query string.
%v Standard servername for service of this request
%V The server name is set according to the USERCANONICALNAME directive.
%X Connection status when the request is complete:
X= The connection is interrupted before the answer is complete.
+= Continue to connect after the reply is delivered.
-= Closes the connection after the reply is delivered.

(in a later version of 1.3, this directive is %c , but this is the same as the past SSL syntax: %{var}c conflict)

%I The number of bytes received, including the data for the request header, and cannot be zero. To use this command you must enable the Mod_logio module.
%O The number of bytes sent, including the data for the request header, and cannot be zero. To use this command you must enable the Mod_logio module.

Log request logs by day or hour

======================================

Using Customlog and Rotatelogs (already included in the Apache Bin directory: Rotatelogs.exe.linux under the directory may be different):

Customlog "| Bin/rotatelogs logs/access_log.%y-%m-%d 86400 "combined

This configuration will output the log to another program, not to a file. That is: Rotatelogs.exe. This allows us to generate new files without restarting Apache. The script automatically opens the new file at the specified time and writes to the log.

Rotatelogs The first parameter is the file path and name of the log file: logs/access_log.%y-%m-%d 86400 If it contains more than one% character, it will be treated as a format string of strftime (3); As the format above represents the year of the crystal. Otherwise, a 10-digit long update time (in seconds) is appended to the file name. For example, if the file name is foo, a log file such as foo.1234567890 will be generated. The above file will produce: Access_ log.2011-04-25 such a file.

The second parameter is the interval of two updates (in seconds). When the system time is a multiple of this parameter value, it produces an update operation. For example, 86,400 seconds a day, a new log file will be generated every night if you set this situation.

    • Related articles recommended:
    • Install Webalizer to analyze Nginx and Apache logs
    • Apache implementation of single IP multi-port mapping method
    • Configure Awstats to analyze Nginx and Apache logs
    • This article is from: Linux technology Network
    • This article link: http://www.ahlinux.com/apache/9411.html

Apache Log Optimization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.