1. 本文转自:http://www.2cto.com/kf/201302/191149.html
  2.  
  3. 一共两个java文件,第一个是例子,第二个是配置文件加载类;
  4.  
  5. LogbackTest.java
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package logbacktest;
  6.  
  7. import ch.qos.logback.core.joran.spi.JoranException;
  8. import java.io.IOException;
  9. import org.slf4j.LoggerFactory;
  10.  
  11. /**
  12. *
  13. * @author Administrator
  14. */
  15. public class LogbackTest {
  16. /**
  17. * @param args the command line arguments
  18. */
  19. public static void main(String[] args) throws IOException, JoranException {
  20. LogBackConfigLoader.load("logback-log.xml");
  21. org.slf4j.Logger logger = LoggerFactory.getLogger("snail");
  22. logger.debug("Hello");
  23. }
  24. }
 
LogBackConfigLoader.java
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package logbacktest;
  6. import java.io.File;
  7. import java.io.IOException;
  8.  
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11.  
  12. import ch.qos.logback.classic.LoggerContext;
  13. import ch.qos.logback.classic.joran.JoranConfigurator;
  14. import ch.qos.logback.core.joran.spi.JoranException;
  15. import ch.qos.logback.core.util.StatusPrinter;
  16.  
  17. /**
  18. * Simple Utility class for loading an external config file for logback
  19. * @author daniel
  20. */
  21. public class LogBackConfigLoader {
  22.  
  23. public static void load (String externalConfigFileLocation) throws IOException, JoranException{
  24. LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
  25.  
  26. File externalConfigFile = new File(externalConfigFileLocation);
  27. if(!externalConfigFile.exists()){
  28. throw new IOException("Logback External Config File Parameter does not reference a file that exists");
  29. }else{
  30. if(!externalConfigFile.isFile()){
  31. throw new IOException("Logback External Config File Parameter exists, but does not reference a file");
  32. }else{
  33. if(!externalConfigFile.canRead()){
  34. throw new IOException("Logback External Config File exists and is a file, but cannot be read.");
  35. }else{
  36. JoranConfigurator configurator = new JoranConfigurator();
  37. configurator.setContext(lc);
  38. lc.reset();
  39. configurator.doConfigure(externalConfigFileLocation);
  40. StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
  41. }
  42. }
  43. }
  44. }
  45.  
  46. }
 
附上一个简单的logback-log.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration>
  3. <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
  4. <!-- encoder 默认配置为PatternLayoutEncoder -->
  5. <encoder>
  6. <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss} %msg%n</pattern>
  7. </encoder>
  8. </appender> www.2cto.com
  9. <appender name="debug" class="ch.qos.logback.core.FileAppender">
  10. <File>log/debug.log</File>
  11. <Append>true</Append>
  12. <encoder>
  13. <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss} %msg%n</pattern>
  14. </encoder>
  15. <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
  16. <level>TRACE</level>
  17. </filter>
  18. </appender>
  19. <logger name="snail" level="TRACE" additivity="false">
  20. <appender-ref ref="debug"/>
  21. </logger>
  22. </configuration>

日志处理(三) logback 手动加载(转)的更多相关文章

  1. 使用angular.bootstrap() 完成模块的手动加载

    之前我们看到使用ng-app指令,可以实现模块的自动加载.现在我们看下,angular中如何手动加载模块.需要使用到angular.bootstrap这个函数. <html> <he ...

  2. AngularJS中多个ng-app(手动加载模块)

    1.当有多个ng-app时:(首先是要加载angularJS) <div ng-app=""> <p>姓名:<input type="tex ...

  3. angularjs 手动加载

    利用ng-app可以完成自动加载,如果不利用ng-app.那么使用bootstrarp实现手动加载模块 <html> <head> <script src="a ...

  4. TP中手动加载类库

    加载第三方类库,包括不符合命名规范和后缀的类库,以及没有使用 命名空间或者空间和路径不一致的类库.可手动加载. // 导入Org类库包 Library/Org/Util/Date.class.php类 ...

  5. AngularJS之手动加载模块app和controller

    使用ng的页面中一般都是使用模块自动加载,页面的结构一般是这样的 加载angularjs脚本 加载业务代码脚本(或者写在script标签中) html结构代码(带有ng指令) 就像这样 app.htm ...

  6. mybatis(三)懒加载

    懒加载的好处: 所谓懒加载(lazy)就是延时加载,延迟加载.什么时候用懒加载呢,我只能回答要用懒加载的时候就用懒加载.至于为什么要用懒加载呢,就是当我们要访问的数据量过大时,明显用缓存不太合适,因为 ...

  7. java基础-jdbc——三种方式加载驱动建立连接

    String url = "jdbc:mysql://localhost:3306/student?Unicode=true&characterEncoding=utf-8" ...

  8. [Q]手动加载菜单方法

    一般情况下,安装程序会自动安装依云软件菜单,但可能由于某些原因未能自动安装的话,您可以手动加载菜单,步骤如下: 在AoutCAD命令行输入"CUILOAD",会弹出"加载 ...

  9. EF的三种数据加载方式

    EF的关联实体加载有三种方式:Lazy Loading,Eager Loading,Explicit Loading,其中Lazy Loading和Explicit Loading都是延迟加载. (一 ...

随机推荐

  1. [UIImage _isCached]: message sent to deallocated instance

    本文转载至 http://zhuhaibobb.blog.163.com/blog/static/2744006720124191633375/       这几天做了个APP打开20份钟左右就强制退 ...

  2. 设置eclipse中python脚本的编码格式

    今天在运行python脚本时报如下错误: SyntaxError: Non-ASCII character '\xe5' in file D:\pythonlearn1\src\day01\direc ...

  3. 工作表(Worksheet)基本操作应用示例

    在编写代码时,经常要引用工作表的名字.知道工作表在工作簿中的位置.增加工作表.删除工作表.复制工作表.移动工作表.重命名工作表,等等.下面介绍与此有关及相关的一些属性和方法示例. [示例04-01]增 ...

  4. LeetCode-Combination Sum IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  5. LeetCode 笔记系列二 Container With Most Water

    题目:Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai ...

  6. 160415、sql语句sort排序,sort为空的在后面

    按sort排序,sort为空的在后面 select * from 表名 order by (case when sort is null or sort='' then 1 else 0 end),s ...

  7. sql语句中left join、right join 以及inner join之间的使用与区别

    sql语句中left join.right join 以及innerjoin之间的使用与区别 left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录  right join( ...

  8. ZOJ 3715 Kindergarten Election

    At the beginning of the semester in kindergarten, the n little kids (indexed from 1 to n, for conven ...

  9. 分享:宽恕的艺术 Forgive

    宽恕的艺术 To forgive may be divine, but no one ever said it was easy. 宽恕是神圣的,但是没有人说很容易做到宽恕别人. When someo ...

  10. The Personal Touch Client Identification 个性化接触 客户识别

    w服务器要知道和谁在交谈. HTTP The Definitive Guide Web servers may talk to thousands of different clients simul ...