Apache logs include error log error.log and access log access.log, which are indispensable for server analysis and maintenance.
When we use apche to run the website, if the traffic is large, we will find that the log logs in apache are getting larger and larger, and some logs reach a few gigabytes in size, which affects the running status of the server.
These large logs have been occupied by the server and will be re-read and written every time. When reading large files, the server will slow down, and we need to manually stop the server and delete it. So how can we cut the log to let the server access log? Or the error log is automatically divided and recreated according to the specified rules?
The apache log format is generally defined in the httpd.conf configuration file, and the # sign in front of LoadModule log_config_module modules/mod_log_config.so needs to be removed.
We use rotatelogs, the splitting tool that comes with apache, to split the log.
1. First select the appropriate log format
Open the httpd.conf configuration file, generally in the conf- folder of the apache folder.
Find the <If Module log_config_module> tag
Replace CustomLog logs/access.log with
CustomLog "|bin/rotatelogs.exe -l logs/access_%Y%m%d.log 86400" combined
Replace ErrorLog "logs/error.log" with
ErrorLog "|bin/rotatelogs.exe logs/%Y%m%d_error.log 86400 480"
The format of the log is specified in double quotation marks. Each variable in the format represents a specific piece of information. This information is written to the log file in the order of the rules, where %Y represents the current year, %m, current month, and %d, current day, and 86400 is the default The value represents 1 day, and 480 is a 6-hour time difference, which means that the log is divided once a day, so that the log will be re-created every day instead of continuing to use the log records of the previous day. This greatly reduces the size of the log.
Some other parameters:
%% is represented as the symbol "%" itself
%A Full local week name
%B local month name
%H 2-digit hour in 24-hour format
%I 2-digit hour in 12-hour format
%M 2 digit minutes
%S 2-digit second
%U When Sunday is a Monday, the 2-digit week number of the year
%W When Sunday is a Monday, the 2-digit week number of the year
%X local time
%Y 4-digit year
%Z time zone name
%a local 3-character weekday name
%b local 3-character month name
%c local date and time
%d 2-digit day of the month
%j 3-digit day of the year
%m 2-digit month
%p morning and afternoon in the local 12-hour clock
%w When Sunday is a Monday, 1-digit week number
%x local date
%y 2-digit year
If you need to split the log according to the file size, you can also
CustomLog "|bin/rotatelogs.exe logs/access_%Y%m%d.log 5M" common
ErrorLog "|bin/rotatelogs.exe logs/%Y%m%d_error.log 5M"
Among them, 5M means that the log file is 5Mb in size and is automatically divided. The supported units are K, M, G
The same is true: the log record of the website when configuring the website can also use this method to split the log:
will:
<VirtualHost *:80>
DocumentRoot "D:/website root directory"
ServerName website domain name
ErrorLog "logs/domain name of the website-error.log"
CustomLog "logs/domain name of the website-access.log" common
</VirtualHost>
change to:
<VirtualHost *:80>
DocumentRoot "D:/website root directory"
ServerName website domain name
ErrorLog "|bin/rotatelogs.exe -l logs/domain name of website-error-%Y-%m.log 86400"
CustomLog "|bin/rotatelogs.exe -l logs/domain name of website-access-%Y-%m.log 86400" common
</VirtualHost>
In the apache under the windows server environment, because we use external tools to split the log, we will see a lot of Apache rotatelogs command exe processes in the system process. How can we reduce these processes?
Here we need to use apche's third-party log module: mod_log_rotate.so
You can go to the external address specified by apache to download the module
Then put the file in the module directory
Add in httpd.conf
LoadModule log_rotate_module modules/mod_log_rotate.so
RotateLogs On
RotateLogsLocalTime On
RotateInterval 86400
The syntax is similar to the Apache default, the code is as follows:
ErrorLog logs/domain name of the website-error-%Y-%m.log
CustomLog logs/domain name of the website-access-%Y-%m.log common
The main difference from the default tool is that one needs to write |bin/rotatelogs.exe -l, and the module does not need to write and call rotatelogs.exe.
The above are some ways to split logs in apache server.