Apache access.log error.log log file too large optimization method

Source: Internet
Author: User
Tags apache log file local time log log apache access log apache log

Have not found Apache log file generated more than a day, not generally big, if you Apache installed in the C drive, that can be miserable, not a few days hard disk is full, too scary, there is no way to optimize the log, so it is not so big? The answer is yes.

First, stop the Apache service and delete the Error.log and Access.log files in the Apache/logs/directory.

Second, open the Apache conf/httpd.conf configuration file and locate the following configuration information:

Errorlog Logs/error.logcustomlog Logs/access.log Common

Please comment out the "#" number before the above two lines of configuration code, following the changes:

Error log file Error.log limit error log file is 1 m:

Errorlog "|bin/rotatelogs.exe-l logs/error-%y-%m-%d.log 1M"

or generate an error log file every day:

Errorlog "|bin/rotatelogs.exe-l logs/error-%y-%m-%d.log 86400"

Log file Access.log limit access log file to 1 m:

Customlog "|bin/rotatelogs.exe-l logs/access-%y-%m-%d.log 1M" common

or generate an access log file every day:

Customlog "|bin/rotatelogs.exe-l logs/access-%y-%m-%d.log 86400" common


Customlog has a very important role, reflected in the following points

1:seo and website optimization. Through log to see the dead chain, as well as visiting spiders, so as to optimize the site targeted. : 2

2: Identify potential security issues. Because this log records the HTTP request header, there is a request address, which helps us to find some illegal requests constructed by the attackers, especially the database injection-based attack

Note that the contents of the Apache Access log default request may not meet our needs, Logformat records as follows:

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

Modified to a header with an HTTP request.

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

Then comment out the #customlog "Logs/access.log" common in http.conf and remove

#CustomLog "Logs/access.log" combined the previous comment in this line. You can now log the query string in the request and the user agent information.



Apache Log management and large log cleanup methods


There are three ways to round the Web server logs in a better way:
The first approach is to take advantage of the Linux system's own log file round robin mechanism: logrotate;
The second method is to use Apache's own log rotation program Rotatelogs;
The third is to use the FAQ in Apache to recommend the development of a more mature log round tool cronolog.

What I bring to you here is to use Apache's own log-round program rotatelogs, and use shell scripts to periodically delete log files, keep only the files for nearly 3 days, so that the log files are full
Disk space.
To modify the master configuration file httpd.conf
Find the following and modify it to:
Errorlog/var/log/httpd/error_log
Customlog "|/usr/local/apache2/bin/rotatelogs/var/log/httpd/access_log 86400 400M" combined
86400---Log scrolling time is one day
---log file max 400M scroll
Combined---with composite format

The shell script that clears the log file is then created, and the file name is Clean_log
#! /bin/bash
Logdir=/var/log/httpd
CD ${logdir}
Declare-i filesum= ' ls access_log.* | Wc-l '
Declare-i delnum= $filesum-3
if ["${delnum}"-ge 1];then
Rm-rf ' Ls-tr access_log.* | Head-${delnum} '
Fi

chmod 755 Clean_log
This preserves the last 3 days of log files.

Build automated tasks
* * * */usr/local/crontab/clean_log
OK, that's it, it's easy. So you don't have to worry about the growing log files!


Use the Rotatelogs gadget that comes with Apache
The syntax is as follows:

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

Parameter explanation:
-L: Use local time instead of GMT time as the time base. Note: using-l in an environment that alters the GMT offset, such as daylight saving time, can cause unpredictable results.
LogFile: It adds the base name to the log file name. If logfile contains "%", it will be treated as a format string for strftime (), otherwise it will be automatically added in seconds

A bit of the ". nnnnnnnnnn" suffix. Both formats represent the time when the new log began to be used.
Rotationtime: The interval of time in seconds that the log files are scrolled.
Offset: The number of minutes from the time difference from UTC. If omitted, it is assumed to be "0" and UTC time is used. For example, to specify local time for a region with a UTC difference of "-5 hours", this parameter

Should be "-300".
Filesizem: Specifies to scroll with the Filesizem file size instead of by time or slack.

Example:
1. Scroll log files by time:
Error log: errorlog "|/data/apache/bin/rotatelogs log storage directory/%y%m%d_error.log 86400 480"
Access log: Customlog "|/data/apache/bin/rotatelogs log storage directory/%y%m%d_access.log 86400 480" common

which
/data/apache: For the Apache installation directory, according to their actual situation to determine;
86400: Seconds, 24 hours, indicates that the generated log files are scrolled by day, that is, a log file is generated daily;
480: Minute, time offset.
Similarly, you can scroll the log files by the hour, every one hours, a few hours ... Generate a log file.

Extension: Can write a script to delete log files, only a few days of the log, if the site traffic is large, a day will generate dozens of hundred m or even larger log files, both the hard disk and affect server performance.

2. Scroll through the log files by size:
Error log: errorlog "|/data/apache/bin/rotatelogs-l log storage directory/%y%m%d_error.log 5M"
Access log: Customlog "|/data/apache/bin/rotatelogs-l log storage directory/%y%m%d_access.log 5M" common

Scrolls the log file when it reaches 5M.





This article is from the "Rosy Clouds Fly" blog, please be sure to keep this source http://caixia.blog.51cto.com/2266345/1674935

Apache access.log error.log log file too large optimization method

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.