本文转自: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>

日志处理(三) 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. galera安装之编译安装xtrabackup 2.2.11

    ----1.编译安装percona-xtrabackup yum -y install cmake gcc gcc-c++ libaio libaio-devel automake autoconf ...

  2. Linux命令之乐--nmap

    Nmap是一款非常强大的实用工具,可用于:检测活在网络上的主机(主机发现)检测主机上开放的端口(端口发现或枚举)检测到相应的端口(服务发现)的软件和版本检测操作系统,硬件地址,以及软件版本检测脆弱性的 ...

  3. Codelf 搜索开源代码帮程序员命名

    "计算机科学里两件最难的事:缓存失效和命名." Codelf通过搜索在线开源平台Github, Bitbucket, Google Code, Codeplex, Sourcefo ...

  4. nginx的allow和deny配置

    转自:http://www.ttlsa.com/linux/nginx-modules-ngx_http_access_module/ 单看nginx模块名ngx_http_access_module ...

  5. angular4 checkbox复选框的全选,反选及个别选择

    <label><input type="checkbox" name="" [(ngModel)]="master"> ...

  6. angular 路由

    在路由时传递数据 1. 在查询参数中传递数据 /product?id=1&name=2 => ActivatedRoute.queryParams[id] 2.在路由路径中传递数据 {p ...

  7. Vue基础-匿名插槽与作用域插槽的合并和覆盖行为

    Vue 测试版本:Vue.js v2.5.13 Vue 文档: <slot> 元素可以用一个特殊的特性 name 来进一步配置如何分发内容.多个插槽可以有不同的名字.具名插槽将匹配内容片段 ...

  8. JS内存泄漏排查方法-Chrome Profiles

    原文链接:http://caibaojian.com/chrome-profiles.html 一.概述 Google Chrome浏览器提供了非常强大的JS调试工具,Heap Profiling便是 ...

  9. 170331、58到家MQ如何快速实现流量削峰填谷

    问:为什么会有本文? 答:上一篇文章<到底什么时候该使用MQ?>引起了广泛的讨论,有朋友回复说,MQ的还有一个典型应用场景是缓冲流量,削峰填谷,本文将简单介绍下,MQ要实现什么细节,才能缓 ...

  10. Code Forces 26C Dijkstra?

    C. Dijkstra? time limit per test 1 second memory limit per test 64 megabytes input standard input ou ...