Commit e9eec4b4 by QIANGLU

新增日志级别动态变更

parent 837337c6
......@@ -13,15 +13,10 @@
<name>${project.artifactId}</name>
<dependencies>
<!-- &lt;!&ndash; 桥接slf4j &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>org.slf4j</groupId>-->
<!-- <artifactId>jcl-over-slf4j</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.slf4j</groupId>-->
<!-- <artifactId>log4j-over-slf4j</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 打印skywalking tid插件-->
<dependency>
<groupId>org.apache.skywalking</groupId>
......@@ -31,6 +26,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.secoo.mall.logs.config;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import com.ctrip.framework.apollo.spring.boot.ApolloAutoConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.logging.LogLevel;
import org.springframework.boot.logging.LoggingSystem;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Set;
/**
* @author qianglu
*/
@Configuration
@ConditionalOnClass(ApolloAutoConfiguration.class)
public class MatrixLogListenerConfiguration {
private static final Logger logger = LoggerFactory.getLogger(MatrixLogListenerConfiguration.class);
private static final String LOGGER_TAG = "matrix.loggers.";
@Resource
private LoggingSystem loggingSystem;
@ApolloConfig
private Config config;
@ApolloConfigChangeListener
private void onChange(ConfigChangeEvent changeEvent) {
refreshLoggingLevels();
}
@PostConstruct
private void refreshLoggingLevels() {
Set<String> keyNames = config.getPropertyNames();
for (String key : keyNames) {
if (containsIgnoreCase(key, LOGGER_TAG)) {
String strLevel = config.getProperty(key, "info");
LogLevel level = LogLevel.valueOf(strLevel.toUpperCase());
loggingSystem.setLogLevel(key.replace(LOGGER_TAG, ""), level);
logger.info("update class {} logger level to {}", key, strLevel);
}
}
}
private static boolean containsIgnoreCase(String str, String searchStr) {
if (StringUtils.isEmpty(str) || StringUtils.isEmpty(searchStr)) {
return false;
}
int len = searchStr.length();
int max = str.length() - len;
for (int i = 0; i <= max; i++) {
if (str.regionMatches(true, i, searchStr, 0, len)) {
return true;
}
}
return false;
}
}
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.secoo.mall.logs.config.MatrixLogListenerConfiguration
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!-- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/2002/xmlspec/dtd/2.10/xmlspec.dtd"> -->
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<springProperty scope="context" name="logName" source="spring.application.name" defaultValue="app"/>
<springProperty scope="context" name="logLevel" source="log.level" defaultValue="info"/>
<springProperty scope="context" name="LOG_NAME" source="spring.application.name" defaultValue="app"/>
<springProperty scope="context" name="LOG_LEVEL" source="log.level" defaultValue="info"/>
<springProperty scope="context" name="LOG_PATH" source="log.path" defaultValue="/data/logs"/>
<springProperty scope="context" name="MAX_FILE_SIZE" source="log.max_file_size" defaultValue="128MB"/>
<springProperty scope="context" name="MAX_HISTORY" source="log.max_history" defaultValue="7"/>
<springProperty scope="context" name="PATTERN" source="log.pattern" defaultValue="-|%d{yyyy-MM-dd HH:mm:ss.SSS}|%-5level|%X{tid}|%thread|%logger{36}.%M:%L-%msg%n"/>
<property name="LOG_DIR" value="${LOG_PATH}/${logName}/%d{yyyyMMdd}"/>
<property name="LOG_DIR" value="${LOG_PATH}/${LOG_NAME}/%d{yyyyMMdd}"/>
<property name="CHARSET" value="UTF-8"/>
<jmxConfigurator/>
......@@ -31,9 +30,9 @@
<onMismatch>DENY</onMismatch>
</filter>
<!-- 定义文件的名称 -->
<file>${LOG_PATH}/${logName}/error.log</file>
<file>${LOG_PATH}/${LOG_NAME}/error.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${LOG_DIR}/err_${logName}%i.log</FileNamePattern>
<FileNamePattern>${LOG_DIR}/err_${LOG_NAME}%i.log</FileNamePattern>
<MaxHistory>${MAX_HISTORY}</MaxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>${MAX_FILE_SIZE}</maxFileSize>
......@@ -46,9 +45,9 @@
<appender name="APP" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 定义文件的名称 -->
<file>${LOG_PATH}/${logName}/app.log</file>
<file>${LOG_PATH}/${LOG_NAME}/app.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${LOG_DIR}/all_${logName}%i.log</FileNamePattern>
<FileNamePattern>${LOG_DIR}/all_${LOG_NAME}%i.log</FileNamePattern>
<MaxHistory>${MAX_HISTORY}</MaxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>${MAX_FILE_SIZE}</maxFileSize>
......@@ -80,17 +79,17 @@
</appender>
<logger name="app" level="${logLevel}">
<logger name="app" level="${LOG_LEVEL}">
<appender-ref ref="ASYN_APP"/>
</logger>
<logger name="error" level="${logLevel}">
<logger name="error" level="${LOG_LEVEL}">
<appender-ref ref="ASYN_ERROR"/>
</logger>
<!-- 日志排除 -->
<logger name="org.logicalcobwebs.proxool" level="error"/>
<!--统一日志输出级别,其他appender中如果有高于此处等级设置的也会被输出 -->
<root level="${logLevel}">
<root level="${LOG_LEVEL}">
<springProfile name="test,dev,local">
<appender-ref ref="ASYN_STDOUT"/>
</springProfile>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment