From f67fcba56366d48c835ca5f191d05c563a945c72 Mon Sep 17 00:00:00 2001 From: poison <1650652506@qq.com> Date: Wed, 6 Dec 2023 19:17:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9logback=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96=E9=80=BB=E8=BE=91=E5=9C=A8=E9=85=8D=E7=BD=AEeasy-log?= =?UTF-8?q?=E5=90=AF=E7=94=A8=E7=9A=84=E6=97=B6=E5=80=99=E6=89=8D=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=EF=BC=8C=E5=B9=B6=E5=8A=A0=E5=85=A5logback?= =?UTF-8?q?=E7=B1=BB=E5=88=A4=E6=96=AD=EF=BC=8C=E8=BF=99=E4=B8=AA=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E5=8F=AF=E8=83=BD=E9=9C=80=E8=A6=81=E4=BF=AE=E6=AD=A3?= =?UTF-8?q?=E5=88=B0=E7=BB=9F=E4=B8=80=E4=B8=80=E4=B8=AA=E7=B1=BB=E5=8E=BB?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E6=97=A5=E5=BF=97=E7=BB=84=E4=BB=B6=E6=98=AF?= =?UTF-8?q?=E5=90=A6=E4=BE=9D=E8=B5=96=20=E8=AE=BE=E7=BD=AE=E6=89=80?= =?UTF-8?q?=E6=9C=89=E4=BE=9D=E8=B5=96=E4=B8=8D=E4=BC=A0=E9=80=92=EF=BC=8C?= =?UTF-8?q?=E5=9D=87=E9=9C=80=E8=A6=81=E8=B0=83=E7=94=A8=E6=96=B9=E8=87=AA?= =?UTF-8?q?=E5=B7=B1=E5=BC=95=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 20 +++++++++---------- .../easylog/logback/LogbackInitializer.java | 14 +++++++++++++ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/pom.xml b/pom.xml index 18ccdc4..f54828d 100644 --- a/pom.xml +++ b/pom.xml @@ -27,39 +27,39 @@ org.springframework.boot spring-boot-autoconfigure + provided ch.qos.logback logback-classic 1.2.3 + provided - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.boot - spring-boot-starter-logging - + + org.elasticsearch.client elasticsearch-rest-high-level-client 7.17.7 + provided org.springframework.boot spring-boot-starter-data-elasticsearch + provided org.elasticsearch elasticsearch 7.17.7 - compile - true + provided + org.projectlombok lombok + provided + diff --git a/src/main/java/com/our/base/easylog/logback/LogbackInitializer.java b/src/main/java/com/our/base/easylog/logback/LogbackInitializer.java index c52e712..2c4c994 100644 --- a/src/main/java/com/our/base/easylog/logback/LogbackInitializer.java +++ b/src/main/java/com/our/base/easylog/logback/LogbackInitializer.java @@ -7,9 +7,11 @@ import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.core.encoder.LayoutWrappingEncoder; import ch.qos.logback.core.joran.spi.JoranException; import com.our.base.easylog.logback.appender.PushLogAppender; +import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.slf4j.LoggerFactory; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ConfigurableApplicationContext; @@ -20,13 +22,25 @@ import java.net.URL; * @Description logback 初始化实现 (在spring boot 初始化之前) * @date 2023/12/6 **/ +@ConditionalOnProperty(prefix = "easy-log", name = "enable", matchIfMissing = true) @ConditionalOnClass(name = "ch.qos.logback.classic.LoggerContext") @Slf4j public class LogbackInitializer implements ApplicationContextInitializer { + @Override public void initialize(ConfigurableApplicationContext configurableApplicationContext) { + boolean enable= Boolean.TRUE.equals(configurableApplicationContext.getEnvironment().getProperty("easy-log.enable", Boolean.class)); + if (!enable){ + return; + } + try { + Class.forName("ch.qos.logback.classic.LoggerContext"); + } catch (ClassNotFoundException e) { + log.info("没有logback依赖"); + return; + } LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); JoranConfigurator configurator = new JoranConfigurator(); -- Gitee