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 在现实工作中,我们常常需要保存一些系统配置信息,大家一般都会选择配 ...
随机推荐
- js中将时间字符串转换为时间戳
var time = "2017-4-18 09:18"; ; console.info(date);
- linux安装memcache及memcache扩展
一.安装libevent# wget http://www.monkey.org/~provos/libevent-2.0.12-stable.tar.gz# tar zxf libevent-2.0 ...
- 为web文件夹添加IIS应用程序池用户权限
在文件夹或文件右键属性—>安全——>编辑——>添加——>输入IIS APPPOOL\应用程序池名,确定即可将IIS 7或7.5.8的应用程序池虚拟用户添加到权限控制里面,然后再 ...
- django drf Filter
1.定义get_queryset()方法 from django.shortcuts import render from rest_framework.views import APIView fr ...
- Office 2019 官方镜像下载地址
http://officecdn.microsoft.com/pr/492350f6-3a01-4f97-b9c0-c7c6ddf67d60/media/zh-cn/ProPlus2019Retail ...
- 《Beginning Java 7》 - 5 - Hash Codes 哈希码
哈希码和 equals() 都是用来比较的. 1. 哈希码的作用是用来提高比较的效率.因为当比较的对象比较复杂时,equals() 可能很耗时,但哈希码只需要比较一个 int .哈希码常用于集 (se ...
- windbg 常用命令详解
= kd> ln 8046e100 (8046e100) nt!KeServiceDescriptorTableShadow | (8046e140) nt!MmSectionExtendRes ...
- 不建议使用*{margin:0; padding:0}?
是不建议用的,应该把具体的标签名都列出来,有时别人在写示例时为了方便会直接这么写; body{ magin:0;padding:0; }这种,就是清除浏览器有可能默认设置边距: 因为“在全局范围使用* ...
- linux密码修改实验
1.在单用户模式下进行引导 在不同的运行级别中,一个重要的运行级别就是单用户模式(运行级别1),该模式中,只有一个系统管理员使用特定的机器,而且尽可能少地运行系统服务,其中包含登录.单用户模式对少数管 ...
- 使用 webpack 搭建 React 项目
简评:相信很多开发者在入门 react 的时候都是使用 create-react-app 或 react-slingshot 这些脚手架来快速创建应用,当有特殊需求,需要修改 eject 出来的 we ...