Log4j to logback (migration)

We use log4j + commons-logging in our current projects. Now we mirror log4j with Logback, so can we just replace log4j.properties with logback.xml or do we need to convert log4j to SLF4J?

as per the suggestion I saved the code, I have a code like this

import org.apache.log4j.Level; import org.apache.log4j.Logger; import org.apache.log4j.Priority; public class LoggerUtil { // logger static Logger logger; // applications public static String APP_AccInqSERVICE = "AccInqApp"; 

the above code is part of LoggerUtils.java, it is in commonutils. now there is an AccInqWeb module that uses LoggerUtils for such logs

  LoggerUtil.info(LoggerUtil.APP_ACCT_INQ, AccountInqService.class, "searchAccountSnapshot", "method starts...."); 

I saved the LoggerUtils.java code as it is and turned on log4j-over-slf4.jar and deleted log4j.jar, now it compiled fine, and I deploy the commonutils module on the server, and I add add -Dlogback.configurationFile = C: \ u001 \ isuser \ tesbea \ user_projects \ domains \ iservices 10 \ resources \ logback.xml in setDomainEnv.cmd and logback jar I set the logback jar on the classpath, but I don't get any logs that we use WLS10.3.0, is there anything that I need to complete the configuration

+4
source share
1 answer

Redundancy should work with log4j

The logback-classic module can be assimilated into a significantly improved version of log4j. In addition, logback-classic initially implements the SLF4J API, so you can easily switch between logging and other logging systems such as log4j or java.util.logging (JUL). logback website

Add jar file to project or POM

 <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.1.3</version> </dependency> 

To switch to logback, the first thing you want to do is add logback.xml to your resources folder. This is a file that looks like this:

 <?xml version="1.0" encoding="UTF-8"?> <configuration> <property name="USER_HOME" value="C:\\logs\\analyzer" /> <appender name="FILE-THREAD" class="ch.qos.logback.classic.sift.SiftingAppender"> <!-- This is MDC value --> <!-- We will assign a value to 'logFileName' via Java code --> <discriminator> <key>logFileName</key> <defaultValue>head0</defaultValue> </discriminator> <sift> <!-- A standard RollingFileAppender, the log file is based on 'logFileName' at runtime --> <appender name="FILE-${logFileName}" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${USER_HOME}/${logFileName}.log</file> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <Pattern> %d{yyyy-MM-dd HH:mm:ss} %mdc [%thread] %level %logger{35} - %msg%n </Pattern> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> <FileNamePattern>${USER_HOME}/${logFileName}.%i.log.zip </FileNamePattern> <MinIndex>1</MinIndex> <MaxIndex>10</MaxIndex> </rollingPolicy> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <MaxFileSize>10MB</MaxFileSize> </triggeringPolicy> </appender> </sift> </appender> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <layout class="ch.qos.logback.classic.PatternLayout"> <Pattern> %-5level %logger{36} - %msg%n </Pattern> </layout> </appender> <logger name="com.yourproject.analyzer.core" level="debug" additivity="false"> <appender-ref ref="FILE-THREAD" /> <appender-ref ref="STDOUT" /> </logger> <root level="error"> <appender-ref ref="STDOUT" /> </root> </configuration> 

Then you need to change the logging directory in web.xml from log4j to logback

 <context-param> <param-name>loggingName</param-name> <param-value>logbackdirectory</param-value> <context-param> 

logbook

+1
source

Source: https://habr.com/ru/post/1403558/


All Articles