Last active
September 12, 2019 01:37
-
-
Save 7castle/9ac1218f4f4d868a249a13ca711fb3d0 to your computer and use it in GitHub Desktop.
Logrotate config https://www.jamescoyle.net/cheat-sheets/676-logrotate-cheat-sheet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<pre>/etc/logrotate.conf</pre> | |
<p>And the configuration folder can be found:</p> | |
<pre>/etc/logrotate.d/</pre> | |
<p>The configuration folder is usually where you would add new log file configurations which are unique for a particular log file, or set of log files. For example, if you were to add a new log rotation action for the Ubuntu package manager <strong>apt</strong>, you may use something like below:</p> | |
<pre>vi /etc/logrotate.d/apt</pre> | |
<pre>/var/log/apt/term.log { | |
rotate 12 | |
monthly | |
compress | |
missingok | |
notifempty | |
} | |
/var/log/apt/history.log { | |
rotate 12 | |
monthly | |
compress | |
missingok | |
notifempty | |
}</pre> | |
<p><strong>logrotate</strong> runs each day by default, and is invoked using the daily cron job. The below subject detail the common components of creating a <strong></strong><b>logrotate</b> configuration file.</p> | |
<h3>Create a new empty template</h3> | |
<p>To create a new <strong>logrotate</strong> configuration file, you need to create a new file in <strong>/etc/logrotate.d/</strong>. You will then need to add a reference to the log files you wish to rotate. This can be directly to a single file or use pattern matching to match a group of log files. The below example matches all log files in <strong>/var/log/myapp/</strong> which have a <strong>.log</strong> extension.</p> | |
<pre>/var/log/myapp/*.log { | |
}</pre> | |
<p>You will need to add further commands to this template before it becomes useful. Further commands will be added inside the <strong>{ </strong>and <strong>}</strong> tags.</p> | |
<h3>Rotate based on log file size</h3> | |
<p>Use the <strong>size</strong> keyword to rotate the log file when it exceeded a given file size. The below example rotates a file when it reaches 10 KB.<strong> </strong></p> | |
<pre>/var/log/myapp/*.log { | |
size 10k | |
}</pre> | |
<h3>Rotate based on time (Month, Week or Day)</h3> | |
<p>You can rotate logs using the <strong>monthly</strong>, <strong>weekly</strong> or <strong>daily</strong> keyword to create a new log based on duration. The keywords explain them selves, and they can be used in conjunction with the <strong>size</strong> keyword to rotate on which ever criteria is met first.</p> | |
<pre>/var/log/myapp/*.log { | |
size 10k | |
weekly | |
}</pre> | |
<h3>Limit how many log files are kept after rotation by number</h3> | |
<p>The <strong>rotate</strong> keyword allows you to specify how many old, rotated, log files are kept before logrotate deletes them. The <strong>rotate</strong> keyword requires an integer to specify how many old log files are kept.</p> | |
<pre>/var/log/myapp/*.log { | |
size 10k | |
weekly | |
rotate 8 | |
}</pre> | |
<p>The above example will keep <strong>8</strong> old log files.</p> | |
<h3>Limit how many files are kept after rotation by date</h3> | |
<p>You can specify how long to keep rotated files using the <strong>maxage</strong> keyword. Any rotated log files which are older than <strong>maxage</strong> will be deleted. The below example will keep rotated log files for 56 days.</p> | |
<pre>/var/log/myapp/*.log { | |
size 10k | |
weekly | |
maxage 56 | |
}</pre> | |
<h3>Compress rotated log files</h3> | |
<p>Log files which have been rotated can be compressed to save disk space. Gzip is used by default.</p> | |
<pre>/var/log/myapp/*.log { | |
size 10k | |
weekly | |
rotate 8 | |
compress | |
}</pre> | |
<p>You can change the default gzip compression to another format by specifying the <strong>compresscmd</strong> command and a different executable to use. The below example changes the compression format to bzip2 for better file compression.</p> | |
<pre>/var/log/myapp/*.log { | |
size 10k | |
weekly | |
rotate 8 | |
compress | |
compresscmd /bin/bzip2 | |
}</pre> | |
<h3>Ignore missing log files</h3> | |
<p>If a log file does not exist when <strong>logrotate</strong> is running then an error will be thrown. You can use the keyword <strong>missingok</strong> to avoid this scenario and instruct logrotate to ignore the log file if it does not exist.</p> | |
<pre>/var/log/myapp/*.log { | |
size 10k | |
weekly | |
rotate 8 | |
missingok | |
}</pre> | |
<h3>Continue writing to the same log file after rotation</h3> | |
<p>Usually when a log file is rotated the log file is moved to a new location. Some applications may throw an error, and others may continue to write to the relocated file. The <strong>copytruncate</strong> keyword copies all the log in the file to a new file and then truncates the original file. This keeps the original log file in place and also allows rotation to continue.</p> | |
<pre>/var/log/myapp/*.log { | |
size 10k | |
weekly | |
rotate 8 | |
copytruncate | |
}</pre> | |
<p> </p> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment