Summary of Apache Log Settings

Source: Internet
Author: User
Tags apache log
1. Apache logs are recorded by date

In the pache configuration file, find
Errorlog logs/error_log
Customlog logs/access_log common

Linux system configuration method:

Change it
Errorlog "|/usr/local/Apache/bin/rotatelogs
/Home/logs/www/% Y _ % m _ % d_error_log 86400 480 ″
Customlog "|/usr/local/Apache/bin/rotatelogs
/Home/logs/www/% Y _ % m _ % d_access_log 86400 480 "common

Configuration method in Windows:

# Errorlog "| bin/rotatelogs.exe logs/vicp_net_error-% Y % m % d. Log 86400
480 ″
# Customlog "| bin/rotatelogs.exe logs/vicp_net_access-% Y % m % d. Log 86400
480 "common"

If you do not know how to set the 480 parameter for the first time, the log record time is 8 hours different from the server time. It turns out that rotatelogs has an offset parameter, which indicates
The time difference in minutes. China is the eighth time zone, with a difference of 480 minutes. 86400 indicates a day.

Appendix rotatelogs description

Rotatelogs logfile [rotationtime [offset] | [filesizem]

Option
Logfile
The reference name is the log file name. If logfile contains '%', it is considered as a string in strftime (3) format. Otherwise, it is automatically added in seconds.
. Nnnnnnnnnn suffix. Both formats indicate the start time of the new log.
Rotationtime
Interval of Log File rollback in seconds
Offset
The number of minutes relative to the UTC time difference. If it is omitted, it is assumed that it is 0 and UTC time is used. For example, to specify the local time of the area with the UTC time difference of-5 hours, this parameter should be-300.
Filesizem
Specify the size of the file with the suffix m in MB when you roll back, instead of specifying the rollback time or time difference.

Ii. Set Apache logging format

The format of the custom log file involves two Commands: The logformat command and the customlog command. By default, the httpd. conf file provides several
Example.

The logformat command defines the format and specifies a name for the format. Then we can directly reference this name. Set the log file in the customlog command and specify the log file used
Format (usually by the format name ).

The function of the logformat command is to define the log format and specify a name for it. For example, in the default httpd. conf file, we can find the following line of code:

Logformat "% H % L % u % t/" % R/"%> S % B" common
Network Management Alliance www.bitscn.com
This command creates a log format named "common". The log format is specified in the content enclosed by double quotation marks. Each variable in the format string represents a specific information.
The sequence specified by the style string is written to the log file.

The Apache document has provided all the variables that can be used for format strings and their meanings:
%... A: remote IP Address
%... A: local IP Address
%... B: Number of sent bytes, excluding the HTTP Header
%... B: Number of sent bytes in CLF format, excluding the HTTP header. For example, if no data is sent, write '-' instead of 0.
%... {Foobar} e: content of the Environment Variable foobar
%... F: File Name
%... H: Remote Host
%... H request protocol
%... {Foobar} I: content of foobar, the header row of the request sent to the server.
%... L: Remote Login Name (from identd, if provided)
%... M Request Method
%... {Foobar} n: content of the annotation "foobar" from another module
%... {Foobar} O: content of foobar, response header line
%... P: port used by the server to respond to the request
%... P: the ID of the subprocess that responds to the request.
%... Q query string (if a query string exists, it contains "?" Otherwise, it is an empty string .)

%... R: the first line of the request.
%... S: status. For internal redirection requests, this refers to the status of * original * requests. If %…> S, that is, later requests.
%... T: Time in the format of Public log time (or standard English format)
%... {Format} t: Time in the specified format
%... T: The time it takes to respond to the request, in seconds
%... U: remote user (from auth; if the returned status (% s) is 401, it may be forged)
%... U: URL path requested by the user
%... V: servername of the server responding to the request
%... V: The server name obtained based on UseCanonicalName

Among all the variables listed above, "…" Indicates an optional condition. If no condition is specified, the value of the variable is replaced. Analyze the previous information from the default httpd. conf file
The logformat command example shows that it creates a log format named "common", including: remote host, remote login name, remote user, request time, the first request
Line code, Request status, and number of bytes sent.

Sometimes we only want to record some specific and defined information in the log, then we need to use "…". If one or more HTTP status codes are put between "%" and the variable
When the returned status code belongs to the specified status code, the content represented by the variable is recorded. For example, if you want to record all invalid links of a website, you can use:
Logformat % 404 {Referer} I brokenlinks

If we want to record requests whose status code is not equal to the specified value, we only need to add a "!" Symbol:

Logformat %! 200u somethingwrong

3. Specially record a record

Setenvifnocase User-Agent baiduspbaidu_robot
Logformat "% H % t/" % R/"%> S % B" Robot
Customlog "|/usr/local/apache2.2.0/bin/rotatelogs
/Usr/local/apache2.2.0/logs/baidu_policy?m=d.txt 86400 480 "Robot
ENV = baidu_robot
Windows
Customlog "| bin/rotatelogs.exe logs/baidu_policy?m=d.txt 86400 480" Robot
ENV = baidu_robot
In this example, the log of "baidu_ .txt" is generated every day in the logs directory. Each record is similar to the following:
61.135.168.14 [22/OCT/2008: 22: 21: 26 + 0800] "Get/HTTP/1.1" 200 8427

 

Apache Log backup:

  • Configure according to the Linux system configuration method
    After server a, you can back up server B and write the backup script. Assume that the IP address of server a is 192.168.0.3. The script is as follows:

    [Copy to clipboard]


    View code

    Bash
    # Apache Log backup
    #! /Bin/bash
     
    # Retrieve the date of yesterday
    Logfilename = 'date-d Yesterday + % Y % m % d'
    # Set the log backup file path
    Backupfilepath =/var/www/logs/
     
    # Backing up Error Log Files
    Echo "Get example Error Log :"
    CD $ {backupfilepath}
    # Obtain the log file from server a yesterday
    SCP root@192.168.0.3:/var/log/httpd/example_error_log/$ {logfilename}./example/error/
    CD $ {backupfilepath} example/error/
    # Package and compress log files
    Tar zcvf implements multiple logfilename=.tar.gz $ {logfilename}
    Rm-RF $ {logfilename}
     
    # Back up access log files
    Echo "Get example access log :"
    CD $ {backupfilepath}
    SCP root@192.168.0.3:/var/log/httpd/example_access_log // $ {logfilename}./example/access/
    CD $ {backupfilepath} example/access/
    Tar zcvf implements multiple logfilename=.tar.gz $ {logfilename}
    Rm-RF $ {logfilename}
    Echo "done ."

    Before running the above script, you must first confirm that the key generated by the ssh-keygen command between the two servers sets the trust relationship.
    Then name the script apachelog_backup.sh and save it to the/var/cron/directory.

  • Make sure that the backup directories of the two servers have been created. Test the script. For example, create a log file named after the previous day in the Backup Directory of server,
    Then go to backup server B and run the script to check whether the backup is successful.
    Finally, use crontab-e
    Command to add an automatic execution task to the Linux scheduled task, so that the script is automatically executed after every day, as shown below:

    30 00 *** sh/var/cron/apachelog_backup.sh

    The script is executed at 00:30 every day.

 

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.