日志处理(三) 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");
}
}
/*
* 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);
}
}
}
} }
<?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 手动加载(转)的更多相关文章
- 使用angular.bootstrap() 完成模块的手动加载
之前我们看到使用ng-app指令,可以实现模块的自动加载.现在我们看下,angular中如何手动加载模块.需要使用到angular.bootstrap这个函数. <html> <he ...
- AngularJS中多个ng-app(手动加载模块)
1.当有多个ng-app时:(首先是要加载angularJS) <div ng-app=""> <p>姓名:<input type="tex ...
- angularjs 手动加载
利用ng-app可以完成自动加载,如果不利用ng-app.那么使用bootstrarp实现手动加载模块 <html> <head> <script src="a ...
- TP中手动加载类库
加载第三方类库,包括不符合命名规范和后缀的类库,以及没有使用 命名空间或者空间和路径不一致的类库.可手动加载. // 导入Org类库包 Library/Org/Util/Date.class.php类 ...
- AngularJS之手动加载模块app和controller
使用ng的页面中一般都是使用模块自动加载,页面的结构一般是这样的 加载angularjs脚本 加载业务代码脚本(或者写在script标签中) html结构代码(带有ng指令) 就像这样 app.htm ...
- mybatis(三)懒加载
懒加载的好处: 所谓懒加载(lazy)就是延时加载,延迟加载.什么时候用懒加载呢,我只能回答要用懒加载的时候就用懒加载.至于为什么要用懒加载呢,就是当我们要访问的数据量过大时,明显用缓存不太合适,因为 ...
- java基础-jdbc——三种方式加载驱动建立连接
String url = "jdbc:mysql://localhost:3306/student?Unicode=true&characterEncoding=utf-8" ...
- [Q]手动加载菜单方法
一般情况下,安装程序会自动安装依云软件菜单,但可能由于某些原因未能自动安装的话,您可以手动加载菜单,步骤如下: 在AoutCAD命令行输入"CUILOAD",会弹出"加载 ...
- EF的三种数据加载方式
EF的关联实体加载有三种方式:Lazy Loading,Eager Loading,Explicit Loading,其中Lazy Loading和Explicit Loading都是延迟加载. (一 ...
随机推荐
- js截取最后一个斜杠之后的内容
var str = "/asdasf/asfaewf/agaegr/trer/rhh"; var index = str .lastIndexOf("\/"); ...
- Mybatis整理系列(01)————传入参数方式以及#{}与${}的区别
一.在MyBatis的select.insert.update.delete这些元素中都提到了parameterType这个属性.MyBatis现在可以使用的parameterType有基本数据类型和 ...
- spring容器加载完毕做一件事情(利用ContextRefreshedEvent事件)
关键字:spring容器加载完毕做一件事情(利用ContextRefreshedEvent事件) 应用场景:很多时候我们想要在某个类加载完毕时干某件事情,但是使用了spring管理对象,我们这个类引用 ...
- window 发布已编译好的ASP文件到IIS
1.进入window 7的控制面板,点击程序,选择程序和功能中的 打开或关闭Windows功能.安装IIS
- 170425、centos安装mysql5.6数据库
# rpm -qa | grep mysql ## 查看该操作系统上是否已经安装了 mysql 数据库, 有的话,可以通过 rpm -e 命令 或者 rpm -e --nodeps 命令来卸载掉 # ...
- Golang学习-第一篇 Golang的简单介绍及Windows环境下安装、部署
序言 这是本人博客园第一篇文章,写的不到位之处,希望各位看客们谅解. 本人一直从事.NET的开发工作,最近在学习Golang,所以想着之前学习的过程中都没怎么好好的将学习过程记录下来.深感惋惜! 现在 ...
- java面试基础题------》Java 中的父子类静态代码块,代码块,构造方法执行顺序
4.指出下面程序的运行结果. class A { static { System.out.print("1"); } public A() { System.out.print(& ...
- 2017 Multi-University Training Contest - Team 1—HDU6040
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6040 题意:不知道北航的同学为何解释题意之前都要想一段故事,导致刚开始题意不是很懂,题意就是给你n,m ...
- FW: linux screen -recorder by ffcast convert
fcast -s ffmpeg -r 20 -vcodec huffyuv out.avi 上面的命令会让你选择一个要录制的区域,然后呢,就会你就可以操作了,操作完后退回去按 q 键退出结束.如果你想 ...
- 【转】VMwareCLI命令参考
VMwareCLI命令参考 目录 基本命令范例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 2 ...