I have to migrate from logj1.x to log4j2.x. For this, I have changed my below source code.
After I performed the below changes when I start the tomcat service I am getting the below error in the log file.
[Fatal Error] TEST_log4j.xml:3:22: The prefix "log4j" for element "log4j:configuration" is not bound.
Source Code changes From
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
To
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.config.Configurator;
From
private static Logger myLogger = Logger.getLogger(MyCustomClass.class);
To
private static Logger myLogger = LogManager.getLogger(MyCustomClass.class);
From
DOMConfigurator.configure("/home/user/TEST_log4j.xml");
To
Configurator.initialize(null, "/home/user/TEST_log4j.xml");
My /home/user/TEST_log4j.xml file configuration is given below
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration>
<appender name="ConsoleAppender" >
<layout >
<param name="ConversionPattern" value="%d{MM/dd HH:mm:ss} %-5p 0.30c %x - %m\n"/>
</layout>
</appender>
<appender name="Fileappender" >
<param name="file" value="/opt/tomcat/logs/MyCustomClassLogs.log"/>
<param name="MaxFileSize" value="5MB"/>
<param name="MaxBackupIndex" value="5"/>
<layout >
<param name="ConversionPattern" value="%d{MM/dd HH:mm:ss} %-5p 0.30c %x - %m\n"/>
</layout>
</appender>
<appender name="ResetClass" >
<param name="file" value="/opt/tomcat/logs/resetTrace.log"/>
<param name="MaxFileSize" value="1MB"/>
<param name="MaxBackupIndex" value="5"/>
<layout >
<param name="ConversionPattern" value="%d{MM/dd HH:mm:ss} %-5p 0.30c %x - %m\n"/>
</layout>
</appender>
<category name="com.demo.custom.filter.ResetClass" additivity="false">
<priority value="TRACE"/>
<appender-ref ref="ResetClass"/>
</category>
<category name="com.demo.custom.test.MyCustomClass" additivity="false">
<priority value="TRACE"/>
<appender-ref ref="Fileappender"/>
</category>
<root>
<level value="error" />
<appender-ref ref="ConsoleAppender"/>
</root>
</log4j:configuration>
I am moving to log4j v 2.17.1.
CodePudding user response:
I have resolved my issue by doing the below changes to my /home/user/TEST_log4j.xml
<Configuration>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d{MM/dd HH:mm:ss} %-5p 0.30c %x - %m\n"/>
</Console>
<RollingFile name="RollingFile" fileName="/opt/tomcat/logs/MyCustomClassLogs.log" filePattern="/opt/tomcat/logs/MyCustomClassLogs.log-%i">
<PatternLayout>
<pattern>%d{MM/dd HH:mm:ss} %-5p 0.30c %x - %m\n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="5 MB" />
</Policies>
<DefaultRolloverStrategy max="5"/>
</RollingFile>
<RollingFile name="ResetClass" fileName="/opt/tomcat/logs/resetTrace.log" filePattern="/opt/tomcat/logs/resetTrace.log-%i">
<PatternLayout>
<pattern>%d{MM/dd HH:mm:ss} %-5p 0.30c %x - %m\n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="5 MB" />
</Policies>
<DefaultRolloverStrategy max="5"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.demo.custom.filter.ResetClass" level="TRACE">
<AppenderRef ref="ResetClass"/>
</Logger>
<Logger name="com.demo.custom.test.MyCustomClass" level="TRACE">
<AppenderRef ref="RollingFile"/>
</Logger>
<Root level="error">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>