mirror of
https://gitee.com/dromara/MilvusPlus.git
synced 2025-12-06 17:08:27 +08:00
open-log 控制是否打印日志,默认不打印
This commit is contained in:
parent
338110858b
commit
9042662d9a
@ -20,4 +20,5 @@ public class MilvusPropertiesConfiguration {
|
|||||||
private String password;
|
private String password;
|
||||||
private String token;
|
private String token;
|
||||||
private List<String> packages;
|
private List<String> packages;
|
||||||
|
private boolean openLog;
|
||||||
}
|
}
|
||||||
@ -2,6 +2,7 @@ package org.dromara.milvus.plus.service;
|
|||||||
|
|
||||||
import io.milvus.v2.client.MilvusClientV2;
|
import io.milvus.v2.client.MilvusClientV2;
|
||||||
import org.dromara.milvus.plus.config.MilvusPropertiesConfiguration;
|
import org.dromara.milvus.plus.config.MilvusPropertiesConfiguration;
|
||||||
|
import org.dromara.milvus.plus.log.LogLevelController;
|
||||||
import org.dromara.milvus.plus.model.MilvusProperties;
|
import org.dromara.milvus.plus.model.MilvusProperties;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@ -23,6 +24,7 @@ public class MilvusInit extends AbstractMilvusClientBuilder {
|
|||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void initialize() {
|
public void initialize() {
|
||||||
printBanner();
|
printBanner();
|
||||||
|
LogLevelController.setLoggingEnabledForPackage("org.dromara.milvus.plus", milvusPropertiesConfiguration.isOpenLog());
|
||||||
MilvusProperties milvusProperties = new MilvusProperties();
|
MilvusProperties milvusProperties = new MilvusProperties();
|
||||||
BeanUtils.copyProperties(milvusPropertiesConfiguration, milvusProperties);
|
BeanUtils.copyProperties(milvusPropertiesConfiguration, milvusProperties);
|
||||||
super.setProperties(milvusProperties);
|
super.setProperties(milvusProperties);
|
||||||
|
|||||||
@ -0,0 +1,35 @@
|
|||||||
|
package org.dromara.milvus.plus.log;
|
||||||
|
|
||||||
|
import ch.qos.logback.classic.Level;
|
||||||
|
import ch.qos.logback.classic.LoggerContext;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
public class LogLevelController {
|
||||||
|
|
||||||
|
// 获取LoggerContext
|
||||||
|
private static final LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置特定包下所有类的日志级别。
|
||||||
|
* @param packageName 包名
|
||||||
|
* @param level 日志级别
|
||||||
|
*/
|
||||||
|
public static void setLogLevelForPackage(String packageName, Level level) {
|
||||||
|
for (ch.qos.logback.classic.Logger logger : loggerContext.getLoggerList()) {
|
||||||
|
if (logger.getName().startsWith(packageName)) {
|
||||||
|
logger.setLevel(level);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 动态设置日志开关。
|
||||||
|
* 当设置为Level.OFF时,等同于关闭日志。
|
||||||
|
* @param packageName 包名
|
||||||
|
* @param enabled 是否启用日志
|
||||||
|
*/
|
||||||
|
public static void setLoggingEnabledForPackage(String packageName, boolean enabled) {
|
||||||
|
Level level = enabled ? Level.DEBUG : Level.OFF; // 可以根据需要设置为INFO, WARN, ERROR等
|
||||||
|
setLogLevelForPackage(packageName, level);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,6 +1,6 @@
|
|||||||
package org.dromara.solon;
|
package org.dromara.solon;
|
||||||
|
|
||||||
import org.dromara.solon.entity.MilvusProperties;
|
import org.dromara.solon.entity.MilvusPropertiesConfiguration;
|
||||||
import org.dromara.solon.service.MilvusInit;
|
import org.dromara.solon.service.MilvusInit;
|
||||||
import org.noear.solon.core.AppContext;
|
import org.noear.solon.core.AppContext;
|
||||||
import org.noear.solon.core.Plugin;
|
import org.noear.solon.core.Plugin;
|
||||||
@ -8,7 +8,7 @@ import org.noear.solon.core.Plugin;
|
|||||||
public class XPlugin implements Plugin {
|
public class XPlugin implements Plugin {
|
||||||
|
|
||||||
public void start(AppContext context) throws Throwable {
|
public void start(AppContext context) throws Throwable {
|
||||||
context.beanMake(MilvusProperties.class);
|
context.beanMake(MilvusPropertiesConfiguration.class);
|
||||||
context.beanMake(MilvusInit.class);
|
context.beanMake(MilvusInit.class);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -12,7 +12,7 @@ import java.util.List;
|
|||||||
@Data
|
@Data
|
||||||
@Inject("${milvus}") //see https://solon.noear.org/article/326
|
@Inject("${milvus}") //see https://solon.noear.org/article/326
|
||||||
@Configuration
|
@Configuration
|
||||||
public class MilvusProperties {
|
public class MilvusPropertiesConfiguration {
|
||||||
private boolean enable;
|
private boolean enable;
|
||||||
private String uri;
|
private String uri;
|
||||||
private String dbName;
|
private String dbName;
|
||||||
@ -20,4 +20,5 @@ public class MilvusProperties {
|
|||||||
private String password;
|
private String password;
|
||||||
private String token;
|
private String token;
|
||||||
private List<String> packages;
|
private List<String> packages;
|
||||||
|
private boolean openLog;
|
||||||
}
|
}
|
||||||
@ -1,9 +1,12 @@
|
|||||||
package org.dromara.solon.service;
|
package org.dromara.solon.service;
|
||||||
|
|
||||||
import org.dromara.milvus.plus.service.AbstractMilvusClientBuilder;
|
|
||||||
import org.dromara.solon.entity.MilvusProperties;
|
|
||||||
import io.milvus.v2.client.MilvusClientV2;
|
import io.milvus.v2.client.MilvusClientV2;
|
||||||
import org.noear.solon.annotation.*;
|
import org.dromara.milvus.plus.log.LogLevelController;
|
||||||
|
import org.dromara.milvus.plus.model.MilvusProperties;
|
||||||
|
import org.dromara.milvus.plus.service.AbstractMilvusClientBuilder;
|
||||||
|
import org.dromara.solon.entity.MilvusPropertiesConfiguration;
|
||||||
|
import org.noear.solon.annotation.Bean;
|
||||||
|
import org.noear.solon.annotation.Configuration;
|
||||||
import org.noear.solon.core.bean.LifecycleBean;
|
import org.noear.solon.core.bean.LifecycleBean;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
|
|
||||||
@ -12,10 +15,12 @@ public class MilvusInit extends AbstractMilvusClientBuilder implements Lifecycle
|
|||||||
|
|
||||||
//see https://solon.noear.org/article/324
|
//see https://solon.noear.org/article/324
|
||||||
@Bean
|
@Bean
|
||||||
public MilvusClientV2 init(MilvusProperties milvusProperties) {
|
public MilvusClientV2 init(MilvusPropertiesConfiguration milvusPropertiesConfiguration) {
|
||||||
org.dromara.milvus.plus.model.MilvusProperties milvusProperties1 = new org.dromara.milvus.plus.model.MilvusProperties();
|
printBanner();
|
||||||
BeanUtils.copyProperties(milvusProperties, milvusProperties1);
|
LogLevelController.setLoggingEnabledForPackage("org.dromara.milvus.plus", milvusPropertiesConfiguration.isOpenLog());
|
||||||
super.setProperties(milvusProperties1);
|
MilvusProperties milvusProperties = new MilvusProperties();
|
||||||
|
BeanUtils.copyProperties(milvusPropertiesConfiguration, milvusProperties);
|
||||||
|
super.setProperties(milvusProperties);
|
||||||
super.initialize();
|
super.initialize();
|
||||||
return getClient();
|
return getClient();
|
||||||
}
|
}
|
||||||
@ -28,4 +33,14 @@ public class MilvusInit extends AbstractMilvusClientBuilder implements Lifecycle
|
|||||||
public void stop() throws Throwable {
|
public void stop() throws Throwable {
|
||||||
super.close();
|
super.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void printBanner() {
|
||||||
|
String banner =
|
||||||
|
" __ __ _ _ ____ _ \n" +
|
||||||
|
" | \\/ (_) |_ ___ _ ___ | _ \\| |_ _ ___ \n" +
|
||||||
|
" | |\\/| | | \\ \\ / / | | / __| | |_) | | | | / __|\n" +
|
||||||
|
" | | | | | |\\ V /| |_| \\__ \\ | __/| | |_| \\__ \\\n" +
|
||||||
|
" |_| |_|_|_| \\_/ \\__,_|___/ |_| |_|\\__,_|___/\n\n";
|
||||||
|
System.out.println(banner);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user