"Go" How to use the Logrotate tool to automatically slice the log files in the scroll

Source: Internet
Author: User

From:http://www.2cto.com/os/201503/381812.html

In many real-world projects, the application will continue to write logs, and if there are no log libraries in the program code that support automatic sharding, such as by filesize or date cutting, the log files will grow to the G level very quickly. Operating large files on a single machine is very inconvenient for subsequent follow-up logs.

This article describes how to use the Logrotate tool to slice logs outside the application.

1. What is Logrotate?
Logrotate is a log-cutting tool that comes with most Linux systems, and the shell terminal enters "man logrotate" to see its introduction (partially excerpt from the following):
Logrotate is designed to ease administration of systems, that generate large numbers of log files. It allows automatic rotation, compression, removal, and mailing of log files. Each log file is handled daily, weekly, monthly, or when it grows too large.
A more complete introduction can be seen in the shell terminal of its man document, here not to repeat.
In short, we know that it can be used to cut a log file that is still rolling, where "still scrolling" means that the current log file is still being appended to the application for continuous write operations.

2. Logrotate's application Scenario
If the file is static (that is, no application is currently aligned to write), split is a more commonly used static file cutting tool, and its usage profile is described here, which is skipped here.
Logrotate is often used to cut "dynamic" files that are still being written, which supports automatic segmentation of files by time interval or file size (automatic rotation). The specific usage is described below.

3. How to use Logrotate
According to man Logrotate's instructions, logrotate usage is simple:

?
1 logrotate [-dv] [-f|--force] [-s|--state file] config_file+

The option (s) that appears in [] is optional, we only need to provide a copy of the configuration file, the following example to explain the configuration file format.

?
1234567891011121314151617181920212223242526272829303132333435363738394041 # sample logrotate configuration filecompress # 全局配置项,对切分后的文件做gzip压缩      # 一个配置文件中可以包含多个相互独立的sections# 其中,待切分文件路径 + { xxx }构成了一个独立的section,每个section可以配置针对该类文件的切分行为# 注意:全局配置项会作用于每个section,除非在该section配置中覆盖了全局配置项的行为  # 下面section的配置表明按时间间隔(weekly)触发日志的自动切分,历史数据只保存最近的5# 切分完成后,通过发送-HUP信号来重启syslogd这个daemon进程。/var/log/messages {      rotate 5     weekly    postrotate        /sbin/killall -HUP syslogd    endscript}       # 下面的示例section表明按文件大小触发日志自动切分,大小单位除了上面所示的k外,还可以是M或G# 待切分文件路径可以有多个,多个路径用空格隔开"/var/log/httpd/access.log" /var/log/httpd/error.log {    rotate 5    mail www@my.org    size=100k    sharedscripts # 表明HUP信号只需在所有文件均切分完成后发送一次,若无该配置,则每完成一个文件的切分就会执行一次postrotate/endscript之间配置的命令    postrotate        /sbin/killall -HUP httpd    endscript}       # 下面的示例section表明按时间(monthly)触发切分# 切分后的历史文件会被保存到olddir指定的目录下(该目录需要事先mkdir出来,否则会报错)/var/log/news/news.crit {    monthly    rotate 2    olddir /var/log/news/old    missingok # 若section起始处指定的待切分文件不存在,也不会报错(默认会报错)    postrotate        kill -HUP ‘cat /var/run/inn.pid‘    endscript    nocompress # 切分时不做压缩,这里改写了配置文件第1行的全局配置}

In the above configuration file, "#" means that it is followed by a comment, which can have a single line, or it can be on the same line as the configuration item.
The configuration item "Rotate 5" indicates that the Shard history file holds the most recent 5 copies, and if this shard is triggered, there are 5 historical files (the default is messages.1/messages.2/messages.3/messages.4/ messages.5), the file messages.5 is physically deleted, and the remaining file suffix sequence is added 1.
The specific implementation of the segmentation can be referred to the "Bird's Linux private cuisine" here, the description.
The example configuration file above only shows the basic format of the Logrotate configuration file, which also supports a number of other configuration items, which can be referenced in the description of man logrotate.
The following configuration file describes how log auto-segmentation is done for applications that do not allow restart by file size in the actual project.

?
123456789 # filename: logrotate.conf /home/work/running.log {    copytruncate # 先copy running.log内容至历史文件,然后清空running.log的内容    rotate 30 # 保存30份历史日志     size=1G      # 若日志文件达到1G,则触发切分    olddir /home/work/history # 指定历史日志存放目录    notifempty   # 若/home/work/running.log是空文件,则不做切分    missingok    # 若/home/work/running.log不存在,不报错}

Special note copytruncate This configuration item, according to its documentation description (see excerpt below), It may result in partial data loss, should not be used in scenarios where log data loss is not allowed (if the log is not allowed to be lost, It is best to support HUP signals in your application code for graceful reboots, so you can avoid using copytruncate, and of course, you can 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 is told to close its logfile and thus might continue writing (AppE nding) to the previous log file forever. Note that there was 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 would have a no effect, as the old log file stays in place.=

"Go" How to use the Logrotate tool to automatically slice the log files in the scroll

Related Article

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.