1、启动的时候加载指定目录的logback.xml文件
nohup java -Xms256m -Xmx1024m -Dspring.config.location=config/application.yml,config/application-druid.yml -Dlogging.config=config/logback.xml -jar ruoyi-admin.jar >nobup.log 2>&1 &
APP_NAME=ruoyi-admin.jar
APP_CONFIG=config/application.yml,config/application-druid.yml
LOGBACK_CONFIG=config/logback.xml
#启动方法
start(){
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is already running. pid=${pid} ."
else
nohup java -Xms256m -Xmx1024m -Dspring.config.location=$APP_CONFIG -Dlogging.config=$LOGBACK_CONFIG -jar $APP_NAME >nobup.log 2>&1 &
fi
}
2、参考
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!--定义日志文件的存储地址 勿在 LogBack 的配置中使用相对路径 -->
<!-- <property name="LOG_HOME" value="d:" /> -->
<!-- ch.qos.logback.core.ConsoleAppender 控制台输出 -->
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<!-- error日志输出-保留7天 -->
<appender name="ErrorFile"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 正在记录的日志文件的路径及文件名 -->
<File>${LOG_HOME:-c:/}logs/error/error.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 基于时间和大小的轮转策略中fileNamePattern位置很重要,他决定了生成文件的名称及轮转时机,以及是否压缩 -->
<!-- %d决定以什么时间维度轮转(但实际轮转时机取决于日志事件的到达时间),比如%d{yyyy/MM}:每个月开始的时候轮转,%d默认为 yyyy-MM-dd:按天轮转 -->
<!-- %i为文件按照maxFileSize大小规定轮转后的序号 -->
<FileNamePattern>${LOG_HOME:-c:/}logs/error/error-%d{yyyy-MM-dd}.%i.log</FileNamePattern>
<!--日志文件保留天数-->
<MaxHistory>7</MaxHistory>
<!--每个文件最多100MB,保留7天的历史记录,但最多20GB-->
<TotalSizeCap>20GB</TotalSizeCap>
<!--重启清理日志文件-->
<CleanHistoryOnStart>true</CleanHistoryOnStart>
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
<TimeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!--每个文件最多100MB-->
<MaxFileSize>100MB</MaxFileSize>
</TimeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<!--日志文件输出格式-->
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n</pattern>
</layout>
<!-- 只打印错误日志 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 所有日志输出-保留7天 -->
<appender name="AllFile"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${LOG_HOME:-c:/}logs/all/all.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${LOG_HOME:-c:/}logs/all/all-%d{yyyy-MM-dd}.%i.log</FileNamePattern>
<MaxHistory>7</MaxHistory>
<TotalSizeCap>20GB</TotalSizeCap>
<CleanHistoryOnStart>true</CleanHistoryOnStart>
<TimeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<MaxFileSize>100MB</MaxFileSize>
</TimeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n</pattern>
</layout>
</appender>
<!-- 压缩日志输出 start -->
<!-- 所有日志输出 -->
<appender name="GzAllFile"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${LOG_HOME:-c:/}logs/gz/all/all.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 后缀以".zip"或".gz"结尾,则开启日志文件压缩 -->
<FileNamePattern>${LOG_HOME:-c:/}logs/gz/all/all-%d{yyyy-MM-dd}.%i.log.gz</FileNamePattern>
<MaxHistory>3600</MaxHistory>
<TimeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<MaxFileSize>100MB</MaxFileSize>
</TimeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n
</pattern>
</layout>
</appender>
<!-- error日志输出 -->
<appender name="GzErrorFile"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${LOG_HOME:-c:/}logs/gz/error/error.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${LOG_HOME:-c:/}logs/gz/error/error-%d{yyyy-MM-dd}.%i.log.gz
</FileNamePattern>
<MaxHistory>3600</MaxHistory>
<TimeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<MaxFileSize>100MB</MaxFileSize>
</TimeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n
</pattern>
</layout>
<filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印错误日志 -->
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 压缩日志输出 end -->
<root level="info">
<appender-ref ref="stdout" />
<appender-ref ref="ErrorFile" />
<appender-ref ref="AllFile" />
<appender-ref ref="GzAllFile" />
<appender-ref ref="GzErrorFile" />
</root>
</configuration>
3、若依框架分离版的
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- 日志存放路径 -->
<property name="log.path" value="/home/weijianwei/nhsa/logs" />
<!-- 日志输出格式 -->
<property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n" />
<!-- 控制台输出 -->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
</appender>
<!-- 系统日志输出 -->
<appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/sys-info.log</file>
<!-- 循环政策:基于时间创建日志文件 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 日志文件名格式 -->
<fileNamePattern>${log.path}/sys-info.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 日志最大的历史 2 天 -->
<maxHistory>2</maxHistory>
<CleanHistoryOnStart>true</CleanHistoryOnStart>
</rollingPolicy>
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<!-- 过滤的级别 -->
<level>INFO</level>
<!-- 匹配时的操作:接收(记录) -->
<onMatch>ACCEPT</onMatch>
<!-- 不匹配时的操作:拒绝(不记录) -->
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/sys-error.log</file>
<!-- 循环政策:基于时间创建日志文件 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 日志文件名格式 -->
<fileNamePattern>${log.path}/sys-error.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 日志最大的历史 2 天 -->
<maxHistory>2</maxHistory>
<CleanHistoryOnStart>true</CleanHistoryOnStart>
</rollingPolicy>
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<!-- 过滤的级别 -->
<level>ERROR</level>
<!-- 匹配时的操作:接收(记录) -->
<onMatch>ACCEPT</onMatch>
<!-- 不匹配时的操作:拒绝(不记录) -->
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 用户访问日志输出 -->
<appender name="sys-user" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/sys-user.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 按天回滚 daily -->
<fileNamePattern>${log.path}/sys-user.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 日志最大的历史 2 天 -->
<maxHistory>2</maxHistory>
<CleanHistoryOnStart>true</CleanHistoryOnStart>
</rollingPolicy>
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
</appender>
<!-- 系统模块日志级别控制 -->
<logger name="com.ruoyi" level="info" />
<!-- Spring日志级别控制 -->
<logger name="org.springframework" level="warn" />
<root level="info">
<appender-ref ref="console" />
</root>
<!--系统操作日志-->
<root level="info">
<appender-ref ref="file_info" />
<appender-ref ref="file_error" />
</root>
<!--系统用户操作日志-->
<logger name="sys-user" level="info">
<appender-ref ref="sys-user"/>
</logger>
</configuration>