log4j 1.2到log4j 2的迁移不起作用

kq0g1dla  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(210)

我尝试将log4j从1迁移到2,但未生成日志。我删除了写入的实际路径,应用程序能够写入所述路径的日志,因为升级后日志未打印,仅旧版代码包含父Pom

<!-- 
    <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.16.0</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.16.0</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.16.0</version>
</dependency>

子POM:-仅更新版本

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <!-- <version>1.7.5</version> -->
    <version>1.7.25</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-ext</artifactId>
    <!--  <version>1.7.5</version> -->
    <version>1.7.25</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-jcl</artifactId>
    <!-- <version>1.7.5</version> -->
    <version>1.7.25</version>
</dependency>

<!-- 
<dependency> 
    <groupId>org.slf4j</groupId> 
    <artifactId>slf4j-log4j12</artifactId> 
    <version>1.7.5</version> 
    </dependency> 
-->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>2.17.1</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-nop</artifactId>
    <!-- <version>1.7.5</version> -->
    <version>1.7.25</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <!-- <version>1.7.5</version> -->
    <version>1.7.25</version>
</dependency>

log4j -〉xml文件

<log4j:configuration>
    <appender name="stdout" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="[%d{ISO8601}] [%t] %-5p %c{1}: %x - %m%n" />
        </layout>
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
            <param name="LevelMin" value="DEBUG" />
            <param name="LevelMax" value="INFO" />
            <param name="AcceptOnMatch" value="true" />
        </filter>
    </appender>

    <appender name="errout" class="org.apache.log4j.ConsoleAppender">
        <param name="Target" value="System.Err" />
        <param name="Threshold" value="WARN" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="[%d{ISO8601}] [%t] %-5p %c{1}: %x - %m%n" />
        </layout>
    </appender>

    <appender name="userLog" class="com.test.RollingFileAppender">
        <param name="File" value="userlogs.log" />
        <param name="Threshold" value="INFO" />
        <param name="MaxFileSize" value="50MB" />
        <param name="MaxBackupIndex" value="5" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%m | [%d{ISO8601}] | [%t]%n" />
        </layout>
    </appender>

    <appender name="errorLog" class="com.test.RollingFileAppender">
        <param name="File" value="error.log" />
        <param name="Threshold" value="ERROR" />
        <param name="MaxFileSize" value="50MB" />
        <param name="MaxBackupIndex" value="10" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="[%d{ISO8601}] [%t] %-5p %c{1}: - %m%n" />
        </layout>
    </appender>

    <category name="REQUEST_LOG" additivity="false">
        <priority value="INFO" />
        <appender-ref ref="userLog" />
    </category>

    <root>
        <priority value="INFO" />
        <appender-ref ref="errorLog" />
    </root>

</log4j:configuration>

我们以编程方式初始化了log4j

import java.io.File;
import java.net.URL;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.xml.DOMConfigurator;

import ApplicationContext;
import ResourceUtility;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.LogManager;

public class Initializer implements Startable {

    private static Log logger = null;
    private static boolean logsPathSet = false;

    private static String logsPath = "";

    static {
        BasicConfigurator.configure();
        logger = LogFactory.getLog(Initializer.class);
    }

    public static void initialize() {
        logsPath = System.getProperty("log4j.configuration","");
        logsPathSet = (logsPath != null && !logsPath.equals(""));
        initializeLogging();
        ApplicationContext.getInstance();
    }

    public static void initializeLogging() {
        String configFile = "";
        URL aURL  = null;

        if ( logsPathSet ) {
            configFile  = logsPath;
            aURL = ResourceUtility.geUrlResource(configFile);
        }

        if ( (!logsPathSet) || ( logsPathSet && aURL == null) ) {
            configFile = ApplicationContext.getInstance().getStringProperty("logging", "log4j", "configFile");
            aURL = ResourceUtility.geUrlResource(configFile);
        }
        DOMConfigurator.configure(aURL);
        logger = LogFactory.getLog(Initializer.class);
        logger.info("Configured Logging from: " + aURL);
    }
}

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.PropertyConfigurator;

