How does the old framework SSM smoothly convert log4j to logback?

If you still have the illusion of upgrading log4j to the 2X version, you have to make sure your JDK version and framework allow it. After many tests to no avail, I decided to delete log4j and change it to logcak. The following steps will teach you a smooth switch.

Click to download the logback dependency package for free icon-default.png?t=N7T8https://download.csdn.net/download/mayanhui11/88449383

  • Delete log4j related dependencies

The main dependencies are: log4j, slf4j-log4j12, slf4j-api, etc. slf4j-api is an upgraded version. There is a class in slf4j-log4j12 that conflicts with the class in logback, which will cause the log not to be written to the file.

  • Add logback dependencies

First of all, you have to be ready to upgrade the logback jar package. If you haven’t, don’t be afraid. I think you are ready. The version we are currently using is 1.1.11, mainly including: logback-classic-1.1.11.jar, logback-core -1.1.11.jar, logback-ext-spring-0.1.2.jar, slf4j-api-1.7.16.jar, etc. Copy these files to the lib directory in the project.

  • Add logback configuration file

Find the relevant configuration files (such as log4j.properties or log4j.xml) used in the project, backup them and delete them. Create a new logback configuration file, which can be named logback.xml, and configure it as needed. You can also use the following (I found it on other blogs and it seems more comprehensive).

