From 93a0822649673d6d5c8c055665b3f2f540aca3b1 Mon Sep 17 00:00:00 2001 From: poison <1650652506@qq.com> Date: Thu, 7 Dec 2023 16:50:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=80=E4=BA=9B=E4=B8=9C?= =?UTF-8?q?=E8=A5=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 1 + .../base/easylog/EasyLogAutoConfigure.java | 2 +- .../easylog/config/EasyLogProperties.java | 13 +++++- .../easylog/constants/PatternConstants.java | 14 +++++++ .../our/base/easylog/es/EsPushLogImpl.java | 2 +- .../easylog/logback/LogbackInitializer.java | 42 ++++++++++++++----- .../logback/appender/PushLogAppender.java | 20 +++++++-- src/main/resources/META-INF/spring.factories | 4 +- 8 files changed, 80 insertions(+), 18 deletions(-) create mode 100644 src/main/java/com/our/base/easylog/constants/PatternConstants.java diff --git a/pom.xml b/pom.xml index f54828d..ef8029f 100644 --- a/pom.xml +++ b/pom.xml @@ -61,6 +61,7 @@ provided + diff --git a/src/main/java/com/our/base/easylog/EasyLogAutoConfigure.java b/src/main/java/com/our/base/easylog/EasyLogAutoConfigure.java index 3b846fb..5a6c249 100644 --- a/src/main/java/com/our/base/easylog/EasyLogAutoConfigure.java +++ b/src/main/java/com/our/base/easylog/EasyLogAutoConfigure.java @@ -38,7 +38,7 @@ public class EasyLogAutoConfigure { @Autowired private EasyLogProperties easyLogProperties; - + @Order(-1) @Bean @ConditionalOnProperty(prefix = "easy-log", name = "storage", havingValue = "es") @ConditionalOnMissingBean(IPushLog.class) diff --git a/src/main/java/com/our/base/easylog/config/EasyLogProperties.java b/src/main/java/com/our/base/easylog/config/EasyLogProperties.java index ddaccd9..3967ccc 100644 --- a/src/main/java/com/our/base/easylog/config/EasyLogProperties.java +++ b/src/main/java/com/our/base/easylog/config/EasyLogProperties.java @@ -1,6 +1,7 @@ package com.our.base.easylog.config; import com.our.base.easylog.config.es.EsProperties; +import com.our.base.easylog.constants.PatternConstants; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; @@ -13,8 +14,18 @@ import org.springframework.boot.context.properties.ConfigurationProperties; @Data @ConfigurationProperties(prefix = "easy-log") public class EasyLogProperties { - private boolean enable; + /** + * 是否启用 + */ + private boolean enable=false; + /** + * 存储方式 + */ private String storage; + /** + * 格式化 + */ + private String pattern= PatternConstants.LOGBACK_DEFAULT_PATTERN; /** * es配置 diff --git a/src/main/java/com/our/base/easylog/constants/PatternConstants.java b/src/main/java/com/our/base/easylog/constants/PatternConstants.java new file mode 100644 index 0000000..4d0cb20 --- /dev/null +++ b/src/main/java/com/our/base/easylog/constants/PatternConstants.java @@ -0,0 +1,14 @@ +package com.our.base.easylog.constants; + +/** + * @author wuyang + * @Description 日志格式常量 + * @date 2023/12/7 + **/ +public class PatternConstants { + /** + * logback默认格式 + */ + public static final String LOGBACK_DEFAULT_PATTERN="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n"; + +} diff --git a/src/main/java/com/our/base/easylog/es/EsPushLogImpl.java b/src/main/java/com/our/base/easylog/es/EsPushLogImpl.java index ce711ac..536955f 100644 --- a/src/main/java/com/our/base/easylog/es/EsPushLogImpl.java +++ b/src/main/java/com/our/base/easylog/es/EsPushLogImpl.java @@ -18,7 +18,7 @@ public class EsPushLogImpl implements IPushLog { } @Override public void pushLog(LogInfo logInfo) { - System.out.println(logInfo.getLog()); + System.out.println("已经可以推送了....."); } } 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 2c4c994..ae83f57 100644 --- a/src/main/java/com/our/base/easylog/logback/LogbackInitializer.java +++ b/src/main/java/com/our/base/easylog/logback/LogbackInitializer.java @@ -2,13 +2,21 @@ package com.our.base.easylog.logback; import ch.qos.logback.classic.Level; import ch.qos.logback.classic.LoggerContext; +import ch.qos.logback.classic.PatternLayout; import ch.qos.logback.classic.joran.JoranConfigurator; +import ch.qos.logback.classic.layout.TTLLLayout; 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.IPushLog; +import com.our.base.easylog.config.EasyLogProperties; +import com.our.base.easylog.config.es.EsProperties; +import com.our.base.easylog.constants.PatternConstants; +import com.our.base.easylog.es.EsPushLogImpl; import com.our.base.easylog.logback.appender.PushLogAppender; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; +import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @@ -16,23 +24,27 @@ import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ConfigurableApplicationContext; import java.net.URL; +import java.util.Objects; /** * @author poison * @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)); + EasyLogProperties easyLogProperties=configurableApplicationContext.getEnvironment().getProperty("easy-log", EasyLogProperties.class); + if (Objects.isNull(easyLogProperties)){ + easyLogProperties=new EasyLogProperties(); + } + - if (!enable){ + if (!easyLogProperties.isEnable()){ return; } try { @@ -62,16 +74,26 @@ public class LogbackInitializer implements ApplicationContextInitializer { catch (JoranException exception){ log.error("没有正确加载logback配置文件:"+ exception.getMessage()); } - - LayoutWrappingEncoder encoder = new LayoutWrappingEncoder<>(); - encoder.setContext(loggerContext); - encoder.start(); + if ("es".equals(easyLogProperties.getStorage())){ + IPushLog pushLog=new EsPushLogImpl(new EsProperties()); + configurableApplicationContext.getBeanFactory().registerSingleton("pushLog", pushLog); + } + //TODO 此处应该读取配置文件的格式,配置文件格式应该写个默认 + PatternLayout layout = new PatternLayout(); + layout.setPattern(easyLogProperties.getPattern()); + layout.setContext(loggerContext); + layout.start(); PushLogAppender pushLogAppender = new PushLogAppender(); + pushLogAppender.setContext(loggerContext); + pushLogAppender.setName("logPush"); + pushLogAppender.setLayout(layout); pushLogAppender.start(); - ch.qos.logback.classic.Logger logger = loggerContext.getLogger("root"); + + ch.qos.logback.classic.Logger logger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME); logger.setLevel(Level.INFO); logger.addAppender(pushLogAppender); - pushLogAppender.setContext(loggerContext); + + } } diff --git a/src/main/java/com/our/base/easylog/logback/appender/PushLogAppender.java b/src/main/java/com/our/base/easylog/logback/appender/PushLogAppender.java index 25e4288..289faad 100644 --- a/src/main/java/com/our/base/easylog/logback/appender/PushLogAppender.java +++ b/src/main/java/com/our/base/easylog/logback/appender/PushLogAppender.java @@ -11,6 +11,8 @@ import com.our.base.easylog.entity.LogInfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import java.util.Map; + /** * 日志推送Appender * @@ -21,17 +23,29 @@ import org.springframework.stereotype.Component; public class PushLogAppender extends AppenderBase { @Autowired IPushLog pushLog; + private Layout layout; + @Override protected void append(ILoggingEvent eventLog) { + //拿到日志 - String log = eventLog.getFormattedMessage(); + String log=layout.doLayout(eventLog); //读取配置(根据配置的MDC信息做分词标签) - eventLog.getMDCPropertyMap(); + Map mdcPropertyMap = eventLog.getMDCPropertyMap(); + String message = eventLog.getMessage(); + eventLog.prepareForDeferredProcessing(); LogInfo logInfo = new LogInfo(); logInfo.setLog(log); - System.out.println(log); pushLog.pushLog(logInfo); } + + public Layout getLayout() { + return layout; + } + + public void setLayout(Layout layout) { + this.layout = layout; + } } diff --git a/src/main/resources/META-INF/spring.factories b/src/main/resources/META-INF/spring.factories index c433c81..6cf0b4d 100644 --- a/src/main/resources/META-INF/spring.factories +++ b/src/main/resources/META-INF/spring.factories @@ -1,3 +1,3 @@ +org.springframework.context.ApplicationContextInitializer=\com.our.base.easylog.logback.LogbackInitializer org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ -com.our.base.easylog.EasyLogAutoConfigure -org.springframework.context.ApplicationContextInitializer=\com.our.base.easylog.logback.LogbackInitializer \ No newline at end of file +com.our.base.easylog.EasyLogAutoConfigure \ No newline at end of file -- Gitee