• 日志处理(三) logback 手动加载(转)


    本文转自:http://www.2cto.com/kf/201302/191149.html

    一共两个java文件,第一个是例子,第二个是配置文件加载类;

    LogbackTest.java
    /* 
     * To change this template, choose Tools | Templates 
     * and open the template in the editor. 
     */  
    package logbacktest;  
      
    import ch.qos.logback.core.joran.spi.JoranException;  
    import java.io.IOException;  
    import org.slf4j.LoggerFactory;  
      
    /** 
     * 
     * @author Administrator 
     */  
    public class LogbackTest {  
        /** 
         * @param args the command line arguments 
         */  
        public static void main(String[] args) throws IOException, JoranException {  
            LogBackConfigLoader.load("logback-log.xml");  
            org.slf4j.Logger logger = LoggerFactory.getLogger("snail");  
            logger.debug("Hello");  
        }  
    }  
     
    LogBackConfigLoader.java
    /* 
     * To change this template, choose Tools | Templates 
     * and open the template in the editor. 
     */  
    package logbacktest;  
    import java.io.File;  
    import java.io.IOException;  
       
    import org.slf4j.Logger;  
    import org.slf4j.LoggerFactory;  
       
    import ch.qos.logback.classic.LoggerContext;  
    import ch.qos.logback.classic.joran.JoranConfigurator;  
    import ch.qos.logback.core.joran.spi.JoranException;  
    import ch.qos.logback.core.util.StatusPrinter;  
       
    /** 
     * Simple Utility class for loading an external config file for logback 
     * @author daniel 
     */  
    public class LogBackConfigLoader {  
       
        public static void load (String externalConfigFileLocation) throws IOException, JoranException{  
            LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();  
              
            File externalConfigFile = new File(externalConfigFileLocation);  
            if(!externalConfigFile.exists()){  
                throw new IOException("Logback External Config File Parameter does not reference a file that exists");  
            }else{  
                if(!externalConfigFile.isFile()){  
                    throw new IOException("Logback External Config File Parameter exists, but does not reference a file");  
                }else{  
                    if(!externalConfigFile.canRead()){  
                        throw new IOException("Logback External Config File exists and is a file, but cannot be read.");  
                    }else{  
                        JoranConfigurator configurator = new JoranConfigurator();  
                        configurator.setContext(lc);  
                        lc.reset();  
                        configurator.doConfigure(externalConfigFileLocation);  
                        StatusPrinter.printInCaseOfErrorsOrWarnings(lc);  
                    }  
                }     
            }  
        }  
          
    }  
     
    附上一个简单的logback-log.xml
    <?xml version="1.0" encoding="UTF-8"?>  
    <configuration>  
        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">     
            <!-- encoder 默认配置为PatternLayoutEncoder -->     
            <encoder>     
                <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss}  %msg%n</pattern>     
            </encoder>     
        </appender>    www.2cto.com
        <appender name="debug" class="ch.qos.logback.core.FileAppender">  
            <File>log/debug.log</File>  
            <Append>true</Append>  
            <encoder>  
                <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss}  %msg%n</pattern>  
            </encoder>  
            <filter class="ch.qos.logback.classic.filter.ThresholdFilter">  
                <level>TRACE</level>  
            </filter>  
        </appender>  
        <logger name="snail" level="TRACE" additivity="false">   
            <appender-ref ref="debug"/>  
        </logger>  
    </configuration> 
  • 相关阅读:
    Unity3D-光照贴图技术
    登岳麓山
    第一个OC程序
    Unity3D之碰撞体,刚体
    TypeError: 'stepUp' called on an object that does not implement interface HTMLInputElement
    QQ互联登录回调路径错误redirect uri is illegal(100010)
    Quartz.Net使用
    C# 文件相关操作
    微信扫码支付模式一和模式二的区别
    ankhSVN安装后,VS2010使用
  • 原文地址:https://www.cnblogs.com/Jtianlin/p/4517836.html
Copyright © 2020-2023  润新知