import ApplicationContext;
import Initializer;
import ResourceUtility;

    public class Startup extends HttpServlet {

        private static Log logger = LogFactory.getFactory().getInstance(Startup.class);
        public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {}
        public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {}

        public void init() throws ServletException {
            super.init();
            ApplicationContext.getInstance();
            Initializer.initialize();
            String loggingProperty = "/" + ApplicationContext.getInstance().getProperty("logging", "log4j", "configFile");
            loggingFile = ResourceUtility.getFileResource(loggingProperty);
            PropertyConfigurator.configure(loggingFile.toString());
        }

    }

我尝试了两种方法。方法1 -〉直接将log4j 1迁移到2,并更新log4j connfig
系统输出-〉SYSTEM_OUT -〉已尝试使用两个目标
更新了log4j xml文件

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <Appenders>
        <Console name="console" target="System.out">
            <PatternLayout pattern="[%d{ISO8601}] [%t] %-5p %c{1}: %x - %m%n" />
            <LevelRangeFilter minLevel="DEBUG" maxLevel="INFO" onMatch="ACCEPT" onMismatch="NEUTRAL" />
        </Console>
        <Console name="consoleError" target="System.Err" follow="true">
            <PatternLayout pattern="[%d{ISO8601}] [%t] %-5p %c{1}: %x - %m%n" />
            <ThresholdFilter level="WARN" onMatch="ACCEPT" onMismatch="NEUTRAL" />
        </Console>

        <RollingFile name="userLog" fileName="/userlogs.log"  filePattern="/userlogs.log.%d{yyyyMMdd}">
            <PatternLayout pattern="%m | [%d{ISO8601}] | [%t]%n" />
            <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY" />
        </RollingFile>

        <RollingFile name="errorLog" fileName="/error.log"  filePattern="/error.log.%d{yyyyMMdd}">
            <PatternLayout pattern="[%d{ISO8601}] [%t] %-5p %c{1}: - %m%n" />
            <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY" />
        </RollingFile>
    </Appenders>

    <Loggers>
        <Logger name="REQUEST_LOG" additivity="false">
            <AppenderRef  ref="userRequestLog" level="INFO" />
        </Logger>
        <Root  level="INFO">
            <AppenderRef  ref="errorLog"/>
        </Root >
    </Loggers>
</Configuration>

已更新初始化程序类

//BasicConfigurator.configure();
Configurator.initialize(new DefaultConfiguration());

// DOMConfigurator.configure(aURL);

LoggerContext context = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
loggingFile = ResourceUtility.getFileResource(configFile);
context.setConfigLocation(loggingFile.toURI());
context.reconfigure();

ConfigurationFactory.setConfigurationFactory(new XmlConfigurationFactory());
Configurator.reconfigure(loggingFile.toURI());

将启动更新为

// PropertyConfigurator.configure(loggingFile.toString());
        ConfigurationFactory.setConfigurationFactory(new XmlConfigurationFactory());
        LoggerContext context = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
        //Configurator.reconfigure(loggingFile.toURI());
        context.setConfigLocation(loggingFile.toURI());
        context.reconfigure();

上述方法不会生成日志
方法2 -〉通过添加桥jar进行相同的更改-〉跳过log4j xml更改
提前感谢您提供的任何建议、更改或方法,或者请您分享您在迁移相同内容时遵循的步骤,或者分享代码更改最少的方法。

wribegjk

wribegjk1#

我建议采用第二种方法,但是你必须使用log4j 2.17.2+版本。桥jar在以前的版本中有很大的问题,但是开发人员在今年年初做出了努力来修复它。我本想把它作为一个评论而不是一个答案,但是声誉很低。
在我的例子中,可以创建一个简单的jar文件,并在200个正在运行的应用程序上用它覆盖现有的log4j版本1 jar,而不需要更改应用程序或应用服务器上的任何配置文件。

相关问题