log4j2中的属性配置器

az31mfrm  于 2022-11-06  发布在  其他
关注(0)|答案(7)|浏览(199)

我正在将log4j 1.2.8迁移到log4j 2.3。一切都很好,除此之外,我没有找到任何PropertyConfigurator的替代品。
是否有另一个类来处理PropertyConfigurator以前所做的事情?

eiee3dmh

eiee3dmh1#

也许这个能帮到你?

  • 如何使用特定的配置文件在代码中重新配置log4j2?请参阅下面的示例。请注意,此LoggerContext类不是公共API的一部分,因此您的代码可能会在任何次要版本中中断。*
// import org.apache.logging.log4j.core.LoggerContext;

LoggerContext context = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
File file = new File("path/to/a/different/log4j2.xml");

// this will force a reconfiguration
context.setConfigLocation(file.toURI());**
nhaq1z21

nhaq1z212#

我的解决方案只是简单地按照Log4J站点https://logging.apache.org/log4j/2.x/manual/migration.html中的说明进行操作,也就是说,我将

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-1.2-api</artifactId>
    <version>2.14.1</version>
 </dependency>

即使使用类org.apache.log4j.PropertyConfigurator,也没有编译错误。
这比尝试迁移到

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.14.1</version>
</dependency>

正如@Christian所指出的那样,它不能替代PropertyConfigurator类。不可否认,如果不迁移,我将无法从Log4j 2的新功能中获益。

ki0zmccv

ki0zmccv3#

Log4j 2目前支持XML、JSON或YAML的配置。虽然不久的将来可能也会支持属性文件,但语法肯定会与Log4j 1不同。

knsnq2tg

knsnq2tg4#

下面我对同一个问题的解决方案:

网页.xml

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <context-param>
        <param-name>isLog4jContextSelectorNamed</param-name>
        <param-value>false</param-value>
    </context-param>

    <!-- ... and so on -->

</web-app>

日志记录器帮助程序.java

import java.io.File;
    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.core.LoggerContext;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;

    public final class LoggerHelper {

      public static Logger INSTANCE;

      public static void initialize() {    
        String        fileName = "<PATH>/log4j.xml";
        LoggerContext context  = (LoggerContext)LogManager.getContext(false);

        context.setConfigLocation(new File(fileName).toURI());
        INSTANCE = LoggerFactory.getLogger(LoggerHelper.class);

        String logMessage = String.format("Initialized (%s).", fileName);
        System.out.println(logMessage);
        INSTANCE.info(logMessage);
      }

    }
tyg4sfes

tyg4sfes5#

If you want to change some properties in the log4j2.properties file and make it effect immediatly. Here is the method:

    private static void changeSomeProperties(String pathToPropertiesFile) {

    File log4j2PropertiesFile= new File(pathToPropertiesFile, "log4j2.properties");
    Properties properties = new Properties();
    try (FileInputStream fileInputStream = new FileInputStream(log4j2PropertiesFile);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream)) {
        properties.load(bufferedInputStream);
    } catch (Exception e) {
        //handle exception
    }

    properties.setProperty("property.logDirectory", "someLogPath");
    File updatedLog4j2PropertiesFile = new File(pathToPropertiesFile, "updatedlog4j2.properties");

    try (OutputStream output = new FileOutputStream(updatedLog4j2PropertiesFile)) {
        properties.store(output, null);
    } catch (IOException e) {
        //handle exception
    }

    LoggerContext loggerContext = LoggerContext.getContext(false);
loggerContext.setConfigLocation(updatedLog4j2PropertiesFile.toURI());
    loggerContext.reconfigure();
}
yhived7q

yhived7q6#

任何log4j 2代码前的第一行- configure sys prop到您的属性配置工厂
第一个
或扩展属性配置工厂

11dmarpk

11dmarpk7#

No need of PropertyConfigurator in log4j2. 
It will automatically read your log4j2.property file.

Just make sure you have below jar included in your pom.

            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-api</artifactId>
                <version>2.17.2</version>
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-core</artifactId>
                <version>2.17.2</version>
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-slf4j-impl</artifactId>
                <version>2.17.2</version>
            </dependency>
           <dependency>
               <groupId>org.slf4j</groupId>
               <artifactId>slf4j-api</artifactId>
               <version>1.7.36</version>
           </dependency>

    ----------------------------------------------------------------------
    log4j2.properties

    appenders=xyz
    appender.xyz.type = Console
    appender.xyz.name = myOutput
    appender.xyz.layout.type = PatternLayout
    appender.xyz.layout.pattern = [%d{yy-MMM-dd HH:mm:ss:S}] [%p] [%c{1}:%L] - %m%n
    rootLogger.level = info
    rootLogger.appenderRefs = abc
    rootLogger.appenderRef.abc.ref = myOutputstrong text

    ----------------------------------------------------------------------
    MyClass.java

        import org.slf4j.Logger;
        import org.slf4j.LoggerFactory;

        public class MyClass {

            public static void main(String[] args) {

                Logger logger = LoggerFactory.getLogger(MyClass.class);
                logger.info("----start-------");

            }
        }

    ----------------------------------------------------------------------

    Output:

        [22-Jul-22 17:11:09:128] [INFO] [MyClass:24] - ----start-------
  • 重新设定

如果您想使用特定的配置文件在代码中重新配置log4j2,请使用下面的代码。

// import org.apache.logging.log4j.core.LoggerContext;

LoggerContext context = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
File file = new File("path/to/a/different/log4j2.xml");

// this will force a reconfiguration
context.setConfigLocation(file.toURI());

相关问题