Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
guoyuanlu committed Oct 22, 2024
1 parent f8f8f17 commit a6664e1
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 1 deletion.
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
@@ -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 a6664e1

Please sign in to comment.