最近在写一个简单的java命令行程序,执行代码时,代码窗口出现了烦人的debug日志。
分析了一下,出现这种问题的原因是因为,maven项目的依赖包中传递依赖了一些日志框架,导致会出现日志内容的打印。查看了一下,我的项目是依赖了logback,所以解决这个问题的办法是把logback的配置文件配置一下,覆盖掉默认的就可以了。
因此,在main/resources下面建立logback.xml,问题解决。下面是简单的logback代码
<?xml version="1.0"?>
<configuration>
<!-- ch.qos.logback.core.ConsoleAppender 控制台输出 -->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder charset="UTF-8">
<pattern>[%-5level] %d{HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<!-- 日志级别 -->
<root>
<level value="info" />
<appender-ref ref="console" />
</root>
</configuration>
也可是是复杂一点的,如下面所示,问题解决。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- ch.qos.logback.core.ConsoleAppender 控制台输出 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder charset="UTF-8">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<!-- ch.qos.logback.core.rolling.RollingFileAppender 文件日志输出 -->
<appender name="INFO"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/data/javalogs/guide_info.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/data/javalogs/guide_info.%d{yyyy-MM-dd}-%i.log</fileNamePattern>
<maxHistory>30</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<MaxFileSize>50MB</MaxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<!-- ch.qos.logback.core.rolling.RollingFileAppender 异常日志输出 -->
<appender name="ERROR"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/data/javalogs/guide_error.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/data/javalogs/guide.%d{yyyy-MM-dd}-%i.log</fileNamePattern>
<maxHistory>30</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<MaxFileSize>50MB</MaxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<!--打印info级别的日志-->
<logger name="infoLog" level="info" additivity="false">
<appender-ref ref="INFO" />
<appender-ref ref="STDOUT" />
</logger>
<!--打印异常错误日志-->
<logger name="errorLog" level="error" additivity="false">
<appender-ref ref="ERROR" />
</logger>
<!-- 日志级别 -->
<root level="info">
<appender-ref ref="INFO" />
<appender-ref ref="STDOUT" />
</root>
</configuration>