Configuration method of log4j2 in ssm (xml mode)

Method 1: adopt the default configuration

 1. Configure in web.xml

 2. Add the log4j2 configuration file under the resource resource path, note that the file name is log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="OFF" monitorInterval="1800">
    <properties>
        <property name="LOG_HOME">D:/logs-dxk-ssmbasics</property>
        <property name="FILE_NAME">finance-pay</property>
    </properties>

    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>

        <!--日志输出路径-->
        <RollingFile name="running-log" fileName="${LOG_HOME}/${FILE_NAME}.log"
                     filePattern="${LOG_HOME}/$${date:yyyy-MM}/${FILE_NAME}-%d{yyyy-MM-dd}-%i.log.gz"
                     immediateFlush="true">
            <PatternLayout
                    pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n" />
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="10 MB" />
            </Policies>
            <DefaultRolloverStrategy max="20" />
        </RollingFile>
    </Appenders>
    <Loggers>

        <Logger name="com.nn.*" level="trace" additivity="true">
            <AppenderRef ref="running-log"/>
        </Logger>

        <Root level="info">
            <!-- 这里是输入到文件,很重要-->
            <AppenderRef ref="running-log"/>
            <!-- 这里是输入到控制台-->
            <AppenderRef ref="Console"/>
        </Root>
        <!--这里不再进行配置打印sql  可以到druid监控中心查看http://localhost:8080/ssm/druid-->

    </Loggers>
</Configuration>

Method two (recommended):

 1. Configure in xml, you can modify the configuration file path to deal with obsessive-compulsive disorder patients (me)

 2. Implement ServletContextListener

public class Log4j2ConfigListener implements ServletContextListener {
    private static final String KEY = "log4jConfigLocation";

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        String fileName = getContextParam(arg0);
        Configurator.initialize("Log4j2", fileName);
    }

    private String getContextParam(ServletContextEvent event) {
        Enumeration<String> names = event.getServletContext().getInitParameterNames();
        while (names.hasMoreElements()){
            String name = names.nextElement();
            String value = event.getServletContext().getInitParameter(name);
            if(name.trim().equals(KEY)){
                return value;
            }
        }
        return null;
    }

}

3. The content of the configuration file is the same as that of the first method log4j2.xml (you're done, detailed configuration, you can find it yourself, I only made a simple configuration here)


The following is the error that occurred during my configuration can refer to the following ---- "":

The first method will report an error if the configuration file is not placed under resource, and the configuration file cannot be found (the official default is to put the configuration file log4j2.xml under the resource path -resource):

12-Nov-2018 13:51:26.488 INFO [RMI TCP Connection(3)-127.0.0.1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. Set system property 'log4j2.debug' to show Log4j2 internal initialization logging.

Guess you like

Origin blog.csdn.net/ke_new/article/details/83990828