Apache Log Setup Method rollup

Source: Internet
Author: User
Tags local time apache log
First, the Apache log is recorded by date

Found in the Pache configuration file
ErrorLog Logs/error_log
Customlog Logs/access_log Common

Linux System configuration methods:

Change it to
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 methods under Windows system:

#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

The first time you do not know set 480 this parameter, resulting in logging time and server time difference of 8 hours, originally is Rotatelogs has an offset parameter, which indicates the time difference in UTC compared to the number of minutes, China is the eighth timezone, a difference of 480 minutes. 86400 means 1 days.

Attached Rotatelogs description

Rotatelogs logfile [rotationtime [offset]] | [Filesizem]

Options
LogFile
It plus the reference name is the log file name. If logfile contains '% ', it will be treated as a format string for strftime (3), otherwise it will be automatically added with the. nnnnnnnnnn suffix in seconds. Both of these formats indicate when the new log is to be used.
Rotationtime
Log file time interval in seconds for the volume to be returned
Offset
The number of minutes of time difference relative to UTC. If omitted, 0 is assumed and UTC time is used. For example, to specify local time for a region with a UTC difference of 5 hours, this argument should be-300.
Filesizem
Specifies the file size of the suffix letter M in megabytes, not the rollback time or jet lag, when the volume is rolled back. Two, set Apache logging format

The format of the custom log file involves two instructions, the Logformat directive and the customlog instruction, and the default httpd.conf file provides several examples of these two directives.

The LOGFORMAT directive defines the format and assigns a name to the format, which we can then refer to directly. The customlog instruction sets the log file and indicates the format in which the log file is used (usually by the name of the format).

The function of the Logformat directive 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 directive creates a log format called "Common", which is specified in double quotes. Each variable in the format string represents a specific piece of information that is written to the log file in the order specified by the format string.

The Apache document has given all the variables that can be used for the format string and its meaning, as follows:
%...A: Remote IP Address
%... A: Local IP address
%... B: Number of bytes sent, no HTTP headers included
The number of sent bytes in the%...B:CLF format and does not contain HTTP headers. For example, when no data is sent, write '-' instead of 0.
%... {foobar}e: Content of environment variable FOOBAR
%...F: File name
%...H: Remote Host
%... The protocol requested by H
%... {Foobar}i:foobar, the header line of the request sent to the server.
%...L: Telnet name (from Identd, if provided)
Method of%...M Request
%... {foobar}n: The contents of the annotation ' Foobar ' from another module
%... {Foobar}o:foobar contents, header line of answer
%...P: Port used when the server responds to requests
%... P: The child process ID that responds to the request.
%...Q query string (if there is a query string, contains "?" The remainder of the section, otherwise it is an empty string. )%...R: The first line of the request
%...S: State. For internal redirection requests, this refers to the status of the * original * request. If you use%...>s, you refer to subsequent requests.
%...T: Time represented in the public log Time format (or Standard English format)
%... {format}t: Time represented in the specified format
%... T: The time spent in response to a request, in seconds
%...U: Remote user (from Auth; if the return status (%s) is 401, it may be forged)
%... U: URL path requested by the user
%...V: ServerName In response to requested server
%... V: Server name according to Usecanonicalname settings

In all of the variables listed above, "..." indicates an optional condition. If you do not specify a condition, the value of the variable is replaced with "-". Analyzing the previous example of the Logformat directive from the default httpd.conf file, you can see that it creates a log format called "Common," which includes: remote host, Telnet name, remote user, request time, first line of request code, request status, and number of bytes sent.

Sometimes we just want to record some specific, defined information in the log, and then we need to use "...". If one or more HTTP status codes are placed between "%" and a variable, the content represented by the variable is recorded only if the status code returned by the request belongs to one of the specified status codes. For example, if we want to record all the invalid links for a Web site, we can use:
Logformat%404{referer}i Brokenlinks

Conversely, if we want to record the request that the status code is not equal to the specified value, simply add a "!" Symbols can be:

Logformat%!200u Somethingwrong Third, special record of a record

Setenvifnocase user-agent Baiduspider Baidu_robot
Logformat "%h%t/"%r/"%>s%b" robot
Customlog "|/usr/local/apache2.2.0/bin/rotatelogs/usr/local/apache2.2.0/logs/baidu_%y%m%d.txt 86400 480″robot env= Baidu_robot
Under Windows
Customlog "|bin/rotatelogs.exe logs/baidu_%y%m%d.txt 86400 480″robot Env=baidu_robot
In this way, in the logs directory, you will produce a daily baidu_. txt log, 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: After you configure Server A with the Linux system configuration method, you can back up Server B write backup scripts, assuming that server A's IP is: 192.168.0.3, the script is as follows: [Copy to clipboard] View Code BASH

# Apache log backup 
#!/bin/bash
 
# Get yesterday's date
Logfi Lename = ' date-d yesterday +% Y% m% d '
# set log backup file path
Backupfilepath =/var/www/logs/
 
# Backup error log file
Echo Get example error log:
CD ${backupfilepath}
# received log files to server A yesterday
SCP root@ 192.168.0.3:/var/log/h ttpd/example_error_log/${logfilename}./example/error/
CD ${backupfilepath} example/error/
# Packing compressed log file
T Ar zcvf ${logfilename}. tar.gz ${logfilename}
Rm-rf ${logfilename}
 
# Backup access log file
Echo ' Get Exampl E 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 ${logfilename}. tar.gz ${logfilename}
RM -RF ${logfilename}
Echo done. "

Before you are ready to run the above script, verify that the key generated by the Ssh-keygen command is set up with a trust relationship between two servers
and then name the script apachelog_backup.sh to the/var/cron/directory. Verify that the backup directory of two servers has been established, test the script, such as the backup directory of server A to create a log file named after the day before, and then go to backup Server B to execute a script to see if the backup was successful.
Finally, use the CRONTAB-E command to automatically perform tasks in the Linux timing task, allowing the script to be executed automatically after 12 o'clock in the morning every day, as follows:

* * * sh/var/cron/apachelog_backup.sh

This sets a 0-point 30-minute execution script per 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.