首页 > 代码库 > Logback 将日志分级别打印
Logback 将日志分级别打印
最近项目中用到了logback 记录日志, 关于为啥使用logback 请百度一下: logback与Log4J的区别
好了,废话不多说,直奔主题, 研究了好久,终于将日志按级别将日志分文件打印出来了, 具体步骤如下:
1. 下载对于的JAR 包:
http://logback.qos.ch/
logback-access-1.1.2.jar
logback-classic-1.1.2.jar
logback-core-1.1.2.jar
http://www.slf4j.org/
slf4j-api-1.7.10.jar
2. 创建一个web project, 写一个简单的JAVA class 或者Servlet.
public class ILoveYou{ private final static Logger log = LoggerFactory.getLogger(ILoveYou.class); public static void main(String[] arg){ log.trace("==111111111=======trace ==================="); log.debug("============22222===debug2222222===================="); log.info("=====================33333333info333333333===================="); log.warn(" =====================warn=444444444444==================="); log.error(" =====================error====55555555555================"); } }
4. 将第一步所下载的jar包导入, 并运行, 控制台打印出如下信息
==111111111=======trace ===================
============22222===debug2222222====================
=====================33333333info333333333====================
=====================warn=444444444444===================
=====================error====55555555555================
5. 建立日志配置文件: logback.xml (或者logback-test.xml)
<?xml version="1.0" encoding="UTF-8"?> <configuration> <!-- 定义log 文件的 home 目录 --> <property name="LOG_HOME" value="C:/Users/Administrator/Desktop/logs"></property> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <!-- %.-1level 只显示信息级别的首字母,%-5level 左对齐显示信息级别全称 --> <Pattern>[%date{yyyy-MM-dd HH:mm:ss}] [%-5level] --%mdc{client} %msg%n</Pattern> </encoder> </appender> <appender name="TRACE_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <encoding>UTF-8</encoding> <file>${LOG_HOME}/trace.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <FileNamePattern>logs/trace.%d{yyyy-MM-dd}.log</FileNamePattern> </rollingPolicy> <layout> <pattern>[%date{yyyy-MM-dd HH:mm:ss}] [%-5level] [%logger:%line] --%mdc{client} %msg%n</pattern> </layout> <!-- <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>TRACE</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> --> </appender> <appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <encoding>UTF-8</encoding> <file>${LOG_HOME}/info.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <FileNamePattern>logs/info.%d{yyyy-MM-dd}.log</FileNamePattern> </rollingPolicy> <layout> <pattern>[%date{yyyy-MM-dd HH:mm:ss}] [%-5level] [%logger:%line] --%mdc{client} %msg%n</pattern> </layout> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>INFO</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> </appender> <appender name="DEBUG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <encoding>UTF-8</encoding> <file>${LOG_HOME}/debug.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <FileNamePattern>logs/debug.%d{yyyy-MM-dd}.log</FileNamePattern> </rollingPolicy> <layout> <pattern>[%date{yyyy-MM-dd HH:mm:ss}] [%-5level] [%logger:%line] --%mdc{client} %msg%n</pattern> </layout> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>DEBUG</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> </appender> <appender name="WARN_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <encoding>UTF-8</encoding> <file>${LOG_HOME}/warn.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <FileNamePattern>logs/warn.%d{yyyy-MM-dd}.log</FileNamePattern> </rollingPolicy> <layout> <pattern>[%date{yyyy-MM-dd HH:mm:ss}] [%-5level] [%logger:%line] --%mdc{client} %msg%n</pattern> </layout> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>WARN</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> </appender> <appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <encoding>UTF-8</encoding> <file>${LOG_HOME}/error.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <FileNamePattern>logs/error.%d{yyyy-MM-dd}.log</FileNamePattern> </rollingPolicy> <layout> <pattern>[%date{yyyy-MM-dd HH:mm:ss}] [%-5level] [%logger:%line] --%mdc{client} %msg%n</pattern> </layout> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>ERROR</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> </appender> <!-- <logger name="com.mchange" level="INFO"/> <logger name="org.springframework" level="WARN"/> <logger name="java.sql.Connection" level="DEBUG"/> <logger name="java.sql.ResultSet" level="INFO"/> <logger name="com.boaotech.util" level="DEBUG"/> --> <root level="TRACE"> <appender-ref ref="STDOUT"/> <appender-ref ref="TRACE_FILE" /> <appender-ref ref="INFO_FILE" /> <appender-ref ref="DEBUG_FILE" /> <appender-ref ref="WARN_FILE" /> <appender-ref ref="ERROR_FILE" /> </root> </configuration>
6. 将logback.xml 放置到web工程的classes目路下,运行项目,看你的桌面,是否有如下日志文件:
分别记录上面trace, info, debug, warn, error 的日志。
日志需要系统的学习一下,你到官网学习!
Logback 将日志分级别打印