国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Linux > 正文

Linux使用logrotate來切割日志文件

2024-08-28 00:02:24
字體:
供稿:網(wǎng)友

程序在運行的時候為了了解運行狀態(tài),會輸出日志文件,時間久了日志文件會變得非常大,甚至達到GB級別。我在golang應(yīng)用里使用logrus包來打日志,配置和使用都很方便,就是沒有日志分割的功能,應(yīng)用在線上運行一個月后日志文件都已經(jīng)達到上百兆。后來發(fā)現(xiàn)了logrotate,這是centos自帶的日志分割工具,都不用安裝額外組件就能實現(xiàn)定時分割日志。

1.運行原理

logrotate由系統(tǒng)的cron運行,位置在/etc/cron.daily/logrotate

#!/bin/sh/usr/sbin/logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.confEXITVALUE=$?if [ $EXITVALUE != 0 ]; then /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"fiexit 0

可以看到入口配置文件是/etc/logrotate.conf,依次運行/etc/logrotate.conf.d里的配置文件 如果發(fā)現(xiàn)配置的logrotate沒有執(zhí)行,可以看下系統(tǒng)的crond服務(wù)有沒有開啟

2.配置

如果有安裝nginx,可以參考nginx里的配置例子

/var/log/nginx/*log { create 0644 nginx nginx daily rotate 10 missingok notifempty compress sharedscripts postrotate  /bin/kill -USR1 `cat /run/nginx.pid 2>/dev/null` 2>/dev/null || true endscript}

第一行定義的是日志文件的路徑,可以用*通配,一般可以定義成*.log來匹配所有日志文件。也可以指定多個文件,用空格隔開,比如

/var/log/nginx/access.log /var/log/nginx/error.log { }

花括號里面是日志切割相關(guān)的參數(shù),下面是常用的切割參數(shù)

compress 是否開啟壓縮,壓縮格式gzip 不開啟壓縮 compresscmd 自定義壓縮命令 compressexty 壓縮文件名后綴 compressoptions 壓縮選項 copy 復制一份文件 create 后面跟mode owner group,設(shè)置新日志文件的權(quán)限 daily 按天分割 weekly 按周分割 monthly 按月分割 rotate 后面跟數(shù)字,表示需要保留的文件歷史記錄,超過數(shù)量就會刪除,或者通過郵件發(fā)送 size 后面跟文件大小,比如100k、100M,超過這個大小后分割 missingok 忽略不存在的文件,不報錯 notifempty 不分割空文件 sharedscripts 配合postrotate、prerotate,讓他們只執(zhí)行一次 postrotate/endscript 文件分割完后,執(zhí)行postrotate、endscript之間的命令 prerotate/endscript 文件分割完前,執(zhí)行prerotate、endscript之間的命令

下面看幾個例子

/var/log/httpd/error.log { rotate 5 mail i@wuyuans.com size=100k sharedscripts postrotate  /sbin/killall -HUP httpd endscript}

切割/var/log/httpd/error.log日志文件,超過100k后切割,保留最新的5個歷史記錄,超過5個的郵件發(fā)送到fss@qq.com,postrotate里的的命令是為了讓httpd重新打開日志文件。

/var/lib/mysql/mysqld.log { # create 600 mysql mysql notifempty daily rotate 3 missingok compress postrotate # just if mysqld is really running if test -x /usr/bin/mysqladmin && / /usr/bin/mysqladmin ping &>/dev/null then /usr/bin/mysqladmin --local flush-error-log /    flush-engine-log flush-general-log flush-slow-log fi endscript}

這是對mysql日志的切割,每天一份,忽略空文件,保留最新3份,使用gzip壓縮

/home/wuyuan/log/*.log { su wuyuan wuyuan create 0777 wuyuan wuyuan daily rotate 10 olddir /home/wuyuan/log/old missingok postrotate endscript nocompress}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 灵璧县| 吉隆县| 周至县| 宝应县| 桑日县| 密云县| 额尔古纳市| 洪泽县| 江口县| 东兰县| 竹山县| 安庆市| 洛南县| 惠来县| 万安县| 新巴尔虎右旗| 虹口区| 灵丘县| 伽师县| 陆良县| 无锡市| 华安县| 阳山县| 安仁县| 武宣县| 滨海县| 日照市| 东城区| 富平县| 长兴县| 金昌市| 广汉市| 彭泽县| 乌拉特前旗| 长乐市| 石泉县| 桃源县| 信宜市| 封开县| 沂南县| 镇康县|