Skip to content

Commit

Permalink
Merge pull request #216 from shiyindaxiaojie/feature
Browse files Browse the repository at this point in the history
Feature
  • Loading branch information
shiyindaxiaojie authored Oct 22, 2024
2 parents 868ac41 + a6664e1 commit 7d7a6f6
Show file tree
Hide file tree
Showing 13 changed files with 233 additions and 31 deletions.
13 changes: 13 additions & 0 deletions eden-components/eden-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
<log4j2.version>2.20.0</log4j2.version>
<logback-extensions.version>0.1.5</logback-extensions.version>
<logstash-logback-encoder.version>6.5</logstash-logback-encoder.version>
<loki-logback-appender.version>1.6.0-m1</loki-logback-appender.version>

<!-- 监控组件 -->
<prometheus.version>0.16.0</prometheus.version>
Expand Down Expand Up @@ -622,6 +623,11 @@
<artifactId>eden-logstash-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.github.shiyindaxiaojie</groupId>
<artifactId>eden-loki-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.github.shiyindaxiaojie</groupId>
<artifactId>eden-metrics-spring-boot-starter</artifactId>
Expand Down Expand Up @@ -2023,6 +2029,13 @@
</exclusion>
</exclusions>
</dependency>
<!-- Loki -->
<dependency>
<groupId>com.github.loki4j</groupId>
<artifactId>loki-logback-appender</artifactId>
<version>${loki-logback-appender.version}</version>
</dependency>


