首页 > 代码库 > log4j2指定日志文件路径到工程路径

log4j2指定日志文件路径到工程路径

1、pom.xml中配置log4j2所使用的jar包

<!-- log4j2 -->        <dependency>            <groupId>org.apache.logging.log4j</groupId>            <artifactId>log4j-core</artifactId>            <version>${log4j}</version>        </dependency>        <dependency>            <groupId>org.apache.logging.log4j</groupId>            <artifactId>log4j-api</artifactId>            <version>${log4j}</version>        </dependency>        <dependency>            <groupId>org.apache.logging.log4j</groupId>            <artifactId>log4j-slf4j-impl</artifactId>            <version>${log4j}</version>        </dependency>        <dependency>            <groupId>org.apache.logging.log4j</groupId>            <artifactId>log4j-web</artifactId>            <version>${log4j}</version>        </dependency>

2、在classpath目录中创建log4j2.xml文件

<?xml version="1.0" encoding="UTF-8"?><Configuration status="INFO" monitorInterval="600">    <!--先定义所有的appender -->    <appenders>        <!--输出控制台 -->        <Console name="Console" target="SYSTEM_OUT">            <ThresholdFilter level="debug" />            <PatternLayout charset="GBK"                pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n" />        </Console>        <!--输出到文件-->        <File name="LogFile" fileName="${web:rootDir}/logs/app.log" advertise="true">            <PatternLayout pattern="%d %p %C{1.} [%t] %m%n" />        </File>    </appenders>    <loggers>        <!--建立一个默认的root的logger -->        <root level="info">            <appender-ref ref="LogFile" />            <appender-ref ref="Console" />        </root>    </loggers></Configuration>

3、web.xml中添加如下信息

    <context-param>        <param-name>webAppRootKey</param-name>        <param-value>webApp.root</param-value>    </context-param>    <listener>        <listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>    </listener>

 

log4j2指定日志文件路径到工程路径