Linux日志系统-03:logrotate主配置文件详解

浅浅的花香味﹌ 2023-01-19 08:30 48阅读 0赞

logrotate主配置文件详解

一、logrotate主配置文件/etc/logrotate.conf

  1. # see "man logrotate" for details
  2. # rotate log files weekly
  3. #所有的日志文件,每周滚动一次
  4. weekly
  5. # keep 4 weeks worth of backlogs
  6. #日志发生滚动后,指定备份日志文件保存多少个副本(权限不变)
  7. rotate 4
  8. # create new (empty) log files after rotating old ones
  9. #是否创建一个空的新的日志文件
  10. create
  11. # use date as a suffix of the rotated file
  12. #指定滚动文件的后缀是当前日期
  13. dateext
  14. # uncomment this if you want your log files compressed
  15. #是否对滚动后的日志进行压缩
  16. #compress
  17. # RPM packages drop log rotation information into this directory
  18. #加载子配置文件
  19. include /etc/logrotate.d
  20. #####上面是全局配置,下面是局部配置#####
  21. # no packages own wtmp and btmp -- we'll rotate them here
  22. #指定对特定的日志文件的滚动规则
  23. /var/log/wtmp {
  24. monthly #一月滚动一次
  25. create 0664 root utmp #指定滚动后创建的新文件的权限为0644,数组为root,属组为utmp
  26. minsize 1M #指定文件的值小于1M不滚动
  27. rotate 1 #指定保留几个备份副本
  28. }
  29. /var/log/btmp {
  30. missingok #如果日志文件不存在发送错误消息
  31. monthly
  32. create 0600 root utmp
  33. rotate 1
  34. }
  35. # system-specific logs may be also be configured here.

二、主配置文件参数详解

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaWJvMTIzMDEyMw_size_16_color_FFFFFF_t_70

