SpringBoot log4j2日志框架
第一步: 排除logback
Springboot默认采用logback的日志框架,所以需要排除logback, 不然会引起jar依赖冲突的错误
1 2 3 4 5 6 7 8 9 10 11
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency>
|
第二步: 引入log4j2依赖
1 2 3 4
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency>
|
第三步: 添加配置文件路径
在springboot配置文件application.yml文件中添加配置,
1 2
| logging: config: classpath:log4j2.xml
|
log4j日志配置文件默认名称如果是log4j2-spring.xml, 则不用再application.yml中配置
第四步: 日志配置文件模板
log4j2.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
| <?xml version="1.0" encoding="UTF-8"?> <Configuration> <properties> <property name="LOG_HOME">./logs</property> </properties> <Appenders> <Console name="consoleAppender" target="SYSTEM_OUT"> <PatternLayout pattern="%style{%d{ISO8601}}{bright,green} %highlight{%-5level} [%style{%t}{bright,blue}] %style{%C{}}{bright,yellow}: %msg%n%style{%throwable}{red}" disableAnsi="false" noConsoleNoAnsi="false"/> </Console>
<RollingFile name="allFileAppender" fileName="${LOG_HOME}/all.log" filePattern="${LOG_HOME}/$${date:yyyy-MM}/all-%d{yyyy-MM-dd}-%i.log.gz"> <PatternLayout> <pattern>%d %p %C{} [%t] %m%n</pattern> </PatternLayout> <Policies> <SizeBasedTriggeringPolicy size="100 MB"/> <TimeBasedTriggeringPolicy/> </Policies> <DefaultRolloverStrategy max="100"/> </RollingFile>
<RollingFile name="debugFileAppender" fileName="${LOG_HOME}/debug.log" filePattern="${LOG_HOME}/$${date:yyyy-MM}/debug-%d{yyyy-MM-dd}-%i.log.gz"> <Filters> <ThresholdFilter level="info" onMatch="DENY" onMismatch="NEUTRAL"/> </Filters> <PatternLayout> <pattern>%d %p %C{} [%t] %m%n</pattern> </PatternLayout> <Policies> <SizeBasedTriggeringPolicy size="100 MB"/> <TimeBasedTriggeringPolicy/> </Policies> <DefaultRolloverStrategy max="100"/> </RollingFile>
<RollingFile name="infoFileAppender" fileName="${LOG_HOME}/info.log" filePattern="${LOG_HOME}/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log.gz"> <Filters> <ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/> </Filters> <PatternLayout> <pattern>%d %p %C{} [%t] %m%n</pattern> </PatternLayout> <Policies> <SizeBasedTriggeringPolicy size="100 MB"/> <TimeBasedTriggeringPolicy interval="1" modulate="true" /> </Policies> </RollingFile>
<RollingFile name="warnFileAppender" fileName="${LOG_HOME}/warn.log" filePattern="${LOG_HOME}/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log.gz"> <Filters> <ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/> </Filters> <PatternLayout> <pattern>%d %p %C{} [%t] %m%n</pattern> </PatternLayout> <Policies> <SizeBasedTriggeringPolicy size="100 MB"/> <TimeBasedTriggeringPolicy/> </Policies> <DefaultRolloverStrategy max="100"/> </RollingFile>
<RollingFile name="errorFileAppender" fileName="${LOG_HOME}/error.log" filePattern="${LOG_HOME}/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log.gz"> <PatternLayout> <pattern>%d %p %C{} [%t] %m%n</pattern> </PatternLayout> <Policies> <SizeBasedTriggeringPolicy size="100 MB"/> <TimeBasedTriggeringPolicy/> </Policies> <DefaultRolloverStrategy max="100"/> </RollingFile>
<RollingFile name="errorJsonAppender" fileName="${LOG_HOME}/error-json.log" filePattern="${LOG_HOME}/error-json-%d{yyyy-MM-dd}-%i.log.gz"> <JSONLayout compact="true" eventEol="true" locationInfo="true"/> <Policies> <SizeBasedTriggeringPolicy size="100 MB"/> <TimeBasedTriggeringPolicy interval="1" modulate="true"/> </Policies> </RollingFile> </Appenders>
<Loggers> <Root level="debug"> <AppenderRef ref="allFileAppender" level="all"/> <AppenderRef ref="consoleAppender" level="debug"/> <AppenderRef ref="debugFileAppender" level="debug"/> <AppenderRef ref="infoFileAppender" level="info"/> <AppenderRef ref="warnFileAppender" level="warn"/> <AppenderRef ref="errorFileAppender" level="error"/> <AppenderRef ref="errorJsonAppender" level="error"/> </Root>
<Logger name="org.springframework" level="debug"/> <Logger name="druid.sql.Statement" level="warn"/> <Logger name="com.mybatis" level="warn"/> <Logger name="org.hibernate" level="warn"/> <Logger name="com.zaxxer.hikari" level="info"/> <Logger name="org.quartz" level="info"/> <Logger name="com.andya.demo" level="debug"/> </Loggers>
</Configuration>
|
第五步: 使用log4j2
在需要记录日志的类下定义日志对象变量
1
| public final static Logger log = LoggerFactory.getLogger(需要记录日志的类.class);
|
然后就可以使用此日志对象