android自带的log4j2,Log4j2的使用基本配置

使用log4j2需要两个包:apache官网上有的下:我使用的是log4j-api-2.5.jar和log4j-core-2.5.jar。

读取配置文件方法:System.setProperty("log4j.configurationFile", “文件存放路径”);

配置文件读取:

//java加载src下的配置文件

//src下的配置文件会默认的被log4j的框架加载(文件名必须log4j2.xml)。

//绝对路径配置文件

//方法1  使用  public ConfigurationSource(InputStream stream) throws IOException造

ConfigurationSource source= newConfigurationSource(newFileInputStream("D:\\log4j2.xml"));

//方法2 使用 public ConfigurationSource(InputStream stream, File file)构造函数

File config=newFile("D:\\log4j2.xml");

source= newConfigurationSource(newFileInputStream(config),config);

//方法3 使用 public ConfigurationSource(InputStream stream, URL url) 构造函数

String path="D:\\log4j2.xml";

source = newConfigurationSource(newFileInputStream(path),newFile(path).toURL());

//source.setFile(new File("D:\log4j2.xml"));

//source.setInputStream(new FileInputStream("D:\log4j2.xml"));

Configurator.initialize(null, source);

//相对路径的配置文件加载

//这里需要注意路径中不要出现中文和空格,如果存在中文,请使用url转码

//方法1  使用getResource()

String path="/com/herman/config/log4j2.xml";

URL url=ConfigTest.class.getResource(path);

ConfigurationSource source;= newConfigurationSource(newFileInputStream(newFile(url.getPath())),url);

Configurator.initialize(null, source);

/方法2 使用System.getProperty

String config=System.getProperty("user.dir");

source = newConfigurationSource(newFileInputStream(config+"\\src\\com\\herman\\config\\log4j2.xml"));

Configurator.initialize(null, source);

接下来看一下配置文件:

%d{yyyy-MM-dd HH:mm:ss} %-5level - %msg%xEx%n

./logs

smart_home

20MB

debug

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值