logback最为简单的日志配置
如果是springboot ,直接
logging.config=classpath:logback-spring.xml
在对应的类里面直接调用
/** * 写日志 */ private static Logger logger = LoggerFactory.getLogger(XXX.class);
如果非SpringBoot框架
直接将logback.groovy logback-test.xml logback.xml 放在资源路径下就可以
public URL findURLOfDefaultConfigurationFile(boolean updateStatus) { ClassLoader myClassLoader = Loader.getClassLoaderOfObject(this); URL url = this.findConfigFileURLFromSystemProperties(myClassLoader, updateStatus); if (url != null) { return url; } else { url = this.getResource("logback.groovy", myClassLoader, updateStatus); if (url != null) { return url; } else { url = this.getResource("logback-test.xml", myClassLoader, updateStatus); return url != null ? url : this.getResource("logback.xml", myClassLoader, updateStatus); } } }
logback的源码如上,会自动去加上资源路径下的配置文件,顺序依次是
logback.groovy logback-test.xml logback.xml
接下来就是logback配置文件的内容了
<?xml version="1.0" encoding="UTF-8"?> <configuration>
<!-- 从高到地低 OFF 、 FATAL 、 ERROR 、 WARN 、 INFO 、 DEBUG 、 TRACE 、 ALL --><!-- 重置log位置 --> <property name ="LOG_HOME" value ="${HOME}/logs" ></property>
<!-- 日志格式 --> <property name="consoleLayoutPattern"value ="%-20(%d{yyyyMMdd_HH:mm:ss.SSS} [%level] [%thread] [%logger] [%-4relative]) %msg%n" /> <property name ="fileLayoutPattern" value ="%-20(%d{yyyyMMdd_HH:mm:ss.SSS} [%level] [%thread] [%logger{10}] [%-4relative]) %msg%n" />
<!-- 定义日志存在路径,和内容过滤 --> <appender name ="STDOUT" class ="ch.qos.logback.core.ConsoleAppender" > <layout class ="ch.qos.logback.classic.PatternLayout" > <pattern>${consoleLayoutPattern} </pattern> </layout> </appender>
<!-- 定义日志存在路径,和内容过滤 -->
<appender
name
="DEBUG_FILE"
class
="ch.qos.logback.core.rolling.RollingFileAppender"
>
<file>${LOG_HOME}/zf-ud1exec-server/zf-ud1exec-server_debug.log
</file>
<rollingPolicy
class
="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"
>
<fileNamePattern>${LOG_HOME}/zf-ud1exec-server/commonConfig_debug.log.%d{yyyy-MM-dd}
</fileNamePattern>
<maxHistory>10
</maxHistory>
</rollingPolicy>
<layout
class
="ch.qos.logback.classic.PatternLayout"
>
<Pattern>${fileLayoutPattern}
</Pattern>
</layout>
<filter
class
="ch.qos.logback.classic.filter.LevelFilter"
>
<level>debug
</level>
<onMismatch>DENY
</onMismatch>
</filter>
</appender>
<appender
name
="INFO_FILE"
class
="ch.qos.logback.core.rolling.RollingFileAppender"
>
<file>${LOG_HOME}/zf-ud1exec-server/zf-ud1exec-server_info.log
</file>
<rollingPolicy
class
="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"
>
<fileNamePattern>${LOG_HOME}/zf-ud1exec-server/commonConfig_info.log.%d{yyyy-MM-dd}
</fileNamePattern>
<maxHistory>10
</maxHistory>
</rollingPolicy>
<layout
class
="ch.qos.logback.classic.PatternLayout"
>
<Pattern>${fileLayoutPattern}
</Pattern>
</layout>
<filter
class
="ch.qos.logback.classic.filter.LevelFilter"
>
<level>info
</level>
<onMismatch>DENY
</onMismatch>
</filter>
</appender>
<appender
name
="ERROR_FILE"
class
="ch.qos.logback.core.rolling.RollingFileAppender"
>
<file>${LOG_HOME}/zf-ud1exec-server/zf-ud1exec-server_error.log
</file>
<rollingPolicy
class
="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"
>
<fileNamePattern>${LOG_HOME}/zf-ud1exec-server/zf-ud1exec-server_error.log.%d{yyyy-MM-dd}
</fileNamePattern>
<maxHistory>10
</maxHistory>
</rollingPolicy>
<layout
class
="ch.qos.logback.classic.PatternLayout"
>
<Pattern>${fileLayoutPattern}
</Pattern>
</layout>
<filter
class
="ch.qos.logback.classic.filter.LevelFilter"
>
<level>error
</level>
<onMismatch>DENY
</onMismatch>
</filter>
</appender>
<!-- 定义包的日志级别
-->
<logger
name
="com.XXX.ud1exec"
level
="DEBUG"
/>
<logger
name
="org.apache.zookeeper"
level
="ERROR"
/>
<logger
name
="org.springframework"
level
="ERROR"
/>
<logger
name
="kafka"
level
="ERROR"
/>
<logger
name
="org.I0Itec"
level
="ERROR"
/>
<logger
name
="org.apache.hadoop"
level
="ERROR"
/>
<logger
name
="com.XXXX.common.utils"
level
="ERROR"
/>
<root>
<level
value
="INFO"
/>
<!--
系统全局日志输出最低级别
-->
<appender-ref
ref
="STDOUT"
/>
<appender-ref
ref
="DEBUG_FILE"
/>
<appender-ref
ref
="INFO_FILE"
/>
<appender-ref
ref
="ERROR_FILE"
/>
</root>
</configuration>