[Linux Notes] how to use the logrotate tool to automatically split the log files in the scroll, linuxlogrotate

Source: Internet
Author: User

[Linux Notes] how to use the logrotate tool to automatically split the log files in the scroll, linuxlogrotate

In many real projects, applications continuously write logs. If the program code does not call a logstore that supports automatic splitting (such as by filesize or date, then the log file will soon grow to the G level. It is inconvenient to operate large files on a single machine for subsequent log follow-up.

This article describes how to use the logrotate tool to split logs outside the application.

1. What is logrotate?
Logrotate is a log cutting tool provided by most linux systems. You can enter "man logrotate" on the shell terminal to view its introduction (some of them are extracted as follows ):
Logrotate is designed to your administration of systems that generate large numbers of log files. It allows automatic rotation, compression, removal, and mailing of log files.Each log file may be handled daily, weekly, monthly, or when it grows too large.
For a more complete introduction, you can view the man document on the shell terminal.
In short, we know that it can be used to cut log files that are still rolling. Here "Still rolling" means that the current log file is still being appended to write operations by the application.

2. Applicable scenarios of logrotate
If the file is static (that is, there are no applications alignment for write operations), split is a more common static file Cutting Tool. For more information about its usage, see here.
Logrotate is often used to cut "dynamic" files that are still being written. It supportsBy time interval or file sizeTo trigger automatic File splitting ). The specific usage is described below.

3. How to Use logrotate
According to man logrotate, logrotate is easy to use:
logrotate [-dv] [-f|--force] [-s|--state file] config_file+
The option (s) in [] is optional. You only need to provide a configuration file. The following example describes the format of the configuration file.
# Sample logrotate configuration filecompress # global configuration item, perform gzip compression on the split file # a configuration file can contain multiple mutually independent sections # among them, the path of the file to be split + {xxx} constitutes an independent section. You can configure the splitting behavior for each section. # Note: The global configuration item applies to each section, unless the global configuration item is overwritten in the section configuration # The configuration in the section below indicates that automatic log splitting is triggered by weekly, only the last five copies of historical data are saved. # after splitting, the daemon process syslogd is restarted by sending the-HUP signal. /Var/log/messages {rotate 5 weekly postrotate/sbin/killall-HUP syslogd endscript} # The following sample section indicates that automatic log splitting is triggered by file size, in addition to the k shown above, the size unit can also be M or G # There can be multiple file paths to be split, separate multiple paths with spaces "/var/log/httpd/access. log "/var/log/httpd/error. log {rotate 5 mail www@my.org size = 100 k sharedscripts # indicates that the HUP signal only needs to be sent once after all files are split. If no such configuration exists, each time a file is split, the postrotate/sbin/killall-HUP httpd endscript command configured between postrotate/endscript and postrotate/sbin/killall-HUP httpd endscript is executed. # The following example section indicates by time (monthly) trigger splitting # historical files after splitting will be saved to the directory specified by olddir (this directory needs to be mkdir in advance, or an error will be reported)/var/log/news. crit {monthly rotate 2 olddir/var/log/news/old missingok # If the file to be split specified at the beginning of the section does not exist, no error will be reported (an error will be reported by default) postrotate kill-HUP 'cat/var/run/inn. pid 'endscript nocompress # Do not compress when splitting, here the global configuration of the configuration file 1st is rewritten}
In the preceding configuration file, "#" indicates that the comment is followed. It can take an exclusive line or the same line as the configuration item.
The configuration item "rotate 5" indicates that the most recent five historical files are saved after splitting, if five historical files (messages.1/messages.2/messages.3/messages.4/messages.5 by default) are generated when the splitting is triggered, the file messages.5 is physically deleted, and the suffix number of the remaining files is 1.
For details about the splitting process, refer to the description here in "laruence's Linux private house dish.
The preceding sample configuration file only shows the basic format of the logrotate configuration file. This tool also supports many other configuration items. For details, refer to man logrotate.
The following configuration file describes how to automatically split logs of applications that do not allow restart by file size in the actual project.
# Filename: logrotate. conf/home/work/running. log {copytruncate # copy running first. log content to the history file, and then clear the running. log content rotate 30 # Save 30 historical logs size = 1G # If the log file reaches 1G, split olddir/home/work/history # specify the historical log storage directory emempty # If/home/work/running. log is an empty file, so missingok is not split # If/home/work/running. log does not exist. No error is reported}
Special note CopytruncateThis configuration item is described in the document (see the following excerpt ), This configuration may cause some data loss. This configuration should not be used in scenarios where log data is not allowed to be lost.(If logs cannot be lost, it is best to support the HUP signal in the application code for an elegant restart, so that copytruncate can be avoided; of course, you can also use the create configuration supported by logrotate to achieve the goal ).
Copytruncate
Truncate the original log file in place after creating a copy, instead of moving the old log file and optionally creating a new one, It can be used when some program can not be told to close its logfile and thus might continue writing (appending) to the previous log file forever.Note that there is a very small time slice between copying the file and truncating it, so some logging data might be lost. when this option is used, the create option will have no effect, as the old log file stays in place.

[References]
1. man logrotate
2. Large's Linux private dish-logrotate)

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


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.