片段 1

片段 2

LogbackColorful.java


package cn.mrxionge.netdemo; import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.pattern.color.ANSIConstants;
import ch.qos.logback.core.pattern.color.ForegroundCompositeConverterBase; public class LogbackColorful extends ForegroundCompositeConverterBase<ILoggingEvent> { @Override
protected String getForegroundColorCode(ILoggingEvent event) {
Level level = event.getLevel();
switch (level.toInt()) {
case Level.ERROR_INT:
return ANSIConstants.RED_FG;
case Level.WARN_INT:
return ANSIConstants.YELLOW_FG;
case Level.INFO_INT:
return ANSIConstants.BLUE_FG;
case Level.DEBUG_INT:
return ANSIConstants.GREEN_FG;
default:
return ANSIConstants.DEFAULT_FG;
}
}
}

logback.xml


<configuration>
<!--自定义颜色配置-->
<conversionRule conversionWord="customcolor" converterClass="cn.mrxionge.netdemo.LogbackColorful"/>
<!--控制台日志配置-->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %magenta([%thread]) %customcolor(%-5level) %cyan(%logger{15}) - %msg%n
</pattern>
</encoder>
</appender>
<!--日志文件配置-->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logs/App%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>15</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<!--自定义日志等级-->
<logger name="cn.mrxionge" level="debug"/>
<logger name="io.vertx" level="debug"/>
<logger name="io.netty" level="debug"/>
<!--全局日志等级-->
<root level="error">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"/>
</root>
</configuration>
05-22 02:08