Log4j 2.0读取配置文件的方法
log4j中配置日志文件存放的位置不一定在src下面,即根目录下。这个时候我们需要解决如何加载配置文件的问题。在log4j1.x中解决的方法就比较多了。如:PropertyConfigurator.configure();和DOMConfigurator.configure ();这两种方法读取。而在log4j2.x当中,这两个类都已经不存在了。这个时候我们该如何去加载配置文件呢?
答案也很简单,就是log4j2.x的版本给我提供了ConfigurationSource和Configurator这两个类。我们可以使用它们进行手动的加载任意位置的配置文件信息。
我就主要介绍三种方法:log4j 2读取配置文件的三种方法。
log4j 2读取的配置文件可以分为三类:src下的配置文件、绝对路径的配置文件、相对路径的配置文件。我们一一给例子。直接看代码:
package com.herman.test; import java.io.File;
import java.io.FileInputStream;
import java.net.URL; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.ConfigurationSource;
import org.apache.logging.log4j.core.config.Configurator; public class ConfigTest { private static Logger logger = LogManager.getLogger(ConfigTest.class);
/**
* log4j 2读取配置文件
* log4j 2读取的配置文件可以分为三类:src下的配置文件、绝对路径的配置文件、相对路径的配置文件
*/ //第一类 加载src下的配置文件
public static void test0(){
//src下的配置文件会默认的被log4j的框架加载,我们就不显示的加载了
//直接测试
logger.info("我打印了.......");
//输出内容
//2014-09-01 15:49:30,229 INFO [main] test.ConfigTest (ConfigTest.java:18) - 我打印了.......
} //第二类 绝对路径的配置文件
public static void test1(){
//我们将log4j2.xml放在D盘下
//这是需要手动的加载
//绝对路径配置文件
ConfigurationSource source;
try {
//方法1 使用 public ConfigurationSource(InputStream stream) throws IOException 构造函数
source = new ConfigurationSource(new FileInputStream("D:\\log4j2.xml")); //方法2 使用 public ConfigurationSource(InputStream stream, File file)构造函数
File config=new File("D:\\log4j2.xml");
source = new ConfigurationSource(new FileInputStream(config),config); //方法3 使用 public ConfigurationSource(InputStream stream, URL url) 构造函数
String path="D:\\log4j2.xml";
source = new ConfigurationSource(new FileInputStream(path),new File(path).toURL()); //source.setFile(new File("D:\log4j2.xml"));
//source.setInputStream(new FileInputStream("D:\log4j2.xml"));
Configurator.initialize(null, source);
Logger logger = LogManager.getLogger(ConfigTest.class.getName());
logger.trace("trace...");
logger.debug("debug...");
logger.info("info...");
logger.warn("warn...");
logger.error("error...");
logger.fatal("fatal...");
//一下是运行效果
/*2014-09-01 16:03:07,331 DEBUG [main] test.ConfigTest (ConfigTest.java:42) - debug...
2014-09-01 16:03:07,331 INFO [main] test.ConfigTest (ConfigTest.java:43) - info...
2014-09-01 16:03:07,331 WARN [main] test.ConfigTest (ConfigTest.java:44) - warn...
2014-09-01 16:03:07,331 ERROR [main] test.ConfigTest (ConfigTest.java:45) - error...
2014-09-01 16:03:07,331 FATAL [main] test.ConfigTest (ConfigTest.java:46) - fatal...*/
} catch (Exception e) {
e.printStackTrace();
}
} //第三类 相对路径的配置文件加载
public static void test2(){
//这里需要注意路径中不要出现中文和空格,如果存在中文,请使用url转码
ConfigurationSource source;
try {
//方法1 使用getResource()
String path="/com/herman/config/log4j2.xml";
URL url=ConfigTest.class.getResource(path);
source = new ConfigurationSource(new FileInputStream(new File(url.getPath())),url);
Configurator.initialize(null, source); //方法2 使用System.getProperty
String config=System.getProperty("user.dir");
source = new ConfigurationSource(new FileInputStream(config+"\\src\\com\\herman\\config\\log4j2.xml"));
Configurator.initialize(null, source); //输出内容
/*2014-09-01 16:32:19,746 DEBUG [main] test.ConfigTest (ConfigTest.java:53) - debug...
2014-09-01 16:32:19,746 INFO [main] test.ConfigTest (ConfigTest.java:54) - info...
2014-09-01 16:32:19,746 WARN [main] test.ConfigTest (ConfigTest.java:55) - warn...
2014-09-01 16:32:19,746 ERROR [main] test.ConfigTest (ConfigTest.java:56) - error...
2014-09-01 16:32:19,746 FATAL [main] test.ConfigTest (ConfigTest.java:57) - fatal...*/
} catch (Exception e) {
e.printStackTrace();
}
} public static void main(String[] args) {
//test0();
//test1();
test2();
}
}
Log4j 2.0读取配置文件的方法的更多相关文章
- Shell读取配置文件的方法
参考:http://www.cnblogs.com/binbinjx/p/5680214.html 做批量软件安装自动化时,都喜欢用配置文件的方式改变参数,那怎么通过shell读取配置文件的配置呢?参 ...
- .net core 2.0 读取配置文件
1.引用Microsoft.Extensions.Configuration2.在Startup中注入服务 public static IConfiguration Configuration { g ...
- 面试突击75:SpringBoot 有几种读取配置文件的方法?
Spring Boot 中读取配置文件有以下 5 种方法: 使用 @Value 读取配置文件. 使用 @ConfigurationProperties 读取配置文件. 使用 Environment 读 ...
- java中读取配置文件的方法
转自:http://blog.csdn.net/stypace/article/details/38414871 一.使用org.apache.commons.configuration 需要使用的是 ...
- shell 读取配置文件的方法
原文地址:http://bbs.chinaunix.net/thread-3628456-1-1.html 总结地址:https://www.cnblogs.com/binbinjx/p/568021 ...
- Spring ClassPathXmlApplicationContext和FileSystemXmlApplicationContext读取配置文件的方法
先说:ClassPathXmlApplicationContext 这个类,默认获取的是WEB-INF/classes/下的路径,也就是在myeclipse的src下的路径,所以用这个是获取不到WEB ...
- ASP .NET CORE 读取配置文件的方法
老式的config文件在ASP.net core2.0中变成了appsettings.json,如果想要读取自定义配置,可以写如下代码 { "Logging": { "I ...
- java读取配置文件的方法
1. Preferences类 这个主要是设置个人喜好.它的数据一般存在系统目录或是用户目录.还可以操作注册表. 2. Properties类 保存键值对.可以指定路径. 3. commons con ...
- java读取配置文件的几种方法
java读取配置文件的几种方法 原文地址:http://hbcui1984.iteye.com/blog/56496 在现实工作中,我们常常需要保存一些系统配置信息,大家一般都会选择配 ...
随机推荐
- Android-xx倍图
图片文件夹对应倍图关系: ldpi 0.75 倍图 mdpi 1.0 倍图 hdpi 1.5 倍图 xhdpi 2.0 倍图 xxhdpi 3.0 倍图 xxxhdpi 4.0 ...
- centos 安装erlang rpm包互相依赖问题
在项目中使用 centos 6.5 mini 版本(网络隔离,无法上外网),因测试需要使用到 erlang 环境. erlang rpm 包下载地址:https://www.erlang-soluti ...
- GRPC .netcore
GRPC是Google发布的一个开源.高性能.通用RPC(Remote Procedure Call)框架.提供跨语言.跨平台支持.以下以一个.NET Core Console项目演示如何使用GRPC ...
- 基于GOJS绘制流程图
基于GOJS封装的流程图设计(展示)工具类,主要分为两个工具类: 工具库依赖于go.js.jquery以及layer.js http://gojs.net/ http://jquery.com/ ht ...
- 【QTP-场景恢复】Post-Recovery Test Run Options Screen
Post-Recovery Test Run Options Screen When you clear the Add another recovery operation check box in ...
- A. The Meaningless Game(数学)
A. The Meaningless Game time limit per test:1 second memory limit per test:256 megabytes input:stand ...
- 3.iptables 扩展模块
--tcp-flags 用于匹配报文的tcp头的标志位 iptables -t filter -I INPUT -p tcp -m tcp --dport 22 --tcp-flags SYN,AC ...
- “全栈2019”Java第六十三章:接口与抽象方法详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- kali linux之拒绝服务攻击工具
hping3 几乎可以定制发送任何TCP/IP数据包,用于测试FW,端口扫描,性能测试 -c - 计数包计数 -i - interval wait(uX表示X微秒,例如-i u1000) ...
- acedSSGet使用自定义提示字符:$模式
ads_name ss; struct resbuf *pRbList=NULL; pRbList=acutBuildList(RTDXF0,_T("lwpolyline,ins ...