<?xmlversion="1.0"encoding="UTF-8"?><Configurationstatus="off"monitorInterval="1800"><properties><propertyname="LOG_HOME">/opt/apache-tomcat-9.0.6/logs</property><!--<propertyname="LOG_HOME">/Users/lhq/apache-tomcat-9.0.6/logs</property>--><propertyname="FILE_NAME">mylog</property></properties><Appenders><Consolename="Console"target="SYSTEM_OUT"><PatternLayoutpattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/></Console><!--<Filename="File"fileName="${LOG_HOME}/${FILE_NAME}.log">--><!--<PatternLayout>--><!--<Pattern>%date{yyyy-MM-ddHH:mm:ss.SSS} %level [%thread][%file:%line] -%msg%n</Pattern>--><!--</PatternLayout>--><!--</File>--><!--注意File和RollingFile不能同时存在,每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档--><RollingFilename="RollingFileInfo"fileName="${LOG_HOME}/${FILE_NAME}.log"filePattern="${LOG_HOME}/${FILE_NAME}-%d{MM-dd-yyyy}-%i.log"><!--控制台只输出info及以上级别的信息(onMatch),其他的直接拒绝(onMismatch),根据这个可以特殊定制化--><!--<ThresholdFilterlevel="info"onMatch="ACCEPT"onMismatch="DENY"/>--><PatternLayout><Pattern>%date{yyyy-MM-ddHH:mm:ss.SSS} %level [%thread][%file:%line] -%msg%n</Pattern><!--定义日志模板,输出的格式--></PatternLayout><Policies><!--按天进行分割日志--><TimeBasedTriggeringPolicy/><!--按文件占用空间进行分割日志--><SizeBasedTriggeringPolicysize="20MB"/></Policies><!--当日文件数量不能超过100个,超过100个会被覆盖--><DefaultRolloverStrategymax="100"><DeletebasePath="${LOG_HOME}"maxDepth="1"><IfFileNameglob="*/${FILE_NAME}-%d{MM-dd-yyyy}-%i.log"/><!--保留30天以内日志文件--><IfLastModifiedage="30d"/></Delete></DefaultRolloverStrategy></RollingFile></Appenders><Loggers><Loggername="com.hsaudio"level="DEBUG"additivity="true"><AppenderRefref="Console"/></Logger><Rootlevel="error"><AppenderRefref="RollingFileInfo"level="ERROR"/></Root></Loggers></Configuration>
这里遇到一个坑,Unable to invoke factory method in class class org.apache.logging.log4j.core.appender.RollingFileAppender for element RollingFile.
Appenders中File和RollingFile节点不同同时存在,否者初始化日志工程时报错。
集成需要的maven,这个集成spring mvc
<properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><!--依赖版本--><spring.version>4.3.7.RELEASE</spring.version><log4j2.version>2.8.2</log4j2.version><slf4j.version>1.7.25</slf4j.version><mybatis-spring.version>1.3.1</mybatis-spring.version><fastjson.version>1.2.31</fastjson.version></properties><dependencies><!--日志begin--><!--slf4jconfigstart--><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>${slf4j.version}</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>jcl-over-slf4j</artifactId><version>${slf4j.version}</version></dependency><!--slf4jconfigend--><!--桥接:告诉Slf4j使用Log4j2--><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-slf4j-impl</artifactId><version>${log4j2.version}</version><exclusions><exclusion><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId></exclusion></exclusions></dependency><!--桥接:告诉commonslogging使用Log4j2--><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-jcl</artifactId><version>${log4j2.version}</version></dependency><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-api</artifactId><version>${log4j2.version}</version></dependency><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-core</artifactId><version>${log4j2.version}</version></dependency><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-web</artifactId><version>${log4j2.version}</version></dependency><!--日志end--></dependencies>
最近log4j2漏洞吵得沸沸扬扬,建议各位把版本包升级到最新