Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package group.rxcloud.capa.examples.log;
package group.rxcloud.capa.examples.telemetry;

import lombok.extern.slf4j.Slf4j;

Expand All @@ -30,6 +30,11 @@
public class DemoLog {

public static void main(String[] args) {
log.info("test");
try {
log.info("test");
}catch (Exception e){
System.out.println();
}

}
}
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -414,5 +414,4 @@
</plugin>
</plugins>
</build>

</project>
26 changes: 25 additions & 1 deletion sdk-component/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
<kotlin-stdlib.version>1.4.10</kotlin-stdlib.version>
<log4j.version>2.8.2</log4j.version>
<logback.version>1.1.7</logback.version>
<guava-version>19.0</guava-version>
<commons-lang3-version>3.11</commons-lang3-version>
<commons-collections-version>3.2.2</commons-collections-version>
</properties>

<dependencies>
Expand Down Expand Up @@ -98,7 +101,13 @@
<optional>true</optional>
<version>${logback.version}</version>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<!-- log4j-slf4j-impl and logback-classic cannot exist at the same time. -->
<optional>true</optional>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
Expand All @@ -109,6 +118,21 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava-version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3-version}</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>${commons-collections-version}</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package group.rxcloud.capa.component.log.agent;

import group.rxcloud.capa.component.log.enums.CapaLogLevel;
import group.rxcloud.capa.component.log.manager.LogManager;
import group.rxcloud.capa.infrastructure.CapaClassLoader;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.Filter;
Expand All @@ -28,6 +30,7 @@
import org.apache.logging.log4j.core.config.plugins.PluginFactory;

import java.io.Serializable;
import java.util.Optional;

/**
* The abstract log4j appender. Extend this and provide your specific impl.
Expand Down Expand Up @@ -97,7 +100,12 @@ public static CapaLog4jAppender buildCapaLog4jAppender() {

@Override
public void append(LogEvent event) {
logAppender.appendLog(event);
if (event != null && event.getLevel() != null) {
Optional<CapaLogLevel> capaLogLevel = CapaLogLevel.toCapaLogLevel(event.getLevel().name());
if (capaLogLevel.isPresent() && LogManager.whetherLogsCanBeOutput(capaLogLevel.get())) {
logAppender.appendLog(event);
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@
*/
package group.rxcloud.capa.component.log.agent;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.UnsynchronizedAppenderBase;
import group.rxcloud.capa.component.log.enums.CapaLogLevel;
import group.rxcloud.capa.component.log.manager.LogManager;
import group.rxcloud.capa.infrastructure.CapaClassLoader;

import java.util.Optional;

/**
* The agent of the logback impl.
*/
public class CapaLogbackAppenderAgent<EVENT> extends UnsynchronizedAppenderBase<EVENT> {
public class CapaLogbackAppenderAgent extends UnsynchronizedAppenderBase<ILoggingEvent> {

/**
* The log component type.
Expand Down Expand Up @@ -58,20 +63,25 @@ public static CapaLogbackAppender buildCapaLogbackAppender() {
* @param event The log event.
*/
@Override
protected void append(EVENT event) {
logbackAppender.appendLog(event);
protected void append(ILoggingEvent event) {
if (event != null && event.getLevel() != null) {
Optional<CapaLogLevel> capaLogLevel = CapaLogLevel.toCapaLogLevel(event.getLevel().levelStr);
if (capaLogLevel.isPresent() && LogManager.whetherLogsCanBeOutput(capaLogLevel.get())) {
logbackAppender.appendLog(event);
}
}
}

/**
* The abstract api of the logback appender impl.Implement this and provide your specific impl.
*/
public interface CapaLogbackAppender<EVENT> {
public interface CapaLogbackAppender {

/**
* Deal with the log.
*
* @param event The log event.
*/
void appendLog(EVENT event);
void appendLog(ILoggingEvent event);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://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.
*/
package group.rxcloud.capa.component.log.configuration;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import group.rxcloud.capa.infrastructure.hook.ConfigurationHooks;
import group.rxcloud.capa.infrastructure.hook.Mixer;
import group.rxcloud.cloudruntimes.domain.core.configuration.SubConfigurationResp;
import group.rxcloud.cloudruntimes.utils.TypeRef;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import reactor.core.publisher.Flux;

import java.util.Map;
import java.util.Optional;

/**
* Log switch configuration
*/
public class LogSwitchConfiguration {

/**
* Capa configuration hooks.
*/
private static final Optional<ConfigurationHooks> configurationHooks;
/**
* Log switch config file name.
*/
private static final String LOGS_SWITCH_CONFIG_FILE_NAME = "log-level-switch.properties";
/**
* Log switch config's value.
*/
private static Map<String, Boolean> logsSwitchConfig = Maps.newHashMap();

/**
* Init configuration hooks and subscribe configuration.
*/
static {
configurationHooks = Mixer.configurationHooksNullable();
configurationHooks.ifPresent(configurationHooks -> {
subscribeConfiguration(configurationHooks);
});
}

/**
* Subscribe configuration.
*
* @param configurationHooks
*/
private static void subscribeConfiguration(ConfigurationHooks configurationHooks) {
String storeName = configurationHooks.registryStoreNames().get(0);
String appId = configurationHooks.defaultConfigurationAppId();
Flux<SubConfigurationResp<Map>> configFlux = configurationHooks.subscribeConfiguration(
storeName,
appId,
Lists.newArrayList(LOGS_SWITCH_CONFIG_FILE_NAME),
null,
StringUtils.EMPTY,
StringUtils.EMPTY,
TypeRef.get(Map.class));
configFlux.subscribe(resp -> {
if (CollectionUtils.isNotEmpty(resp.getItems())) {
logsSwitchConfig = resp.getItems().get(0).getContent();
}
});
}

/**
* Get the log switch config's values.
*
* @return the log switch config's values.
*/
public static Map<String, Boolean> getLogsSwitchConfig() {
return logsSwitchConfig;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://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.
*/
package group.rxcloud.capa.component.log.enums;

import java.util.Arrays;
import java.util.Optional;

/**
* Capa log level.
*/
public enum CapaLogLevel {
/**
* Standard order of log priorities:ALL,TRACE,DEBUG,INFO,WARN,ERROR,FATAL,OFF
*/
ALL(1, "ALL"),
TRACE(2, "TRACE"),
DEBUG(3, "DEBUG"),
INFO(4, "INFO"),
WARN(5, "WARN"),
ERROR(6, "ERROR"),
FATAL(7, "FATAL"),
OFF(8, "OFF");

final int level;
final String levelName;

CapaLogLevel(int level, String levelName) {
this.level = level;
this.levelName = levelName;
}

/**
* Convert logLevelArg to {@link CapaLogLevel}
*
* @param logLevelArg
* @return
*/
public static Optional<CapaLogLevel> toCapaLogLevel(String logLevelArg) {
return Arrays.stream(CapaLogLevel.values())
.filter(logLevel -> logLevel.levelName.equalsIgnoreCase(logLevelArg))
.findAny();
}

/**
* Get level.
*
* @return
*/
public int getLevel() {
return level;
}

public String getLevelName() {
return levelName;
}
}
Loading