1.nacos日志清理
修改nacos-logback.xml
将日志级别改为error级,减少info级日志产生量
将<maxHistory>调整为2以下,将 <totalSizeCap>调整为2GB左右
比如:
[root@iZ0jlapur4hqjezy8waee0Z logs]# ll -h
total 2.1G
-rw-r--r-- 1 root root 13K Aug 1 13:18 alipay-jraft.log
-rw-r--r-- 1 root root 0 Aug 1 13:36 cmdb-main.log
-rw-r--r-- 1 root root 519M Aug 1 13:36 config-client-request.log
-rw-r--r-- 1 root root 1.3M Aug 1 13:36 config-memory.log
-rw-r--r-- 1 root root 444M Aug 1 13:36 config-pull-check.log
-rw-r--r-- 1 root root 608M Aug 1 13:36 config-trace.log
-rw-r--r-- 1 root root 20M Aug 1 13:36 nacos.log
-rw-r--r-- 1 root root 51M Aug 1 08:36 nacos.log.2023-08-01.10
-rw-r--r-- 1 root root 51M Aug 1 09:23 nacos.log.2023-08-01.11
-rw-r--r-- 1 root root 51M Aug 1 10:10 nacos.log.2023-08-01.12
-rw-r--r-- 1 root root 51M Aug 1 10:57 nacos.log.2023-08-01.13
-rw-r--r-- 1 root root 51M Aug 1 11:44 nacos.log.2023-08-01.14
-rw-r--r-- 1 root root 51M Aug 1 12:31 nacos.log.2023-08-01.15
-rw-r--r-- 1 root root 51M Aug 1 13:18 nacos.log.2023-08-01.16
-rw-r--r-- 1 root root 51M Aug 1 06:15 nacos.log.2023-08-01.7
-rw-r--r-- 1 root root 51M Aug 1 07:02 nacos.log.2023-08-01.8
-rw-r--r-- 1 root root 51M Aug 1 07:49 nacos.log.2023-08-01.9
-rw-r--r-- 1 root root 808 Jul 31 10:29 naming-event.log
-rw-r--r-- 1 root root 236K Aug 1 13:35 naming-performance.log
-rw-r--r-- 1 root root 38K Jul 31 10:37 naming-push.log
-rw-r--r-- 1 root root 7.1K Jul 31 10:31 naming-server.log
通过ll -h命令查出config-trace.log和config-pull-check.log比较大,在nacos-logback.xml中查找相关日志的配置:
把相应的日志文件大小改小:
<appender name="traceLog"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/config-trace.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}/config-trace.log.%d{yyyy-MM-dd}.%i</fileNamePattern>
<maxFileSize>50MB</maxFileSize>
<maxHistory>7</maxHistory>
<totalSizeCap>512MB</totalSizeCap>
<cleanHistoryOnStart>true</cleanHistoryOnStart>
</rollingPolicy>
<encoder>
<Pattern>%date|%msg%n</Pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<appender name="pullCheckFile"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/config-pull-check.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}/config-pull-check.log.%d{yyyy-MM-dd}.%i</fileNamePattern>
<maxFileSize>50MB</maxFileSize>
<maxHistory>7</maxHistory>
<totalSizeCap>512MB</totalSizeCap>
<cleanHistoryOnStart>true</cleanHistoryOnStart>
</rollingPolicy>
<encoder>
<Pattern>%msg%n</Pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
调整日志级别为error级,以减少日志产生量:
<logger name="com.alibaba.nacos.config.traceLog" additivity="false">
<level value="error"/>
<appender-ref ref="traceLog"/>
</logger>
<logger name="com.alibaba.nacos.config.pullCheckLog" additivity="false">
<level value="error"/>
<appender-ref ref="pullCheckFile"/>
</logger>
这样就能及时清理日志,减小日志占用的空间
改后重启nacos
[root@iZ0jlapur4hqjezy8waee0Z nacos]# cd bin
[root@iZ0jlapur4hqjezy8waee0Z bin]# ls
derby.log file: logPath_IS_UNDEFINED logs shutdown.cmd shutdown.sh startup.cmd startup.sh work
[root@iZ0jlapur4hqjezy8waee0Z bin]# ./shutdown.sh
The nacosServer(20623) is running...
Send shutdown request to nacosServer(20623) OK
[root@iZ0jlapur4hqjezy8waee0Z bin]# ./startup.sh (这是集群启动方式,不应该用集群方式启动)
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.372.b07-1.el7_9.x86_64/bin/java -Djava.ext.dirs=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.372.b07-1.el7_9.x86_64/jre/lib/ext:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.372.b07-1.el7_9.x86_64/lib/ext -server -Xms2g -Xmx2g -Xmn1g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/home/nacos/logs/java_heapdump.hprof -XX:-UseLargePages -Dnacos.member.list= -Xloggc:/home/nacos/logs/nacos_gc.log -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M -Dloader.path=/home/nacos/plugins,/home/nacos/plugins/health,/home/nacos/plugins/cmdb,/home/nacos/plugins/selector -Dnacos.home=/home/nacos -jar /home/nacos/target/nacos-server.jar --spring.config.additional-location=file:/home/nacos/conf/ --logging.config=/home/nacos/conf/nacos-logback.xml --server.max-http-header-size=524288
nacos is starting with cluster
nacos is starting,you can check the /home/nacos/logs/start.out
需要使用单例方式启动:
[root@iZ0jlapur4hqjezy8waee0Z bin]# ./startup.sh -m standalone
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.372.b07-1.el7_9.x86_64/bin/java -Djava.ext.dirs=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.372.b07-1.el7_9.x86_64/jre/lib/ext:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.372.b07-1.el7_9.x86_64/lib/ext -Xms512m -Xmx512m -Xmn256m -Dnacos.standalone=true -Dnacos.member.list= -Xloggc:/home/nacos/logs/nacos_gc.log -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M -Dloader.path=/home/nacos/plugins,/home/nacos/plugins/health,/home/nacos/plugins/cmdb,/home/nacos/plugins/selector -Dnacos.home=/home/nacos -jar /home/nacos/target/nacos-server.jar --spring.config.additional-location=file:/home/nacos/conf/ --logging.config=/home/nacos/conf/nacos-logback.xml --server.max-http-header-size=524288
nacos is starting with standalone
nacos is starting,you can check the /home/nacos/logs/start.out
Nacos系列(4)-Nacos各种日志太多问题的终极解决办法_nacos日志_云烟成雨TD的博客-CSDN博客
2.设置自动删除定时任务(旧方法,不推荐)
打开终端并输入 crontab -e 命令来编辑 crontab 文件。
在文件末尾添加以下行(将 /path/to/access_log 替换为您的 access_log 文件的实际路径):
0 1 * * * find /home/nacos/logs -mtime +2 -type f -delete
复制
保存并关闭文件。
这样,每天凌晨 1 点,find 命令将搜索 access_log 目录中修改时间超过 2 天的文件,并将其删除。
3.有比 crontab更好的工具 logrotate
logrotate实现日志切割和清理(清晰易懂)_Smart_Maggie的博客-CSDN博客
logrotate实现日志切割和清理(清晰易懂)_Smart_Maggie的博客-CSDN博客
使用时需配置:vim /etc/logrotate.conf
keep 4 weeks worth of backlogs
rotate 4
create new (empty) log files after rotating old ones
create
use date as a suffix of the rotated file
dateext
uncomment this if you want your log files compressed
#compress
RPM packages drop log rotation information into this directory
include /etc/logrotate.d
no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
monthly
create 0664 root utmp
minsize 1M
rotate 1
}
/var/log/btmp {
missingok
monthly
create 0600 root utmp
rotate 1
}
system-specific logs may be also be configured here.
nginx big log
/usr/local/nginx/logs/webaccess.log {
daily
rotate 7
missingok
notifempty
compress
delaycompress
sharedscripts
postrotate
/usr/local/nginx/sbin/nginx -s reopen
endscript
}
"/etc/logrotate.conf" 49L, 895C
无需运行logrotate,在系统的/etc/cron.daily/logrotate默认配置中,已经设定为每天执行一次。
logrotate在很多Linux发行版上都是默认安装的。系统会定时运行logrotate,一般是每天一次。系统是这么实现按天执行的。crontab会每天定时执行/etc/cron.daily目录下的脚本,而这个目录下有个文件叫logrotate。在centos上脚本内容是这样的:
/etc/cron.daily/logrotate
/usr/sbin/logrotate /etc/logrotate.conf >/dev/null 2>&1
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0
logrotate机制与原理
logrotate机制与原理_wx61ee58d59725e的技术博客_51CTO博客
4.tomcat日志清理
- 创建一个logrotate配置文件来管理Tomcat日志。在/etc/logrotate.d/目录下创建一个新文件并将其命名为tomcat,例如:
Copy
sudo nano /etc/logrotate.d/tomcat
- 在文件中添加以下内容:
Copy
/path/to/tomcat/logs/catalina.out {
daily
rotate 7
compress
missingok
notifempty
copytruncate
}
其中:
-
/path/to/tomcat/logs/catalina.out 是Tomcat日志文件的路径。如果您的Tomcat配置文件中指定了不同的日志文件名,请相应地更改路径。
-
daily - 每天轮换日志文件。
-
rotate 7 - 保留7个旧日志文件。
-
compress - 压缩旧的日志文件。
-
missingok - 如果日志文件丢失,不要给出错误。
-
notifempty - 如果日志文件为空,不要轮换日志文件。
-
copytruncate - 不关闭Tomcat进程,直接拷贝并截断日志文件。
-
保存并关闭文件。
-
测试logrotate配置是否正确。使用以下命令手动运行logrotate:
Copy
sudo logrotate -vf /etc/logrotate.d/tomcat
-vf参数用于输出日志并强制执行logrotate。
bash
[root@iZ8vb2ef68w87mgjcj9m06Z logrotate.d]# cat tomcat
/app/web-5210/logs/catalina.out {
daily
rotate 7
compress
missingok
notifempty
copytruncate
}
/app/web-5210/logs/catalina.*.log
/app/web-5210/logs/localhost.*.log
/app/web-5210/logs/localhost_access_log.*.txt
/app/web-5210/logs/manager.*.log
{
daily
rotate 7
compress
missingok
notifempty
}
5.nginx日志清理
bash
[root@iZ8vb2ef68w87mgjcj9m06Z logrotate.d]# cat nginx
# nginx big log
/usr/local/nginx/logs/access.log
/usr/local/nginx/logs/nginx_error.log
/usr/local/nginx/logs/webaccess.log
{
daily
rotate 7
missingok
notifempty
compress
delaycompress
sharedscripts
postrotate
/usr/local/nginx/sbin/nginx -s reopen
endscript
}