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,18 @@
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-parent</artifactId>
<version>1.8.3</version>
</parent>
<artifactId>sentinel-logging</artifactId>
<packaging>pom</packaging>
<modules>
<module>sentinel-logging-slf4j</module>
</modules>
</project>

View File

@@ -0,0 +1,44 @@
# Sentinel Logging Extension SLF4J
To integrate logs of sentinel into your project which uses slf4j for bridge of logging you can
simply introduce following dependency to your project:
```xml
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-logging-slf4j</artifactId>
<version>${sentinel.version}</version>
</dependency>
```
And if you want to control level of logging special for sentinel the loggers that sentinel uses
are called `sentinelRecordLogger` and `sentinelCommandCenterLogger`. For example in XML configration
coming with log4j2 implementation:
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%-5level %logger - %msg%n"/>
</Console>
<File name="FILE" fileName="sentinel-record.log" append="false">
<PatternLayout pattern="%-5level %logger - %msg%n"/>
</File>
<File name="FILE2" fileName="sentinel-command-center.log" append="false">
<PatternLayout pattern="%-5level %logger - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="info"/>
<logger name="sentinelRecordLogger" level="trace">
<appender-ref ref="Console" />
<appender-ref ref="FILE" />
</logger>
<logger name="sentinelCommandCenterLogger" level="trace">
<appender-ref ref="Console" />
<appender-ref ref="FILE2" />
</logger>
</Loggers>
</Configuration>
```

View File

@@ -0,0 +1,49 @@
<?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-logging</artifactId>
<groupId>com.alibaba.csp</groupId>
<version>1.8.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>sentinel-logging-slf4j</artifactId>
<packaging>jar</packaging>
<properties>
<slf4j.version>1.7.25</slf4j.version>
<slf4j-test.version>1.2.0</slf4j-test.version>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-common</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>provided</scope>
</dependency>
<!-- for unit test -->
<dependency>
<groupId>uk.org.lidalia</groupId>
<artifactId>slf4j-test</artifactId>
<version>${slf4j-test.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,81 @@
/*
* 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.logging.slf4j;
import com.alibaba.csp.sentinel.log.LogTarget;
import com.alibaba.csp.sentinel.log.Logger;
import com.alibaba.csp.sentinel.transport.log.CommandCenterLog;
import org.slf4j.LoggerFactory;
/**
* @author wavesZh
*/
@LogTarget(CommandCenterLog.LOGGER_NAME)
public class CommandCenterLogLogger 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,81 @@
/*
* 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.logging.slf4j;
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;
/**
* @author wavesZh
*/
@LogTarget(RecordLog.LOGGER_NAME)
public class RecordLogLogger 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.logging.slf4j.RecordLogLogger
com.alibaba.csp.sentinel.logging.slf4j.CommandCenterLogLogger

View File

@@ -0,0 +1,185 @@
/*
* 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.logging.slf4j;
import uk.org.lidalia.slf4jext.Level;
import uk.org.lidalia.slf4jtest.TestLoggerFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.mockito.Mockito.*;
import java.io.PrintStream;
/**
* @author xue8
* @author jason
*/
public abstract class AbstraceSlf4jLogTest {
private static final class TestException extends Exception {
private static final long serialVersionUID = 1L;
}
PrintStream mockStream;
PrintStream oldOutStream;
PrintStream oldErrStream;
protected abstract String getLoggerName();
protected abstract void debug(String msg, Object... args);
protected abstract void trace(String msg, Object... args);
protected abstract void info(String msg, Object... args);
protected abstract void warn(String msg, Object... args);
protected abstract void error(String msg, Object... args);
@Before
public void mockOutput() {
System.out.println("Try to mock System.out and System.err");
mockStream = mock(PrintStream.class);
oldOutStream = System.out;
oldOutStream = System.out;
System.setOut(mockStream);
System.setErr(mockStream);
TestLoggerFactory.getInstance().setPrintLevel(Level.TRACE);
}
@After
public void restore() {
if (oldOutStream != null) {
System.setOut(oldOutStream);
System.setErr(oldErrStream);
oldOutStream = null;
oldErrStream = null;
System.out.println("Restore System.out and System.err");
}
}
@Test
public void testRecordLog() {
info("init");
verify(mockStream).println(contains("init"));
clearInvocations(mockStream);
int count = 0;
// info test
while (count < 1000) { // 0~999
info("Count {}.", count);
count ++;
}
verify(mockStream).println(contains("Count 0."));
verify(mockStream).println(contains("Count 1."));
verify(mockStream).println(contains("Count 99."));
verify(mockStream).println(contains("Count 123."));
verify(mockStream).println(contains("Count 888."));
verify(mockStream).println(contains("Count 999."));
verify(mockStream, times(1000)).println(contains(" INFO "));
verify(mockStream, times(1000)).println(contains(getLoggerName()));
clearInvocations(mockStream);
// warn test
while (count < 2000) { // 1000~1999
warn("Count {}.", count);
count ++;
}
verify(mockStream).println(contains("Count 1000."));
verify(mockStream).println(contains("Count 1223."));
verify(mockStream).println(contains("Count 1888."));
verify(mockStream).println(contains("Count 1999."));
verify(mockStream, times(1000)).println(contains(" WARN "));
verify(mockStream, times(1000)).println(contains(getLoggerName()));
clearInvocations(mockStream);
// trace test
while (count < 3000) { // 2000~2999
trace("Count {}.", count);
count ++;
}
verify(mockStream).println(contains("Count 2000."));
verify(mockStream).println(contains("Count 2999."));
verify(mockStream, times(1000)).println(contains(" TRACE "));
verify(mockStream, times(1000)).println(contains(getLoggerName()));
clearInvocations(mockStream);
// debug test
while (count < 4000) { // 3000~3999
debug("Count {}.", count);
count ++;
}
verify(mockStream).println(contains("Count 3000."));
verify(mockStream).println(contains("Count 3999."));
verify(mockStream, times(1000)).println(contains(" DEBUG "));
verify(mockStream, times(1000)).println(contains(getLoggerName()));
clearInvocations(mockStream);
// test error
while (count < 5000) { // 4000~4999
error("Count {}.", count);
count ++;
}
verify(mockStream).println(contains("Count 4000."));
verify(mockStream).println(contains("Count 4999."));
verify(mockStream, times(1000)).println(contains(" ERROR "));
verify(mockStream, times(1000)).println(contains(getLoggerName()));
clearInvocations(mockStream);
}
@Test
public void testLogException() {
info("init");
verify(mockStream).println(contains("init"));
verify(mockStream, atLeastOnce()).println(contains(getLoggerName()));
clearInvocations(mockStream);
Exception e = new TestException();
// info test
info("some log", e);
verify(mockStream).println(contains("INFO"));
verify(mockStream).println(isA(TestException.class));
verify(mockStream).println(contains(getLoggerName()));
clearInvocations(mockStream);
// warn test
warn("some log", e);
verify(mockStream).println(contains("WARN"));
verify(mockStream).println(isA(TestException.class));
verify(mockStream).println(contains(getLoggerName()));
clearInvocations(mockStream);
// trace test
trace("some log", e);
verify(mockStream).println(contains("TRACE"));
verify(mockStream).println(isA(TestException.class));
verify(mockStream).println(contains(getLoggerName()));
clearInvocations(mockStream);
// debug test
debug("some log", e);
verify(mockStream).println(contains("DEBUG"));
verify(mockStream).println(isA(TestException.class));
verify(mockStream).println(contains(getLoggerName()));
clearInvocations(mockStream);
// error test
error("some log", e);
verify(mockStream).println(contains("ERROR"));
verify(mockStream).println(isA(TestException.class));
verify(mockStream).println(contains(getLoggerName()));
clearInvocations(mockStream);
}
}

View File

@@ -0,0 +1,55 @@
/*
* 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.logging.slf4j;
import com.alibaba.csp.sentinel.transport.log.CommandCenterLog;
/**
* @author jason
*/
public class CommandCenterLogTest extends AbstraceSlf4jLogTest {
@Override
protected String getLoggerName() {
return CommandCenterLog.LOGGER_NAME;
}
@Override
protected void debug(String msg, Object... args) {
CommandCenterLog.debug(msg, args);
}
@Override
protected void trace(String msg, Object... args) {
CommandCenterLog.trace(msg, args);
}
@Override
protected void info(String msg, Object... args) {
CommandCenterLog.info(msg, args);
}
@Override
protected void warn(String msg, Object... args) {
CommandCenterLog.warn(msg, args);
}
@Override
protected void error(String msg, Object... args) {
CommandCenterLog.error(msg, args);
}
}

View File

@@ -0,0 +1,55 @@
/*
* 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.logging.slf4j;
import com.alibaba.csp.sentinel.log.RecordLog;
/**
* @author jason
*/
public class RecordLogTest extends AbstraceSlf4jLogTest {
@Override
protected String getLoggerName() {
return RecordLog.LOGGER_NAME;
}
@Override
protected void debug(String msg, Object... args) {
RecordLog.debug(msg, args);
}
@Override
protected void trace(String msg, Object... args) {
RecordLog.trace(msg, args);
}
@Override
protected void info(String msg, Object... args) {
RecordLog.info(msg, args);
}
@Override
protected void warn(String msg, Object... args) {
RecordLog.warn(msg, args);
}
@Override
protected void error(String msg, Object... args) {
RecordLog.error(msg, args);
}
}