Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4j to logback(migration)

We are using log4j+commons-logging in our current projects. Now we are mirgrating from log4j to Logback, so can we just use replace log4j.properties with logback.xml or we have to convert log4j to SLF4J?

as per the suggestion i kept the code same i have 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";

above code is part of LoggerUtils.java it is in commonutils. now there is AccInqWeb module where use LoggerUtils for loggings like this

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

I kept the LoggerUtils.java code as it is and include log4j-over-slf4.jar and remove log4j.jar now it is compiled fine and i deploy commonutils module on 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 logback jar on classpath but i am not getting any logs we use WLS10.3.0 is there anything that i need to do with configuration

like image 508
user1137387 Avatar asked Nov 04 '22 03:11

user1137387


1 Answers

Logback should work with log4j

The logback-classic module can be assimilated to a significantly improved version of log4j. Moreover, logback-classic natively implements the SLF4J API so that you can readily switch back and forth between logback and other logging frameworks such as log4j or java.util.logging (JUL). logback website

Add the jar file to your project or POM

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

To switch to logback first thing you want to do is add the logback.xml into your resources folder. It 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 within your web.xml from log4j to logback

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

logback manual

like image 115
Michael Avatar answered Nov 09 '22 14:11

Michael