使用logback.xml配置来实现日志文件输出

Logback是由log4j创始人设计的又一个开源日志组件。logback当前分成三个模块:logback-core,logback- classic和logback-access。logback-core是其它两个模块的基础模块。logback-classic是log4j的一个 改良版本。此外logback-classic完整实现SLF4J API使你可以很方便地更换成其它日志系统如log4j或JDK14 Logging。logback-access访问模块与Servlet容器集成提供通过Http来访问日志的功能。

 

logback.xml配置如下:

 

  1. xml version="1.0" encoding="UTF-8"?>  
  2.   
  3.   
  4.   
  5.   
  6.   
  7.   
  8.   
  9.     <property name="log_dir" value="/logs/ev_cmdb" />  
  10.       
  11.     <property name="maxHistory" value="30"/>  
  12.   
  13.   
  14.   
  15.   
  16.       
  17.     <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">  
  18.           
  19.         <encoder>  
  20.             <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger -%msg%npattern>  
  21.         encoder>  
  22.     appender>  
  23.       
  24.       
  25.       
  26.       
  27.     <appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">  
  28.           
  29.         <filter class="ch.qos.logback.classic.filter.LevelFilter">  
  30.             <level>ERRORlevel>  
  31.             <onMatch>ACCEPTonMatch>  
  32.             <onMismatch>DENYonMismatch>  
  33.         filter>  
  34.           
  35.         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
  36.               
  37.             <fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/error-log.logfileNamePattern>  
  38.               
  39.     <appender name="WARN" class="ch.qos.logback.core.rolling.RollingFileAppender">  
  40.           
  41.         <filter class="ch.qos.logback.classic.filter.LevelFilter">  
  42.             <level>WARNlevel>  
  43.             <onMatch>ACCEPTonMatch>  
  44.             <onMismatch>DENYonMismatch>  
  45.         filter>  
  46.         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
  47.               
  48.             <fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/warn-log.log  
  49.             fileNamePattern>  
  50.               
  51.             <maxHistory>${maxHistory}maxHistory>  
  52.         rollingPolicy>  
  53.         <encoder>  
  54.             <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%npattern>  
  55.         encoder>  
  56.     appender>  
  57.       
  58.       
  59.       
  60.       
  61.       
  62.     <appender name="INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">  
  63.           
  64.         <filter class="ch.qos.logback.classic.filter.LevelFilter">  
  65.             <level>INFOlevel>  
  66.             <onMatch>ACCEPTonMatch>  
  67.             <onMismatch>DENYonMismatch>  
  68.         filter>  
  69.         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
  70.               
  71.             <fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/info-log.log  
  72.             fileNamePattern>  
  73.               
  74.             <maxHistory>${maxHistory}maxHistory>  
  75.         rollingPolicy>  
  76.         <encoder>  
  77.             <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%npattern>  
  78.         encoder>  
  79.     appender>  
  80.       
  81.       
  82.       
  83.       
  84.       
  85.     <appender name="DEBUG" class="ch.qos.logback.core.rolling.RollingFileAppender">  
  86.           
  87.         <filter class="ch.qos.logback.classic.filter.LevelFilter">  
  88.             <level>DEBUGlevel>  
  89.             <onMatch>ACCEPTonMatch>  
  90.             <onMismatch>DENYonMismatch>  
  91.         filter>  
  92.         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
  93.               
  94.             <fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/debug-log.log  
  95.             fileNamePattern>  
  96.               
  97.             <maxHistory>${maxHistory}maxHistory>  
  98.         rollingPolicy>  
  99.         <encoder>  
  100.             <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%npattern>  
  101.         encoder>  
  102.     appender>  
  103.       
  104.       
  105.       
  106.       
  107.       
  108.     <appender name="TRACE" class="ch.qos.logback.core.rolling.RollingFileAppender">  
  109.           
  110.         <filter class="ch.qos.logback.classic.filter.LevelFilter">  
  111.             <level>TRACElevel>  
  112.             <onMatch>ACCEPTonMatch>  
  113.             <onMismatch>DENYonMismatch>  
  114.         filter>  
  115.         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
  116.               
  117.             <fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/trace-log.log  
  118.             fileNamePattern>  
  119.               
  120.             <maxHistory>${maxHistory}maxHistory>  
  121.         rollingPolicy>  
  122.         <encoder>  
  123.             <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%npattern>  
  124.         encoder>  
  125.     appender>  
  126.   
  127.     <logger name="java.sql.PreparedStatement" value="DEBUG" />    
  128.     <logger name="java.sql.Connection" value="DEBUG" />    
  129.     <logger name="java.sql.Statement" value="DEBUG" />    
  130.     <logger name="com.ibatis" value="DEBUG" />    
  131.     <logger name="com.ibatis.common.jdbc.SimpleDataSource" value="DEBUG" />    
  132.     <logger name="com.ibatis.common.jdbc.ScriptRunner" level="DEBUG"/>    
  133.     <logger name="com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate" value="DEBUG" />    
  134.       
  135.       
  136.       
  137.       
  138.     <root level="debug">  
  139.           
  140.         <appender-ref ref="STDOUT" />  
  141.           
  142.         <appender-ref ref="ERROR" />  
  143.         <appender-ref ref="INFO" />  
  144.         <appender-ref ref="WARN" />  
  145.         <appender-ref ref="DEBUG" />  
  146.         <appender-ref ref="TRACE" />  
  147.     root>  
  148. configuration>  

 

 

