This commit is contained in:
shuhongfan
2023-09-04 16:40:17 +08:00
commit cf5ac25c14
8267 changed files with 1305066 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>sentinel-demo</artifactId>
<groupId>com.alibaba.csp</groupId>
<version>1.8.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>sentinel-demo-log-logback</artifactId>
<dependencies>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-common</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,91 @@
/*
* Copyright 1999-2019 Alibaba Group Holding Ltd.
*
* 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.
*/
package com.alibaba.csp.sentinel.demo.log.logback;
import com.alibaba.csp.sentinel.transport.log.CommandCenterLog;
import com.alibaba.csp.sentinel.log.LogTarget;
import com.alibaba.csp.sentinel.log.Logger;
import org.slf4j.LoggerFactory;
/**
* This class is a demo shows how to create a customized logger implementation.
*
* <ul>
* <li>1. Create a class which implements the {@link Logger} SPI interface</li>
* <li>2. Use a {@link LogTarget} to specify the log type</li>
* <li>3. Implement your own method </li>
* <li>4. Add your logger in {@code com.alibaba.csp.sentinel.log.Logger} file which is stored in
* {@code resources/META-INF/services/} directory </li>
* </ul>
*
* @author xue8
*/
@LogTarget(value = CommandCenterLog.LOGGER_NAME)
public class CommandCenterLogLoggerImpl implements Logger {
private final org.slf4j.Logger logger = LoggerFactory.getLogger(CommandCenterLog.LOGGER_NAME);
@Override
public void info(String format, Object... arguments) {
logger.info(format, arguments);
}
@Override
public void info(String msg, Throwable e) {
logger.info(msg, e);
}
@Override
public void warn(String format, Object... arguments) {
logger.warn(format, arguments);
}
@Override
public void warn(String msg, Throwable e) {
logger.warn(msg, e);
}
@Override
public void trace(String format, Object... arguments) {
logger.trace(format, arguments);
}
@Override
public void trace(String msg, Throwable e) {
logger.trace(msg, e);
}
@Override
public void debug(String format, Object... arguments) {
logger.debug(format, arguments);
}
@Override
public void debug(String msg, Throwable e) {
logger.debug(msg, e);
}
@Override
public void error(String format, Object... arguments) {
logger.error(format, arguments);
}
@Override
public void error(String msg, Throwable e) {
logger.error(msg, e);
}
}

View File

@@ -0,0 +1,92 @@
/*
* Copyright 1999-2019 Alibaba Group Holding Ltd.
*
* 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.
*/
package com.alibaba.csp.sentinel.demo.log.logback;
import com.alibaba.csp.sentinel.log.LogTarget;
import com.alibaba.csp.sentinel.log.Logger;
import com.alibaba.csp.sentinel.log.RecordLog;
import org.slf4j.LoggerFactory;
/**
* This class is a demo shows how to create a customized logger implementation.
*
* <ul>
* <li>1. Create a class which implements the {@link Logger} SPI interface</li>
* <li>2. Use a {@link LogTarget} to specify the log type</li>
* <li>3. Implement your own method </li>
* <li>4. Add your logger in {@code com.alibaba.csp.sentinel.log.Logger} file which is stored in
* {@code resources/META-INF/services/} directory </li>
* </ul>
*
* @author xue8
*/
@LogTarget(value = RecordLog.LOGGER_NAME)
public class RecordLogLoggerImpl implements Logger {
private final org.slf4j.Logger logger = LoggerFactory.getLogger(RecordLog.LOGGER_NAME);
@Override
public void info(String format, Object... arguments) {
logger.info(format, arguments);
}
@Override
public void info(String msg, Throwable e) {
logger.info(msg, e);
}
@Override
public void warn(String format, Object... arguments) {
logger.warn(format, arguments);
}
@Override
public void warn(String msg, Throwable e) {
logger.warn(msg, e);
}
@Override
public void trace(String format, Object... arguments) {
logger.trace(format, arguments);
}
@Override
public void trace(String msg, Throwable e) {
logger.trace(msg, e);
}
@Override
public void debug(String format, Object... arguments) {
logger.debug(format, arguments);
}
@Override
public void debug(String msg, Throwable e) {
logger.debug(msg, e);
}
@Override
public void error(String format, Object... arguments) {
logger.error(format, arguments);
}
@Override
public void error(String msg, Throwable e) {
logger.error(msg, e);
}
}

View File

@@ -0,0 +1,2 @@
com.alibaba.csp.sentinel.demo.log.logback.RecordLogLoggerImpl
com.alibaba.csp.sentinel.demo.log.logback.CommandCenterLogLoggerImpl

View File

@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="false" scanPeriod="60000" debug="false">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%-5level %logger - %msg%n</pattern>
</layout>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>sentinel-record.log</file>
<append>true</append>
<encoder>
<pattern>%-5level %logger - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE2" class="ch.qos.logback.core.FileAppender">
<file>sentinel-command-center.log</file>
<append>true</append>
<encoder>
<pattern>%-5level %logger - %msg%n</pattern>
</encoder>
</appender>
<logger name="sentinelRecordLogger" level="trace">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</logger>
<logger name="sentinelCommandCenterLogger" level="trace">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE2" />
</logger>
</configuration>

View File