<!-- 监控组件 -->
<!-- Spring Boot Admin -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
@Getter
public class MaskingConfig {

private String type = "chars-scan";
public static final String CHARS_SCAN = "chars-scan";

private String type = CHARS_SCAN;

private final CharsScan charsScan = new CharsScan();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.ylzl.eden.data.filter.masking.log4j2;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.Configuration;
Expand Down Expand Up @@ -74,12 +75,10 @@ public static MaskingStringLayout createLayout(@PluginConfiguration final Config
@PluginAttribute(value = "charset", defaultString = "UTF-8") String charset,
@PluginAttribute(value = "pattern", defaultString = PatternLayout.DEFAULT_CONVERSION_PATTERN) String pattern,
@PluginAttribute(value = "type") String type,
@PluginAttribute(value = "prefix") String prefix,
@PluginAttribute(value = "scanList") String scanList,
@PluginAttribute(value = "replaceList") String replaceList,
@PluginAttribute(value = "defaultReplace") String defaultReplace,
@PluginAttribute(value = "replaceHash") String replaceHash,
@PluginAttribute(value = "whiteList") String whiteList) {
@PluginAttribute(value = "strategies") String strategies,
@PluginAttribute(value = "replacement") String replacement,
@PluginAttribute(value = "hash") String hash,
@PluginAttribute(value = "whitelist") String whitelist) {

PatternParser patternParser = null;
if (config != null) {
Expand All @@ -90,26 +89,23 @@ public static MaskingStringLayout createLayout(@PluginConfiguration final Config
}

MaskingConfig maskingConfig = new MaskingConfig();
if (type != null) {
if (StringUtils.isNotBlank(type)) {
maskingConfig.setType(type);
}
if (prefix != null) {
maskingConfig.getCharsScan().setPrefix(prefix);
}
if (scanList != null) {
maskingConfig.getCharsScan().setScanList(scanList);
}
if (replaceList != null) {
maskingConfig.getCharsScan().setReplaceList(replaceList);
}
if (defaultReplace != null) {
maskingConfig.getCharsScan().setDefaultReplace(defaultReplace);
}
if (replaceHash != null) {
maskingConfig.getCharsScan().setReplaceHash(replaceHash);
}
if (whiteList != null) {
maskingConfig.getCharsScan().setWhiteList(whiteList);
if (MaskingConfig.CHARS_SCAN.equals(maskingConfig.getType())) {
if (StringUtils.isNotBlank(strategies)) {
maskingConfig.getCharsScan().setReplaceList(strategies);
maskingConfig.getCharsScan().setScanList(strategies);
}
if (StringUtils.isNotBlank(replacement)) {
maskingConfig.getCharsScan().setDefaultReplace(replacement);
}
if (StringUtils.isNotBlank(hash)) {
maskingConfig.getCharsScan().setReplaceHash(hash);
}
if (StringUtils.isNotBlank(whitelist)) {
maskingConfig.getCharsScan().setWhiteList(whitelist);
}
}

MaskingStringLayout layout = new MaskingStringLayout(Charset.forName(charset), maskingConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
Expand All @@ -39,6 +40,7 @@
*/
@ConditionalOnBean(StringRedisTemplate.class)
@ConditionalOnProperty(value = RedisTimeToLiveIdempotentAutoConfiguration.ENABLED, havingValue = "true")
@AutoConfigureAfter(TimeToLiveIdempotentAutoConfiguration.class)
@RequiredArgsConstructor
@Import(TimeToLiveIdempotentAspectRegistrar.class)
@Slf4j
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Auto Configure
#org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
# org.ylzl.eden.idempotent.spring.boot.autoconfigure.TimeToLiveIdempotentAutoConfiguration,\
# org.ylzl.eden.idempotent.spring.boot.autoconfigure.TokenIdempotentAutoConfiguration,\
# org.ylzl.eden.idempotent.spring.boot.autoconfigure.RedisTimeToLiveIdempotentAutoConfiguration,\
# org.ylzl.eden.idempotent.spring.boot.autoconfigure.RedisTokenIdempotentAutoConfiguration
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.ylzl.eden.idempotent.spring.boot.autoconfigure.TimeToLiveIdempotentAutoConfiguration,\
org.ylzl.eden.idempotent.spring.boot.autoconfigure.TokenIdempotentAutoConfiguration,\
org.ylzl.eden.idempotent.spring.boot.autoconfigure.RedisTimeToLiveIdempotentAutoConfiguration,\
org.ylzl.eden.idempotent.spring.boot.autoconfigure.RedisTokenIdempotentAutoConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2012-2019 the original author or authors.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ https://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.github.shiyindaxiaojie</groupId>
<artifactId>eden-spring-boot-starters</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>eden-loki-spring-boot-starter</artifactId>
<packaging>jar</packaging>
<name>eden-loki-spring-boot-starter</name>
<description>Spring Boot Starter For Loki</description>

<dependencies>
<!-- 开发框架 -->
<dependency>
<groupId>io.github.shiyindaxiaojie</groupId>
<artifactId>eden-spring-boot</artifactId>
</dependency>
<dependency>
<groupId>io.github.shiyindaxiaojie</groupId>
<artifactId>eden-spring-integration</artifactId>
</dependency>

<!-- 开发组件 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
1 change: 1 addition & 0 deletions eden-components/eden-spring-boot-starters/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<module>eden-leaf-spring-boot-starter</module>
<module>eden-liquibase-spring-boot-starter</module>
<module>eden-logstash-spring-boot-starter</module>
<module>eden-loki-spring-boot-starter</module>
<module>eden-metrics-spring-boot-starter</module>
<module>eden-mongobee-spring-boot-starter</module>
<module>eden-mongodb-spring-boot-starter</module>
Expand Down
23 changes: 23 additions & 0 deletions eden-components/eden-spring-integration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@
<dependency>
<groupId>io.github.shiyindaxiaojie</groupId>
<artifactId>eden-spring-framework</artifactId>
<exclusions>
<exclusion>
<artifactId>logback-classic</artifactId>
<groupId>ch.qos.logback</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.github.shiyindaxiaojie</groupId>
Expand Down Expand Up @@ -347,6 +353,23 @@
<optional>true</optional>
</dependency>

<!-- Log4j2 -->
<dependency>
<groupId>com.github.loki4j</groupId>
<artifactId>loki-logback-appender</artifactId>
<optional>true</optional>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</exclusion>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- Spock -->
<dependency>
<groupId>org.spockframework</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package org.ylzl.eden.spring.integration.loki.integration.log4j2;

import com.dianping.cat.Cat;
import com.dianping.cat.message.Message;
import lombok.SneakyThrows;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.*;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.appender.AppenderLoggingException;
import org.apache.logging.log4j.core.config.Property;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
import org.apache.logging.log4j.core.config.plugins.PluginElement;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.core.layout.JsonLayout;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.apache.logging.log4j.core.util.Booleans;
import org.ylzl.eden.commons.env.Charsets;
import org.ylzl.eden.spring.integration.cat.CatConstants;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;

/**
* Log4j2 附加器
*
* @author <a href="mailto:shiyindaxiaojie@gmail.com">gyl</a>
* @since 2.4.13
*/
@Plugin(name = Log4j2LokiAppender.NAME, category = Core.CATEGORY_NAME, elementType = Appender.ELEMENT_TYPE, printObject = true)
public class Log4j2LokiAppender extends AbstractAppender {

public static final String NAME = "Loki";

private static final int DEFAULT_MAX_LENGTH = 500;

private final Level level;

private final Integer maxLength;

public Log4j2LokiAppender(String name, Filter filter, Layout<? extends Serializable> layout,
boolean ignoreExceptions, Property[] properties, Level level, Integer maxLength) {
super(name, filter, layout, ignoreExceptions, properties);
this.level = level;
this.maxLength = maxLength != null && maxLength > 0 ? maxLength : DEFAULT_MAX_LENGTH;
}

@Override
public void append(LogEvent event) {
try {
if (event.getLevel().isMoreSpecificThan(level)) {
// TODO
}
} catch (Exception ex) {
if (!ignoreExceptions()) {
throw new AppenderLoggingException(ex);
}
}
}

@SneakyThrows(UnsupportedEncodingException.class)
private void tryAppend(final LogEvent event) {
Layout<? extends Serializable> layout = getLayout();
byte[] data;
if (layout instanceof JsonLayout) {
final byte[] header = layout.getHeader();
final byte[] body = layout.toByteArray(event);
data = new byte[header.length + body.length];
System.arraycopy(header, 0, data, 0, header.length);
System.arraycopy(body, 0, data, header.length, body.length);
} else {
data = layout.toByteArray(event);
}
String message = data.length > maxLength ?
new String(data, 0, maxLength, Charsets.UTF_8_NAME) :
new String(data, Charsets.UTF_8_NAME);
Cat.logEvent(CatConstants.TYPE_LOG_LOG4J2, event.getLevel().name(), Message.SUCCESS, message);
}

@PluginFactory
public static Log4j2LokiAppender build(
@PluginAttribute("name") String name,
@PluginElement("Filter") Filter filter,
@PluginElement("Layout") Layout<? extends Serializable> layout,
@PluginAttribute("ignoreExceptions") String ignore,
@PluginAttribute("level") Level level,
@PluginAttribute("maxLength") Integer maxLength) {

if (name == null) {
return null;
}
if (layout == null) {
layout = PatternLayout.createDefaultLayout();
}
if (level == null) {
level = Level.ERROR;
}
if (maxLength == null) {
maxLength = DEFAULT_MAX_LENGTH;
}
boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
return new Log4j2LokiAppender(name, filter, layout, ignoreExceptions, null, level, maxLength);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package org.ylzl.eden.spring.integration.loki.integration.log4j2;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package org.ylzl.eden.spring.integration.loki.integration.logback;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package org.ylzl.eden.spring.integration.loki;
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
<module>eden-components</module>
<module>eden-plugins</module>
<module>eden-tests</module>
</modules>
<module>eden-components/eden-spring-boot-starters/eden-loki-spring-boot-starter</module>
</modules>

<developers>
<developer>
Expand Down

0 comments on commit 7d7a6f6

Please sign in to comment.