PS:配置文件中的更多参数可用man logrotate查看

  1. compress
  2. Old versions of log files are compressed with gzip(1) by default. See also nocompress.
  3. compresscmd
  4. Specifies which command to use to compress log files. The default is gzip(1). See also compress.
  5. uncompresscmd
  6. Specifies which command to use to uncompress log files. The default is gunzip(1).
  7. compressext
  8. Specifies which extension to use on compressed logfiles, if compression is enabled. The default
  9. follows that of the configured compression command.
  10. compressoptions
  11. Command line options may be passed to the compression program, if one is in use. The default, for
  12. gzip(1), is "-6" (biased towards high compression at the expense of speed). If you use a different
  13. compression command, you may need to change the compressoptions to match.
  14. copy Make a copy of the log file, but don't change the original at all. This option can be used, for
  15. instance, to make a snapshot of the current log file, or when some other utility needs to truncate
  16. or parse the file. When this option is used, the create option will have no effect, as the old log
  17. file stays in place.
  18. copytruncate
  19. Truncate the original log file to zero size in place after creating a copy, instead of moving the
  20. old log file and optionally creating a new one. It can be used when some program cannot be told to
  21. close its logfile and thus might continue writing (appending) to the previous log file forever.
  22. Note that there is a very small time slice between copying the file and truncating it, so some log‐
  23. ging data might be lost. When this option is used, the create option will have no effect, as the
  24. old log file stays in place.
  25. create mode owner group, create owner group
  26. Immediately after rotation (before the postrotate script is run) the log file is created (with the
  27. same name as the log file just rotated). mode specifies the mode for the log file in octal (the
  28. same as chmod(2)), owner specifies the user name who will own the log file, and group specifies the
  29. group the log file will belong to. Any of the log file attributes may be omitted, in which case
  30. those attributes for the new file will use the same values as the original log file for the omitted
  31. attributes. This option can be disabled using the nocreate option.
  32. createolddir mode owner group
  33. If the directory specified by olddir directive does not exist, it is created. mode specifies the
  34. mode for the olddir directory in octal (the same as chmod(2)), owner specifies the user name who
  35. will own the olddir directory, and group specifies the group the olddir directory will belong to.
  36. This option can be disabled using the nocreateolddir option.
  37. daily Log files are rotated every day.
  38. dateext
  39. Archive old versions of log files adding a date extension like YYYYMMDD instead of simply adding a
  40. number. The extension may be configured using the dateformat and dateyesterday options.
  41. dateformat format_string
  42. Specify the extension for dateext using the notation similar to strftime(3) function. Only %Y %m %d
  43. %H and %s specifiers are allowed. The default value is -%Y%m%d except hourly, which uses -%Y%m%d%H
  44. as default value. Note that also the character separating log name from the extension is part of
  45. the dateformat string. The system clock must be set past Sep 9th 2001 for %s to work correctly.
  46. Note that the datestamps generated by this format must be lexically sortable (i.e., first the year,
  47. then the month then the day. e.g., 2001/12/01 is ok, but 01/12/2001 is not, since 01/11/2002 would
  48. sort lower while it is later). This is because when using the rotate option, logrotate sorts all
  49. rotated filenames to find out which logfiles are older and should be removed.
  50. dateyesterday
  51. Use yesterday's instead of today's date to create the dateext extension, so that the rotated log
  52. file has a date in its name that is the same as the timestamps within it.
  53. delaycompress
  54. Postpone compression of the previous log file to the next rotation cycle. This only has effect when
  55. used in combination with compress. It can be used when some program cannot be told to close its
  56. logfile and thus might continue writing to the previous log file for some time.
  57. extension ext
  58. Log files with ext extension can keep it after the rotation. If compression is used, the com‐
  59. pression extension (normally .gz) appears after ext. For example you have a logfile named mylog.foo
  60. and want to rotate it to mylog.1.foo.gz instead of mylog.foo.1.gz.
  61. hourly Log files are rotated every hour. Note that usually logrotate is configured to be run by cron daily.
  62. You have to change this configuration and run logrotate hourly to be able to really rotate logs
  63. hourly.
  64. ifempty
  65. Rotate the log file even if it is empty, overriding the notifempty option (ifempty is the default).
  66. include file_or_directory
  67. Reads the file given as an argument as if it was included inline where the include directive
  68. appears. If a directory is given, most of the files in that directory are read in alphabetic order
  69. before processing of the including file continues. The only files which are ignored are files which
  70. are not regular files (such as directories and named pipes) and files whose names end with one of
  71. the taboo extensions, as specified by the tabooext directive.
  72. mail address
  73. When a log is rotated out of existence, it is mailed to address. If no mail should be generated by a
  74. particular log, the nomail directive may be used.
  75. mailfirst
  76. When using the mail command, mail the just-rotated file, instead of the about-to-expire file.
  77. maillast
  78. When using the mail command, mail the about-to-expire file, instead of the just-rotated file (this
  79. is the default).
  80. maxage count
  81. Remove rotated logs older than <count> days. The age is only checked if the logfile is to be
  82. rotated. The files are mailed to the configured address if maillast and mail are configured.
  83. maxsize size
  84. Log files are rotated when they grow bigger than size bytes even before the additionally specified
  85. time interval (daily, weekly, monthly, or yearly). The related size option is similar except that
  86. it is mutually exclusive with the time interval options, and it causes log files to be rotated with‐
  87. out regard for the last rotation time. When maxsize is used, both the size and timestamp of a log
  88. file are considered.
  89. minsize size
  90. Log files are rotated when they grow bigger than size bytes, but not before the additionally speci‐
  91. fied time interval (daily, weekly, monthly, or yearly). The related size option is similar except
  92. that it is mutually exclusive with the time interval options, and it causes log files to be rotated
  93. without regard for the last rotation time. When minsize is used, both the size and timestamp of a
  94. log file are considered.
  95. missingok
  96. If the log file is missing, go on to the next one without issuing an error message. See also
  97. nomissingok.
  98. monthly
  99. Log files are rotated the first time logrotate is run in a month (this is normally on the first day
  100. of the month).
  101. nocompress
  102. Old versions of log files are not compressed. See also compress.
  103. nocopy Do not copy the original log file and leave it in place. (this overrides the copy option).
  104. nocopytruncate
  105. Do not truncate the original log file in place after creating a copy (this overrides the copytrun‐
  106. cate option).
  107. nocreate
  108. New log files are not created (this overrides the create option).
  109. nocreateolddir
  110. olddir directory is not created by logrotate when it does not exist.
  111. nodelaycompress
  112. Do not postpone compression of the previous log file to the next rotation cycle (this overrides the
  113. delaycompress option).
  114. nodateext
  115. Do not archive old versions of log files with date extension (this overrides the dateext option).
  116. nomail Do not mail old log files to any address.
  117. nomissingok
  118. If a log file does not exist, issue an error. This is the default.
  119. noolddir
  120. Logs are rotated in the directory they normally reside in (this overrides the olddir option).
  121. nosharedscripts
  122. Run prerotate and postrotate scripts for every log file which is rotated (this is the default, and
  123. overrides the sharedscripts option). The absolute path to the log file is passed as first argument
  124. to the script. If the scripts exit with error, the remaining actions will not be executed for the
  125. affected log only.
  126. noshred
  127. Do not use shred when deleting old log files. See also shred.
  128. notifempty
  129. Do not rotate the log if it is empty (this overrides the ifempty option).
  130. olddir directory
  131. Logs are moved into directory for rotation. The directory must be on the same physical device as the
  132. log file being rotated, unless copy, copytruncate or renamecopy option is used. The directory is
  133. assumed to be relative to the directory holding the log file unless an absolute path name is speci‐
  134. fied. When this option is used all old versions of the log end up in directory. This option may be
  135. overridden by the noolddir option.
  136. postrotate/endscript
  137. The lines between postrotate and endscript (both of which must appear on lines by themselves) are
  138. executed (using /bin/sh) after the log file is rotated. These directives may only appear inside a
  139. log file definition. Normally, the absolute path to the log file is passed as first argument to the
  140. script. If sharedscripts is specified, whole pattern is passed to the script. See also prerotate.
  141. See sharedscripts and nosharedscripts for error handling.
  142. prerotate/endscript
  143. The lines between prerotate and endscript (both of which must appear on lines by themselves) are
  144. executed (using /bin/sh) before the log file is rotated and only if the log will actually be
  145. rotated. These directives may only appear inside a log file definition. Normally, the absolute path
  146. to the log file is passed as first argument to the script. If sharedscripts is specified, whole
  147. pattern is passed to the script. See also postrotate. See sharedscripts and nosharedscripts for
  148. error handling.
  149. firstaction/endscript
  150. The lines between firstaction and endscript (both of which must appear on lines by themselves) are
  151. executed (using /bin/sh) once before all log files that match the wildcarded pattern are rotated,
  152. before prerotate script is run and only if at least one log will actually be rotated. These direc‐
  153. tives may only appear inside a log file definition. Whole pattern is passed to the script as first
  154. argument. If the script exits with error, no further processing is done. See also lastaction.
  155. lastaction/endscript
  156. The lines between lastaction and endscript (both of which must appear on lines by themselves) are
  157. executed (using /bin/sh) once after all log files that match the wildcarded pattern are rotated,
  158. after postrotate script is run and only if at least one log is rotated. These directives may only
  159. appear inside a log file definition. Whole pattern is passed to the script as first argument. If the
  160. script exits with error, just an error message is shown (as this is the last action). See also
  161. firstaction.
  162. preremove/endscript
  163. The lines between preremove and endscript (both of which must appear on lines by themselves) are
  164. executed (using /bin/sh) once just before removal of a log file. The logrotate will pass the name
  165. of file which is soon to be removed. See also firstaction.
  166. rotate count
  167. Log files are rotated count times before being removed or mailed to the address specified in a mail
  168. directive. If count is 0, old versions are removed rather than rotated.
  169. size size
  170. Log files are rotated only if they grow bigger then size bytes. If size is followed by k, the size
  171. is assumed to be in kilobytes. If the M is used, the size is in megabytes, and if G is used, the
  172. size is in gigabytes. So size 100, size 100k, size 100M and size 100G are all valid.
  173. sharedscripts
  174. Normally, prerotate and postrotate scripts are run for each log which is rotated and the absolute
  175. path to the log file is passed as first argument to the script. That means a single script may be
  176. run multiple times for log file entries which match multiple files (such as the /var/log/news/*
  177. example). If sharedscripts is specified, the scripts are only run once, no matter how many logs
  178. match the wildcarded pattern, and whole pattern is passed to them. However, if none of the logs in
  179. the pattern require rotating, the scripts will not be run at all. If the scripts exit with error,
  180. the remaining actions will not be executed for any logs. This option overrides the nosharedscripts
  181. option and implies create option.
  182. shred Delete log files using shred -u instead of unlink(). This should ensure that logs are not readable
  183. after their scheduled deletion; this is off by default. See also noshred.
  184. shredcycles count
  185. Asks GNU shred(1) to overwrite log files count times before deletion. Without this option, shred's
  186. default will be used.
  187. start count
  188. This is the number to use as the base for rotation. For example, if you specify 0, the logs will be
  189. created with a .0 extension as they are rotated from the original log files. If you specify 9, log
  190. files will be created with a .9, skipping 0-8. Files will still be rotated the number of times
  191. specified with the rotate directive.
  192. su user group
  193. Rotate log files set under this user and group instead of using default user/group (usually root).
  194. user specifies the user name used for rotation and group specifies the group used for rotation. If
  195. the user/group you specify here does not have sufficient privilege to make files with the ownership
  196. you've specified in a create instruction, it will cause an error.
  197. tabooext [+] list
  198. The current taboo extension list is changed (see the include directive for information on the taboo
  199. extensions). If a + precedes the list of extensions, the current taboo extension list is augmented,
  200. otherwise it is replaced. At startup, the taboo extension list contains .rpmsave, .rpmorig, ~, .dis‐
  201. abled, .dpkg-old, .dpkg-dist, .dpkg-new, .cfsaved, .ucf-old, .ucf-dist, .ucf-new, .rpmnew, .swp,
  202. .cfsaved, .rhn-cfg-tmp-*
  203. weekly [weekday]
  204. Log files are rotated once each weekday, or if the date is advanced by at least 7 days since the
  205. last rotation (while ignoring the exact time). The weekday intepretation is following: 0 means
  206. Sunday, 1 means Monday, ..., 6 means Saturday; the special value 7 means each 7 days, irrespectively
  207. of weekday. Defaults to 0 if the weekday argument is omitted.
  208. yearly Log files are rotated if the current year is not the same as the last rotation.

发表评论

表情:
评论列表 (有 0 条评论,48人围观)

还没有评论,来说两句吧...

相关阅读

    相关 logrotate日志切割

    1 logrotate原理 一般在服务器初始化的时候这些工具都已经存在的,服务的主配置文件:/etc/logrotate.conf,在主配置中可以看到 include /

    相关 Linux logrotate 命令教程日志分割

    Linux使用某些软件的时候会产生日志文件,而这些软件本身对日志不进行分割或者压缩处理,久而久之会导致日志文件异常巨大,影响机器性能,配置不高的VPS上尤为严重。而logrot

    相关 使用logrotate管理nginx日志文件

    描述:linux日志文件如果不定期清理,会填满整个磁盘。这样会很危险,因此日志管理是系统管理员日常工作之一。我们可以使用"logrotate"来管理linux日志文件,它可以实