LogUtils.java

 

  1. package log.base;  
  2.   
  3. package com.ghca.easyview.server.common.utils;  
  4.   
  5. import org.slf4j.Logger;  
  6. import org.slf4j.LoggerFactory;  
  7.   
  8. public class LogUtils {  
  9.     /** 
  10.      * 错误输入日志 
  11.      */  
  12.     public static final Logger log = LoggerFactory.getLogger(LogUtils.class);  
  13.   
  14.     /** 
  15.      * 记录一直 info信息 
  16.      *  
  17.      * @param message 
  18.      */  
  19.     public static void logInfo(String message) {  
  20.         StringBuilder s = new StringBuilder();  
  21.         s.append((message));  
  22.         log.info(s.toString());  
  23.     }  
  24.   
  25.     public static void logInfo(String message, Throwable e) {  
  26.         StringBuilder s = new StringBuilder();  
  27.         s.append(("exception : -->>"));  
  28.         s.append((message));  
  29.         log.info(s.toString(), e);  
  30.     }  
  31.   
  32.     public static void logWarn(String message) {  
  33.         StringBuilder s = new StringBuilder();  
  34.         s.append((message));  
  35.   
  36.         log.warn(s.toString());  
  37.     }  
  38.   
  39.     public static void logWarn(String message, Throwable e) {  
  40.         StringBuilder s = new StringBuilder();  
  41.         s.append(("exception : -->>"));  
  42.         s.append((message));  
  43.         log.warn(s.toString(), e);  
  44.     }  
  45.   
  46.     public static void logDebug(String message) {  
  47.         StringBuilder s = new StringBuilder();  
  48.         s.append((message));  
  49.         log.debug(s.toString());  
  50.     }  
  51.   
  52.     public static void logDebug(String message, Throwable e) {  
  53.         StringBuilder s = new StringBuilder();  
  54.         s.append(("exception : -->>"));  
  55.         s.append((message));  
  56.         log.debug(s.toString(), e);  
  57.     }  
  58.   
  59.     public static void logError(String message) {  
  60.         StringBuilder s = new StringBuilder();  
  61.         s.append(message);  
  62.         log.error(s.toString());  
  63.     }  
  64.   
  65.     /** 
  66.      * 记录日志错误信息 
  67.      *  
  68.      * @param message 
  69.      * @param e 
  70.      */  
  71.     public static void logError(String message, Throwable e) {  
  72.         StringBuilder s = new StringBuilder();  
  73.         s.append(("exception : -->>"));  
  74.         s.append((message));  
  75.         log.error(s.toString(), e);  
  76.     }  
  77. }  

 

TEST

  1. package com.ghca.easyview.server.log.test;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5.   
  6. import org.junit.Before;  
  7. import org.junit.Test;  
  8. import org.slf4j.Logger;  
  9. import org.slf4j.LoggerFactory;  
  10.   
  11. import ch.qos.logback.core.joran.spi.JoranException;  
  12.   
  13. import com.ghca.easyview.server.common.utils.LogBackConfigLoader;  
  14. import com.ghca.easyview.server.common.utils.LogUtils;  
  15. import com.ghca.easyview.server.config.ServerConfig;  
  16.   
  17. public class TestLog {  
  18.   
  19.     @Before  
  20.     public void before() {  
  21.         File file = new File(System.getProperty("user.dir"));  
  22.         String path = file.getPath() + File.separator + "conf";  
  23.         // 将主要配置文件路径赋值给 ServerConfig.configPath属性  
  24.         ServerConfig.config_path = path;  
  25.         try {  
  26.             // 加载日志文件  
  27.             LogBackConfigLoader.load(ServerConfig  
  28.                     .loadLogBackConfPath("logback.xml"));  
  29.         } catch (IOException | JoranException e) {  
  30.             e.printStackTrace();  
  31.         }  
  32.     }  
  33.   
  34.     @Test  
  35.     public void test1() {  
  36.         LogUtils.logInfo("INFO ~");  
  37.         LogUtils.logDebug("DEBUG ~");  
  38.         LogUtils.logError("ERROR~");  
  39.         LogUtils.logWarn("WARN ~");  
  40.     }  
  41. }  

 

 根据日志级别输出不等的日志信息,

你可能感兴趣的:(logback,logback)