Manage logs with logrotate from Linux

Source: Internet
Author: User

Everyone may have the need to manage the log, such as the timing of compressed logs, or log more than a certain size automatically split into two files. Have recently received such a small task. Our program uses the C language, using the Log4cpp library to achieve the logging. But the problem is that Log4cpp does not support automatic splitting of logs over a certain amount of hours, and can only cover the previous log from scratch, but this is obviously not what we want. After a few searches, I found that the Linux logrotate command would be able to implement this function.

This is a brief introduction to Logrotate:

The Logrotate utility is designed to simplify the administration of log files on a system which generates a lot of log fil Es. Logrotate allows for the automatic rotation compression, removal and mailing of log files. Logrotate can is set to handle a log file daily, weekly, monthly or when the log file gets to a certain size.

In order to use it, there are two main places to modify: One is/etc/logrotate.conf, the other is the/etc/logrotate.d/file below.

You can define how to handle your log file directly in logrotate.conf, or you can create a new file for your own log under/logrotate.d/to define the behavior of handling log.

Here is a link to the detailed explanation of the logrotate command: http://linuxcommand.org/man_pages/logrotate8.html

Here is a specific example:

/var/log/news/news.crit {           monthly           2           olddir/var/log/news/old           Missingok           Postrotate                                     kill-hup ' cat/var/run/inn.pid '           endscript           nocompress       }

Monthly: Description is a month to deal with, often also daily,weekly

Rotate 2: It means keeping up to two backups, and the extra old logs are overwritten.

Olddir: Defines where the old log is stored

Missingok: Meaning if the above News.crit file can not be found, do not error, skip directly

Postrotate. Endscript: They and the middle command define what to do after the rotate is executed, typically to let the program using the log file know that the log is replaced and need to reopen the file

Nocompress: Indicates that the old log does not need to be compressed to save

Logrotate defines how the log is handled, which itself is called by the Crond timing. Crond is a Unix-based scheduling software in the operating system, the following text is copied from the wiki:

The Software utility Cron is a time-based job scheduler in Unix-like computer operating systems. People who set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodic Ally at fixed times, dates, or intervals. It typically automates system maintenance or administration-though its general-purpose nature makes it useful for things L Ike connecting to the Internet and downloading e-mail at regular intervals.

The default logrotate is to run once a day, and its script is placed under/etc/cron.daily/. In addition to cron.daily and cron.weekly,cron.monthly,cron.hourly, etc., you can put the script under different folders depending on your needs. After you've set everything out, don't forget to use chkconfig crond on to keep it up and running. And then the Gaocheng.

Manage logs with logrotate from Linux

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.