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.

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
使用Log4j2作为日志框架时,可以通过以下步骤进行配置: 1. 添加Log4j2依赖 在pom.xml文件添加以下依赖: ``` <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.14.1</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.14.1</version> </dependency> ``` 2. 创建Log4j2配置文件 在src/main/resources目录下创建log4j2.xml文件,并添加以下内容: ``` <?xml version="1.0" encoding="UTF-8"?> <Configuration status="INFO"> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d %-5level %logger{36} - %msg%n"/> </Console> </Appenders> <Loggers> <Root level="info"> <AppenderRef ref="Console"/> </Root> </Loggers> </Configuration> ``` 该配置文件定义了一个名为Console的Appender,它将日志输出到console。同时,定义了一个Root Logger,它的日志级别为info,并且引用了Console Appender。 3. 在Spring使用Log4j2 在Spring项目使用Log4j2时,需要在applicationContext.xml文件添加以下配置: ``` <bean id="log4jServletContextInitializer" class="org.springframework.boot.logging.log4j2.Log4J2ServletContextInitializer" /> <bean id="log4jWebConfigurer" class="org.springframework.boot.logging.log4j2.Log4J2WebConfigurer" depends-on="log4j2ServletContextInitializer"> <property name="location" value="classpath:log4j2.xml" /> </bean> ``` 这些配置启用Log4j2,并将log4j2.xml配置文件放置在classpath。 以上就是使用Log4j2作为日志框架的基本配置步骤。如果需要更复杂的配置,可以参考Log4j2官方文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xkng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值