Logback详细说明可以参考这篇文章: logback的使用和logback.xml详解.
本模块我们通过一段模板来说明Logback配置如何使用
%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%X{userId}] [%X{requestId}] %logger - %msg%n
UTF-8
${catalina.base}/logs/log-info.log
${catalina.base}/logs/log-info-%d{yyyy-MM-dd}.log
30
%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%X{accountId}] [%X{requestId}] %logger - %msg%n
INFO
ACCEPT
DENY
UTF-8
${catalina.base}/logs/log-error.log
${catalina.base}/logs/log-error-%d{yyyy-MM-dd}.log
90
%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%X{accountId}] [%X{requestId}] %logger - %msg%n
ERROR
ACCEPT
DENY
Log4j详细说明可以参考这篇文章: log4j.properties 详解与配置步骤.
本模块我们通过一段模板来说明Logback配置如何使用
#定义LOG输出级别
log4j.rootLogger=INFO,Console,File,DEBUG,ERROR
#定义日志输出目的地为控制台
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
#可以灵活地指定日志输出格式,下面一行是指定具体的格式
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n
#文件大小到达指定尺寸的时候产生一个新的文件
log4j.appender.File = org.apache.log4j.RollingFileAppender
#指定输出目录
log4j.appender.File.File =${catalina.base}/logs/log-all.log
#定义文件最大大小
log4j.appender.File.MaxFileSize = 10MB
#输出所有日志,如果换成DEBUG表示输出DEBUG以上级别日志
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [%c] - [ %p ] %m%n
###输出DEBUG 级别以上的日志到
log4j.appender.DEBUG = org.apache.log4j.DailyRollingFileAppender
log4j.appender.DEBUG.File = ${catalina.base}/logs/log-debug.log
log4j.appender.DEBUG.Append = true
log4j.appender.DEBUG.Threshold = DEBUG
log4j.appender.DEBUG.layout = org.apache.log4j.PatternLayout
log4j.appender.DEBUG.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [%c] - [ %p ] %m%n
###输出INFO 级别以上的日志
log4j.appender.INFO = org.apache.log4j.DailyRollingFileAppender
log4j.appender.INFO.File =${catalina.base}/logs/log-info.log
log4j.appender.INFO.Append = true
log4j.appender.INFO.Threshold = INFO
log4j.appender.INFO.layout = org.apache.log4j.PatternLayout
log4j.appender.INFO.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [%c] - [ %p ] %m%n
###输出ERROR 级别以上的日志
log4j.appender.ERROR = org.apache.log4j.DailyRollingFileAppender
log4j.appender.ERROR.File =${catalina.base}/logs/log-error.log
log4j.appender.ERROR.Append = true
log4j.appender.ERROR.Threshold = ERROR
log4j.appender.ERROR.layout = org.apache.log4j.PatternLayout
log4j.appender.ERROR.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [%c] - [ %p ] %m%n
(1)根据项目实际情况选择tomcat打印的日志,不需要的模块直接注释即可,通过conf/log4j.properties进行控制
1catalina.org.apache.juli.AsyncFileHandler.level = FINE
1catalina.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
1catalina.org.apache.juli.AsyncFileHandler.prefix = catalina.
2localhost.org.apache.juli.AsyncFileHandler.level = FINE
2localhost.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
2localhost.org.apache.juli.AsyncFileHandler.prefix = localhost.
3manager.org.apache.juli.AsyncFileHandler.level = FINE
3manager.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
3manager.org.apache.juli.AsyncFileHandler.prefix = manager.
4host-manager.org.apache.juli.AsyncFileHandler.level = FINE
4host-manager.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
4host-manager.org.apache.juli.AsyncFileHandler.prefix = host-manager.
(2)还可将localhost_access_log日志关闭输出,通过编辑/conf/server.xml,将以下模块注释
编辑bin/catalina.sh,将以下这行替换
CATALINA_OUT="$CATALINA_BASE"/logs/catalina.out
替换为
CATALINA_OUT="$CATALINA_BASE"/logs/catalina.`date +%Y-%m-%d`.out
在bin目录下创建一个定时清理脚本
cd ./bin
touch clean.sh
vi ./clean.sh
添加以下内容(注:内部文件路径请根据实际情况替换):
#!/bin/bash
logs_path="/usr/local/tomcat9/logs/"
find $logs_path -mtime +15 -name "*20?*" -exec rm -rf {} \;
echo " " >$logs_path catalina.out;
#!/bin/bash
logs_path="/usr/local/tomcat9/logs/"
d=`date +%Y-%m-%d`
cp $logs_path/catalina.out $logs_path/catalina.${d}.out
>$logs_path/catalina.out;
find $logs_path -mtime +30 -name "catalina.*.out" -exec rm -rf {} \;
find $logs_path -mtime +30 -name "localhost.*.log" -exec rm -rf {} \;
find $logs_path -mtime +30 -name "localhost_access_log.*.txt" -exec rm -rf {} \;
find $logs_path -mtime +30 -name "catalina.*.log" -exec rm -rf {} \;
find $logs_path -mtime +30 -name "manager.*.log" -exec rm -rf {} \;
find $logs_path -mtime +30 -name "host-manager.*.log" -exec rm -rf {} \;
find $logs_path -mtime +30 -name "fileservice.log.*" -exec rm -rf {} \;
给文件授权,并添加可执行文件
chmod +X ./clean.sh
chmod 777 ./clean.sh
配置Conrolog定时清理日志,使用crontab -l查看当前任务,在确认没有其他任务执行的情况下,使用crontab -e新建一个定时执行任务,输入如下内容:
#每天早上10点30执行脚本
30 10 * * * /usr/local/tomcat9//bin/clean.sh
至此清理配置即完成了
crontab常用命令如下(可参考该链接 Linux crontab命令.)
#执行文字编辑器来设定时程表,内定的文字编辑器是 VI
crontab -e
#删除目前的时程表
crontab -r
#列出目前的时程表
crontab -l
文件百度云下载 链接 提取码:pzab
# Defaults:
ac_help=
# 替换ac_default_prefix中的路径,配置安装路径
ac_default_prefix=/app/cronolog/bin/
./configure
make
make install
友情参考链接:
上文如有引用争议请联系我,沟通解决问题,谢谢
·······························
攻城狮之路,互勉以修远。