0004-log4j2日志输出配置
作者:互联网
引入依赖:
核心依赖
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.12.0</version>
</dependency>
在 springboot 中由于已经存在 spring-boot-starter-log4j2 了,所以我们直接使用spring-boot-starter-log4j2就行了(注意排除spring默认的日志处理logback)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 排除spring boot默认日志logback -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 引入log4j2依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
添加配置文件
默认情况下,Log4j2在classpath下查找名为log4j2.xml
的配置文件。你也可以使用Java启动命令指定配置文件的全路径。-Dlog4j.configurationFile=opt/demo/log4j2.xml
,你还可以使用Java代码指定配置文件路径
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.LoggerContext;
import java.io.File;
public static void main(String[] args) {
LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
File file = new File("opt/demo/log4j2.xml");
loggerContext.setConfigLocation(file.toURI());
}
具体实战
这里采用在classpath下创建名为log4j2.xml
的配置文件
-
引入依赖
-
配置log4j2.xml文件(在resources下建立log4j2.xml文件)
<?xml version="1.0" encoding="UTF-8"?> <Configuration> <Properties> <!-- 日志输出级别 --> <Property name="LOG_INFO_LEVEL" value="info"/> <!-- error级别日志 --> <Property name="LOG_ERROR_LEVEL" value="error"/> <!-- 在当前目录下创建名为log目录做日志存放的目录 --> <Property name="LOG_HOME" value="./log"/> <!-- 档案日志存放目录 --> <Property name="LOG_ARCHIVE" value="./log/archive"/> <!-- 模块名称, 影响日志配置名,日志文件名,根据自己项目进行配置 --> <Property name="LOG_MODULE_NAME" value="spring-boot"/> <!-- 日志文件大小,超过这个大小将被压缩 --> <Property name="LOG_MAX_SIZE" value="100 MB"/> <!-- 保留多少天以内的日志 --> <Property name="LOG_DAYS" value="15"/> <!--输出日志的格式:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度, %msg:日志消息,%n是换行符 --> <Property name="LOG_PATTERN" value="%d{yyyy-MM-dd hh:mm:ss.SSS} | %-5level | [%t] |%logger{50} | %L | : %msg%n"/> <!--interval属性用来指定多久滚动一次--> <Property name="TIME_BASED_INTERVAL" value="1"/> </Properties> <Appenders> <!-- 控制台输出 --> <Console name="STDOUT" target="SYSTEM_OUT"> <!--输出日志的格式--> <PatternLayout pattern="${LOG_PATTERN}"/> <!--控制台只输出level及其以上级别的信息(onMatch),其他的直接拒绝(onMismatch)--> <ThresholdFilter level="${LOG_INFO_LEVEL}" onMatch="ACCEPT" onMismatch="DENY"/> </Console> <!-- 这个会打印出所有的info级别以上,error级别一下的日志,每次大小超过size或者满足TimeBasedTriggeringPolicy,则日志会自动存入按年月日建立的文件夹下面并进行压缩,作为存档--> <RollingRandomAccessFile name="RollingRandomAccessFileInfo" fileName="${LOG_HOME}/${LOG_MODULE_NAME}-infoLog.log" filePattern="${LOG_ARCHIVE}/${LOG_MODULE_NAME}-infoLog-%d{yyyy-MM-dd}-%i.log.gz"> <Filters> <!--如果是error级别拒绝,设置 onMismatch="NEUTRAL" 可以让日志经过后续的过滤器--> <ThresholdFilter level="${LOG_ERROR_LEVEL}" onMatch="DENY" onMismatch="NEUTRAL"/> <!--如果是info\warn输出--> <ThresholdFilter level="${LOG_INFO_LEVEL}" onMatch="ACCEPT" onMismatch="DENY"/> </Filters> <PatternLayout pattern="${LOG_PATTERN}"/> <Policies> <!--interval属性用来指定多久滚动一次,根据当前filePattern设置是1天滚动一次--> <TimeBasedTriggeringPolicy interval="${TIME_BASED_INTERVAL}"/> <SizeBasedTriggeringPolicy size="${LOG_MAX_SIZE}"/> </Policies> <!-- DefaultRolloverStrategy属性如不设置,则默认同一文件夹下最多保存7个文件--> <DefaultRolloverStrategy max="${LOG_DAYS}"/> </RollingRandomAccessFile> <!--只记录error级别以上的日志,与info级别的日志分不同的文件保存--> <RollingRandomAccessFile name="RollingRandomAccessFileError" fileName="${LOG_HOME}/${LOG_MODULE_NAME}-errorLog.log" filePattern="${LOG_ARCHIVE}/${LOG_MODULE_NAME}-errorLog-%d{yyyy-MM-dd}-%i.log.gz"> <Filters> <ThresholdFilter level="${LOG_ERROR_LEVEL}" onMatch="ACCEPT" onMismatch="DENY"/> </Filters> <PatternLayout pattern="${LOG_PATTERN}"/> <Policies> <TimeBasedTriggeringPolicy interval="${TIME_BASED_INTERVAL}"/> <SizeBasedTriggeringPolicy size="${LOG_MAX_SIZE}"/> </Policies> <DefaultRolloverStrategy max="${LOG_DAYS}"/> </RollingRandomAccessFile> </Appenders> <Loggers> <!-- 开发环境使用 --> <!-- <Root level="${LOG_INFO_LEVEL}">--> <!-- <AppenderRef ref="STDOUT"/>--> <!-- </Root>--> <!-- 测试,生产环境使用 --> <Root level="${LOG_INFO_LEVEL}"> <AppenderRef ref="RollingRandomAccessFileInfo"/> <AppenderRef ref="RollingRandomAccessFileError"/> </Root> </Loggers> </Configuration>
-
测试
启动项目
2021-10-12 03:57:42.716 | INFO | [main] |com.zhoust.fastdome.FastDomeApplication | 50 | : Starting FastDomeApplication on DESKTOP-27SPPG5 with PID 7940 (D:\workspace\git\zhoustcomm\fast-dome\target\classes started by zhoushengtao in D:\workspace\git\zhoustcomm) 2021-10-12 03:57:42.725 | INFO | [main] |com.zhoust.fastdome.FastDomeApplication | 675 | : No active profile set, falling back to default profiles: default 2021-10-12 03:57:44.026 | INFO | [main] |org.springframework.boot.web.embedded.tomcat.TomcatWebServer | 90 | : Tomcat initialized with port(s): 8088 (http) 2021-10-12 03:57:44.037 | INFO | [main] |org.apache.coyote.http11.Http11NioProtocol | 173 | : Initializing ProtocolHandler ["http-nio-8088"] 2021-10-12 03:57:44.050 | INFO | [main] |org.apache.catalina.core.StandardService | 173 | : Starting service [Tomcat] 2021-10-12 03:57:44.050 | INFO | [main] |org.apache.catalina.core.StandardEngine | 173 | : Starting Servlet Engine: Apache Tomcat/9.0.13 2021-10-12 03:57:44.056 | INFO | [main] |org.apache.catalina.core.AprLifecycleListener | 173 | : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [D:\codersoft\jdk1.8.0_20\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;D:\codersoft\jdk1.8.0_20\bin;D:\codersoft\maven\apache-maven-3.5.0\bin;D:\codersoft\Git\bin;C:\Users\zhoushengtao\AppData\Local\Microsoft\WindowsApps;;.] 2021-10-12 03:57:44.180 | INFO | [main] |org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/] | 173 | : Initializing Spring embedded WebApplicationContext 2021-10-12 03:57:44.181 | INFO | [main] |org.springframework.web.context.ContextLoader | 296 | : Root WebApplicationContext: initialization completed in 1418 ms 2021-10-12 03:57:44.236 | INFO | [main] |com.zhoust.fastdome.filter.MyFilter | 24 | : >>>>>>>>>>过滤器出生<<<<<<<<<<<< 2021-10-12 03:57:45.342 | INFO | [main] |org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor | 171 | : Initializing ExecutorService 'applicationTaskExecutor' 2021-10-12 03:57:45.613 | INFO | [main] |org.apache.coyote.http11.Http11NioProtocol | 173 | : Starting ProtocolHandler ["http-nio-8088"] 2021-10-12 03:57:45.624 | INFO | [main] |org.apache.tomcat.util.net.NioSelectorPool | 173 | : Using a shared selector for servlet write/read 2021-10-12 03:57:45.638 | INFO | [main] |org.springframework.boot.web.embedded.tomcat.TomcatWebServer | 204 | : Tomcat started on port(s): 8088 (http) with context path '' 2021-10-12 03:57:45.641 | INFO | [main] |com.zhoust.fastdome.FastDomeApplication | 59 | : Started FastDomeApplication in 3.315 seconds (JVM running for 4.768)
参考来源
作者:JourWon
链接:https://www.jianshu.com/p/1b04924d8a1f
岁月如歌,时光藏在每个人的心间
标签:0004,03,57,apache,org,日志,main,log4j2 来源: https://www.cnblogs.com/zhoushengtao/p/15399869.html