ssm中日志log4j2的配置方法(xml方式)

方法一:采取默认配置

 1.在web.xml中配置

 2.在resource资源路径下加入log4j2的配置文件,注意文件名为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>

方法二(推荐):

 1.在xml当中配置,可以修改配置文件路径,应对强迫症患者(me)

 2.实现 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.配置文件和第一种方法的内容一样log4j2.xml(大功告成,详细配置,可以自己查找下,我这里只进行了简单的配置)


下面是我配置的时候出现的错误可以参考下----》》:

第一种方法不把配置文件放在resource下会报错,找不到配置文件(官方默认将配置文件log4j2.xml放到资源路径下-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.

猜你喜欢

转载自blog.csdn.net/ke_new/article/details/83990828