<?xml version="1.0" encoding="UTF-8"?>
<!-- Leveled asynchronous file log output configuration -->
<!-- Levels from high to low OFF, FATAL, ERROR, WARN, INFO, DEBUG, TRACE, ALL -->
<!-- Log output rules According to the current ROOT level, when the log output is higher than the root default level, it will be output -->
<!-- Each configured filter below filters out high-level files in the output file, but low-level log information still appears. Only logs of this level are recorded through filter filtering -->
<!-- scan When this property is set to true, the configuration file will be reloaded if it changes. The default value is true. -->
<!-- scanPeriod sets the time interval for monitoring whether the configuration file is modified. If no time unit is given, the default unit is milliseconds. This property takes effect when scan is true. The default interval is 1 minute. -->
<!-- debug When this attribute is set to true, logback internal log information will be printed out and logback running status can be viewed in real time. The default value is false. -->
<configuration scan="true" scanPeriod="60 seconds" debug="false">

    <!-- logback project name -->
    <property name="appName" value="sdhyschedule"></property>

    <!-- Log level DEBUGER INFO WARN ERROR -->
    <property name="logLevel" value="INFO"></property>

    <!-- Log path -->
    <property name="logPath" value="${catalina.home}/logs/schedule"></property>

    <!--Maximum storage time 30 days-->
    <property name="maxHistory" value="10"/>

    <!-- The depth of the asynchronous buffer queue, this value will affect performance. The default value is 256 -->
    <property name="queueSize" value="256"></property>

    <!-- lOGGER PATTERN Choose matching according to personal preference -->
    <property name="logPattern" value="[ %-5level] [?te{yyyy-MM-dd HH:mm:ss.SSS}] %logger{96} [%line] [%thread]- %msg%n"></property>
    <!-- %d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] %logger - %msg%n -->
    <!-- %d{yyyy-MM-dd HH:mm:ss} %-4relative [%thread] %-5level %logger{35} - %msg %n -->
    <!-- [ %-5level] [?te{yyyy-MM-dd HH:mm:ss.SSS}] %logger{96} [%line] [%thread]- %msg%n -->

    <!-- Dynamic log level -->
    <jmxConfigurator/>

    <!-- The standard output of the console -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>${logPattern}</pattern>
        </encoder>
    </appender>

    <!-- DUBUG logging -->
    <appender name="FILE_DEBUG" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>DEBUG</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <file>${logPath}/${appName}_debug.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${logPath}/debug/${appName}_debug.log.%d{yyyy-MM-dd}.zip
            </fileNamePattern>
            <maxHistory>${maxHistory}</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>${logPattern}</pattern>
        </encoder>
    </appender>

    <!-- INFO level logging -->
    <appender name="FILE_INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFO</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <file>${logPath}/${appName}_info.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${logPath}/info/${appName}_info.log.%d{yyyy-MM-dd}.zip
            </fileNamePattern>
            <maxHistory>${maxHistory}</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>${logPattern}</pattern>
            <charset class="java.nio.charset.Charset">UTF-8</charset>
        </encoder>
    </appender>

    <!-- WARN level logging -->
    <appender name="FILE_WARN" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>WARN</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <file>${logPath}/${appName}_warn.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${logPath}/warn/${appName}_warn.log.%d{yyyy-MM-dd}.zip
            </fileNamePattern>
            <maxHistory>${maxHistory}</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>${logPattern}</pattern>
        </encoder>
    </appender>

    <!-- Error level logging -->
    <appender name="FILE_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <file>${logPath}/${appName}_error.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${logPath}/error/${appName}_error.log.%d{yyyy-MM-dd}.zip
            </fileNamePattern>
            <maxHistory>${maxHistory}</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>${logPattern}</pattern>
        </encoder>
    </appender>

    <!-- ASYNC_LOG_DEBUG -->
    <appender name="ASYNC_LOG_DEBUG" class="ch.qos.logback.classic.AsyncAppender">
        <!-- Do not lose logs. By default, if 80% of the queue is full, TRACT, DEBUG, and INFO level logs will be discarded -->
        <discardingThreshold>0</discardingThreshold>
        <!-- Change the default queue depth, which will affect performance. The default value is 256 -->
        <queueSize>${queueSize}</queueSize>
        <appender-ref ref="FILE_DEBUG"/>
    </appender>

    <!-- ASYNC_LOG_INFO -->
    <appender name="ASYNC_LOG_INFO" class="ch.qos.logback.classic.AsyncAppender">
        <!-- Do not lose logs. By default, if 80% of the queue is full, TRACT, DEBUG, and INFO level logs will be discarded -->
        <discardingThreshold>0</discardingThreshold>
        <!-- Change the default queue depth, which will affect performance. The default value is 256 -->
        <queueSize>${queueSize}</queueSize>
        <appender-ref ref="FILE_INFO"/>
    </appender>

    <!-- ASYNC_LOG_WARN -->
    <appender name="ASYNC_LOG_WARN" class="ch.qos.logback.classic.AsyncAppender">
        <!-- Do not lose logs. By default, if 80% of the queue is full, TRACT, DEBUG, and INFO level logs will be discarded -->
        <discardingThreshold>0</discardingThreshold>
        <!-- Change the default queue depth, which will affect performance. The default value is 256 -->
        <queueSize>${queueSize}</queueSize>
        <appender-ref ref="FILE_WARN"/>
    </appender>

    <!--ASYNC_LOG_ERROR -->
    <appender name="ASYNC_LOG_ERROR" class="ch.qos.logback.classic.AsyncAppender">
        <!-- Do not lose logs. By default, if 80% of the queue is full, TRACT, DEBUG, and INFO level logs will be discarded -->
        <discardingThreshold>0</discardingThreshold>
        <!-- Change the default queue depth, which will affect performance. The default value is 256 -->
        <queueSize>${queueSize}</queueSize>
        <appender-ref ref="FILE_ERROR"/>
    </appender>

    <!-- Log recording level -->
    <!-- Reference APPENDER after definition -->
    <root level="${logLevel}">
        <!-- Console -->
        <appender-ref ref="STDOUT"/>

        <!-- Specific log level and file configuration -->
        <appender-ref ref="ASYNC_LOG_DEBUG"/>
        <appender-ref ref="ASYNC_LOG_INFO"/>
        <appender-ref ref="ASYNC_LOG_WARN"/>
        <appender-ref ref="ASYNC_LOG_ERROR"/>
    </root>
</configuration>
  • Configure web.xml to enable logback

Just replace the monitoring and configuration of log4j directly.

<!-- logback monitoring-->
    <context-param><param-name>logbackConfigLocation</param-name>
        <param-value>classpath:logback-spring.xml</param-value>
    </context-param>
    <listener>
        <listener-class>ch.qos.logback.ext.spring.web.LogbackConfigListener</listener-class>
    </listener>
  • Conclusion:

There are some differences in configuration and usage between logback and log4j, so some additional adjustments and configuration may be required during the conversion process. Make sure to conduct adequate testing before converting and back up important configuration and code files to prevent unexpected situations.

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java Skill TreeHomepageOverview 137929 people are learning the system