公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

本文介绍了Log4j2-版本2.5-如何使用java.util.properties对象配置log4j2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用属性对象配置log4j2版本2.5。这样做的原因是从版本1.2.17迁移。我不能直接使用属性文件。我们对它做了一些程序化的修改。

以下是我尝试过的内容:

LogTest.java

public class LogTest {

   static {
          System.setProperty("log4j.configurationFactory",     "logsample.common.util.LogsampleConfigurationFactory");
   }

    private static Logger logger =      LogManager.getLogger(LogTest.class.getName());

    public static void main(String[] args) throws Exception
    {
        logger.debug("First log");
        logger.info("Infoed");
    }
}

跟踪属性

name = PropertiesConfig

property.filename = D:/rolling/rollingtest.log


appenders = file


appender.file.type = File
appender.file.name = LOGFile
appender.file.fileName = ${filename}
appender.file.layout.type = PatternLayout
appender.file.layout.pattern = %d %p %C{1.} [%t] %m%n


loggers = file

logger.file.name = logware.common.util
logger.file.level = debug
logger.file.appenderRefs = file
logger.file.appenderRef.file.ref = LOGFile

LogwareConfigurationFactory.java

public class LogsampleConfigurationFactory extends ConfigurationFactory {

    @Override
    protected String[] getSupportedTypes() {
        return new String[]{".properties", "*"};
    }

    @Override
    public Configuration getConfiguration(ConfigurationSource source) {


        return new PropertiesConfiguration(createConfigurationSource(), null);
    }



    @Override
    public Configuration getConfiguration(String name, URI configLocation) {
        return new PropertiesConfiguration(createConfigurationSource(), null);
    }

    private ConfigurationSource createConfigurationSource()
    {
        Properties p = new Properties();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        InputStream in = null;
        try {
            p.load(new  FileInputStream("D:/log4jSample/properties/trace.properties"));

            p.store(out, null);

        } catch (IOException e) {
            e.printStackTrace();
        }

        in = new ByteArrayInputStream(out.toByteArray());

        ConfigurationSource configSrc = null;
        try {
            configSrc = new ConfigurationSource(in);
        }
        catch (IOException i)
        {

        }
        return configSrc;
    }
}

当我运行LogTest类时,它无法获取指针为空的LogContext。

异常堆栈

java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException
at org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration.<init>(BuiltConfiguration.java:58)
at org.apache.logging.log4j.core.config.properties.PropertiesConfiguration.<init>(PropertiesConfiguration.java:36)
at logware.common.util.LogwareConfigurationFactory.getConfiguration(LogwareConfigurationFactory.java:46)
at org.apache.logging.log4j.core.config.ConfigurationFactory$Factory.getConfiguration(ConfigurationFactory.java:427)
at org.apache.logging.log4j.core.config.ConfigurationFactory.getConfiguration(ConfigurationFactory.java:256)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:561)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:578)
at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:214)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:235)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:41)
at org.apache.logging.log4j.LogManager.getContext(LogManager.java:167)
at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:522)
at logware.common.util.LogTest.<clinit>(LogTest.java:58)

因此,我没有将rootComponent放入PropertiesConfiguration构造函数中。但我不知道它应该是什么。
在这方面有任何指导或线索都是很好的。

推荐答案

对于2.5,我建议您这样做:

@Override
public Configuration getConfiguration(ConfigurationSource source) {
    PropertiesConfigurationFactory factory = new PropertiesConfigurationFactory();
    return factory.getConfiguration(source);
}

在最新版本中,您必须对其进行修改才能执行以下操作:

@Override
public Configuration getConfiguration(LoggerContext ctx, ConfigurationSource source) {
    PropertiesConfigurationFactory factory = new PropertiesConfigurationFactory();
    return factory.getConfiguration(ctx, source);
}

这篇关于Log4j2-版本2.5-如何使用java.util.properties对象配置log4j2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,

分享到:
标签:2.5 Log4j2 properties 如何使用 对象 版本 配置
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定