@@ -0,0 +1,116 @@
/*
* Copyright 1999-2019 Alibaba Group Holding Ltd.
*
* 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.
*/
package com.alibaba.csp.sentinel.demo.log.logback;
import com.alibaba.csp.sentinel.transport.log.CommandCenterLog;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.SystemOutRule;
/**
* @author xue8
*/
public class CommandCenterLogTest {
@Rule
public SystemOutRule log = new SystemOutRule().enableLog();
@Test
public void testLog() {
CommandCenterLog.info("init");
log.clearLog();
int count = 0;
// info test
while (count++ < 1000) {
log.clearLog();
CommandCenterLog.info("Count {}", count);
String str = String.format("INFO sentinelCommandCenterLogger - Count %d" + System.lineSeparator(), count);
Assert.assertEquals(str, log.getLog());
}
// warn test
while (count++ < 2000) {
log.clearLog();
CommandCenterLog.warn("Count {}", count);
String str = String.format("WARN sentinelCommandCenterLogger - Count %d" + System.lineSeparator(), count);
Assert.assertEquals(str, log.getLog());
}
// trace test
while (count++ < 3000) {
log.clearLog();
CommandCenterLog.trace("Count {}", count);
String str = String.format("TRACE sentinelCommandCenterLogger - Count %d" + System.lineSeparator(), count);
Assert.assertEquals(str, log.getLog());
}
// debug test
while (count++ < 4000) {
log.clearLog();
CommandCenterLog.debug("Count {}", count);
String str = String.format("DEBUG sentinelCommandCenterLogger - Count %d" + System.lineSeparator(), count);
Assert.assertEquals(str, log.getLog());
}
// test error
while (count++ < 5000) {
log.clearLog();
CommandCenterLog.error("Count {}", count);
String str = String.format("ERROR sentinelCommandCenterLogger - Count %d" + System.lineSeparator(), count);
Assert.assertEquals(str, log.getLog());
}
}
@Test
public void testLogException() {
CommandCenterLog.info("init");
log.clearLog();
Exception e = new Exception("ex");
// info test
CommandCenterLog.info("Error", e);
// split the log for test
String[] logSplit = log.getLog().split(System.lineSeparator());
Assert.assertEquals("INFO sentinelCommandCenterLogger - Error", logSplit[0]);
// warn test
log.clearLog();
CommandCenterLog.warn("Error", e);
logSplit = log.getLog().split(System.lineSeparator());
Assert.assertEquals("WARN sentinelCommandCenterLogger - Error", logSplit[0]);
// trace test
log.clearLog();
CommandCenterLog.trace("Error", e);
logSplit = log.getLog().split(System.lineSeparator());
Assert.assertEquals("TRACE sentinelCommandCenterLogger - Error", logSplit[0]);
// debug test
log.clearLog();
CommandCenterLog.debug("Error", e);
logSplit = log.getLog().split(System.lineSeparator());
Assert.assertEquals("DEBUG sentinelCommandCenterLogger - Error", logSplit[0]);
// error test
log.clearLog();
CommandCenterLog.error("Error", e);
logSplit = log.getLog().split(System.lineSeparator());
Assert.assertEquals("ERROR sentinelCommandCenterLogger - Error", logSplit[0]);
}
}

View File

@@ -0,0 +1,115 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* 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
*
* 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 com.alibaba.csp.sentinel.demo.log.logback;
import com.alibaba.csp.sentinel.log.RecordLog;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.SystemOutRule;
/**
* @author xue8
*/
public class RecordLogTest {
@Rule
public SystemOutRule log = new SystemOutRule().enableLog();
@Test
public void testLog() {
RecordLog.info("init");
log.clearLog();
int count = 0;
// info test
while (count++ < 1000) {
log.clearLog();
RecordLog.info("Count {}", count);
String str = String.format("INFO sentinelRecordLogger - Count %d" + System.lineSeparator(), count);
Assert.assertEquals(str, log.getLog());
}
// warn test
while (count++ < 2000) {
log.clearLog();
RecordLog.warn("Count {}", count);
String str = String.format("WARN sentinelRecordLogger - Count %d" + System.lineSeparator(), count);
Assert.assertEquals(str, log.getLog());
}
// trace test
while (count++ < 3000) {
log.clearLog();
RecordLog.trace("Count {}", count);
String str = String.format("TRACE sentinelRecordLogger - Count %d" + System.lineSeparator(), count);
Assert.assertEquals(str, log.getLog());
}
// debug test
while (count++ < 4000) {
log.clearLog();
RecordLog.debug("Count {}", count);
String str = String.format("DEBUG sentinelRecordLogger - Count %d" + System.lineSeparator(), count);
Assert.assertEquals(str, log.getLog());
}
// test error
while (count++ < 5000) {
log.clearLog();
RecordLog.error("Count {}", count);
String str = String.format("ERROR sentinelRecordLogger - Count %d" + System.lineSeparator(), count);
Assert.assertEquals(str, log.getLog());
}
}
@Test
public void testLogException() {
RecordLog.info("init");
log.clearLog();
Exception e = new Exception("ex");
// info test
RecordLog.info("Error", e);
// split the log for test
String[] logSplit = log.getLog().split(System.lineSeparator());
Assert.assertEquals("INFO sentinelRecordLogger - Error", logSplit[0]);
// warn test
log.clearLog();
RecordLog.warn("Error", e);
logSplit = log.getLog().split(System.lineSeparator());
Assert.assertEquals("WARN sentinelRecordLogger - Error", logSplit[0]);
// trace test
log.clearLog();
RecordLog.trace("Error", e);
logSplit = log.getLog().split(System.lineSeparator());
Assert.assertEquals("TRACE sentinelRecordLogger - Error", logSplit[0]);
// debug test
log.clearLog();
RecordLog.debug("Error", e);
logSplit = log.getLog().split(System.lineSeparator());
Assert.assertEquals("DEBUG sentinelRecordLogger - Error", logSplit[0]);
// error test
log.clearLog();
RecordLog.error("Error", e);
logSplit = log.getLog().split(System.lineSeparator());
Assert.assertEquals("ERROR sentinelRecordLogger - Error", logSplit[0]);
}
}