Logback - spring 配置 logback.xml (彩色)
有一个公司的老项目,日志文件是放在工程里面配置的,启动的时候自动加载logback.xml文件,给我们后期维护和修改带来极大的不便;
开始不清楚缘由,看到项目里有使用slft4j,也有java自带的log工具包,权衡利弊后,决定吧logback放在外面,并且实现log按照日期每天生成一个log文件,但是多次试验后,发现放在部署目录下并不起作用,奇了怪了,后来忽然想起来,项目的配置文件在启动的main方法是单独加载的,是不是也要把这个logback.xml也单独拎出来,并且在启动的时候加载到项目呢;
//load logback.xml
private static void populateLogback(String path) {
String file_name = path + "/logback.xml" ;
File logbackFile = new File(file_name);
LOGGER.debug("Absolute property file path with file name: {}", logbackFile.getAbsolutePath());
if (logbackFile.exists()) {
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(lc);
lc.reset();
try {
configurator.doConfigure(logbackFile);
} catch (JoranException e) {
e.printStackTrace(System.err);
System.exit(-1);
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<property name="LOG_HOME" value="." />
<!-- <property name="LOG_PATTERN" value="#[%date{ddMMyy HH:mm:ss,SSS} | ${HOSTNAME} | %-5level | %t | %logger{0} | %msg]#%n" /> -->
<property name="LOG_PATTERN" value="#[%d{ddMMyy HH:mm:ss.SSS} | ${HOSTNAME} | %-5level | %t | %logger{0} | %msg]#%n"/>
<!-- <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<charset>UTF-8</charset>
<pattern>#[%date{"ddMMyy HH:mm:ss,SSS"} | ${HOSTNAME} | %-5level | %t | %logger{0} | %msg]#%n</pattern>
</encoder>
</appender> -->
<!-- 控制台输出 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${LOG_PATTERN}</pattern>
</encoder>
</appender>
<appender name="server" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!--日志文件输出的文件名 -->
<fileNamePattern>${LOG_HOME}/logs/test-server.%d{yyyy-MM-dd}.%i.log </fileNamePattern>
<!--日志文件保留天数 -->
<maxFileSize>200MB</maxFileSize>
<maxHistory>30</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${LOG_PATTERN}</pattern>
</encoder>
</appender>
<logger name="com.daopinz.test" level="DEBUG" additivity="false">
<appender-ref ref="server" />
<appender-ref ref="STDOUT" />
</logger>
<!-- 3rdparty Loggers -->
<logger name="org.springframework.core" level="INFO" additivity="false">
<appender-ref ref="STDOUT" />
</logger>
<logger name="org.springframework.beans" level="INFO" additivity="false">
<level value="info" />
<appender-ref ref="STDOUT" />
</logger>
<logger name="org.springframework.context" level="INFO" additivity="false">
<appender-ref ref="STDOUT" />
</logger>
<logger name="org.springframework.web" level="INFO" additivity="false">
<appender-ref ref="STDOUT" />
</logger>
<logger name="org.jboss.resteasy" level="DEBUG" additivity="false">
<appender-ref ref="STDOUT" />
</logger>
<root level="info">
<appender-ref ref="STDOUT" />
</root>
</configuration>
more logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- magenta:洋红 -->
<!-- boldMagenta:粗红-->
<!-- cyan:青色 -->
<!-- white:白色 -->
<!-- magenta:洋红 -->
<!-- boldYellow:金黄 -->
<!-- green:绿色 -->
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<!--定义日志文件的存储地址 勿在 LogBack 的配置中使用相对路径 -->
<property name="LOG_HOME" value="."/>
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
<!--<property name="LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n"/>-->
<property name="LOG_PATTERN" value="%red(%d{yyyy-MM-dd HH:mm:ss.SSS}) %cyan([%thread]) %highlight(%-5level) %green(%logger{36}) - %boldY
ellow(%msg%n)" />
<!-- 控制台输出 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${LOG_PATTERN}</pattern>
</encoder>
</appender>
<appender name="http-server" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!--日志文件输出的文件名 -->
<fileNamePattern>${LOG_HOME}/log/http-server.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>100MB</maxFileSize>
<maxHistory>30</maxHistory>
<!--日志文件保留天数 -->
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${LOG_PATTERN}</pattern>
</encoder>
</appender>
<logger name="com.test.http.server" level="DEBUG">
<appender-ref ref="http-server"/>
<appender-ref ref="STDOUT"/>
</logger>
</configuration>
但是,同时有其他的问题,我们vim nohup.out打开文件的时候,会变成这个样子;