diff --git a/sentinel-extension/pom.xml b/sentinel-extension/pom.xml index 72bd6d92f9..6d96dcce8f 100755 --- a/sentinel-extension/pom.xml +++ b/sentinel-extension/pom.xml @@ -22,6 +22,7 @@ sentinel-datasource-spring-cloud-config sentinel-datasource-consul sentinel-datasource-etcd + sentinel-logging-extension-slf4j diff --git a/sentinel-extension/sentinel-logging-extension-slf4j/README.md b/sentinel-extension/sentinel-logging-extension-slf4j/README.md new file mode 100644 index 0000000000..6abf29b1e3 --- /dev/null +++ b/sentinel-extension/sentinel-logging-extension-slf4j/README.md @@ -0,0 +1,49 @@ +# Sentinel Logging Extension SLF4J + +To use Sentinel Logging Extension SLF4J with Log4j2, you should add the following dependency firstly: + +```xml + + com.alibaba.csp + sentinel-logging-extension-slf4j + x.y.z + + + org.slf4j + slf4j-api + ${slf4j.version} + +``` +If you want to use Slf4j with Log4j2, you can add dependencies of Log4j2 and the binding about Log4j2 and SLF4J. +Then you should provide logging configuration as specification of the logging framework. +And you can add Sentinel's Loggers that it name is `sentinelRecordLogger` or `sentinelCommandCenterLogger` for your needs. For example: + +```xml + + + + + + + + + + + + + + + + + + + + + + + + + +``` + + diff --git a/sentinel-extension/sentinel-logging-extension-slf4j/pom.xml b/sentinel-extension/sentinel-logging-extension-slf4j/pom.xml new file mode 100644 index 0000000000..351904bdb0 --- /dev/null +++ b/sentinel-extension/sentinel-logging-extension-slf4j/pom.xml @@ -0,0 +1,60 @@ + + + + sentinel-parent + com.alibaba.csp + 1.7.2-SNAPSHOT + + 4.0.0 + + sentinel-logging-extension-slf4j + jar + + + 1.7 + 1.7 + 1.7.25 + 2.12.1 + + + + + com.alibaba.csp + sentinel-transport-common + + + org.slf4j + slf4j-api + ${slf4j.version} + provided + + + org.apache.logging.log4j + log4j-core + ${log4j2.version} + test + + + org.apache.logging.log4j + log4j-slf4j-impl + ${log4j2.version} + test + + + + junit + junit + test + + + + com.github.stefanbirkner + system-rules + 1.16.1 + test + + + + \ No newline at end of file diff --git a/sentinel-extension/sentinel-logging-extension-slf4j/src/main/java/com/alibaba/csp/sentinel/logging/slf4j/CommandCenterLogLogger.java b/sentinel-extension/sentinel-logging-extension-slf4j/src/main/java/com/alibaba/csp/sentinel/logging/slf4j/CommandCenterLogLogger.java new file mode 100644 index 0000000000..e1ed4e0ff1 --- /dev/null +++ b/sentinel-extension/sentinel-logging-extension-slf4j/src/main/java/com/alibaba/csp/sentinel/logging/slf4j/CommandCenterLogLogger.java @@ -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); + } + +} diff --git a/sentinel-extension/sentinel-logging-extension-slf4j/src/main/java/com/alibaba/csp/sentinel/logging/slf4j/RecordLogLogger.java b/sentinel-extension/sentinel-logging-extension-slf4j/src/main/java/com/alibaba/csp/sentinel/logging/slf4j/RecordLogLogger.java new file mode 100644 index 0000000000..f9ffa17837 --- /dev/null +++ b/sentinel-extension/sentinel-logging-extension-slf4j/src/main/java/com/alibaba/csp/sentinel/logging/slf4j/RecordLogLogger.java @@ -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); + } + +} diff --git a/sentinel-extension/sentinel-logging-extension-slf4j/src/main/resources/META-INF/services/com.alibaba.csp.sentinel.log.Logger b/sentinel-extension/sentinel-logging-extension-slf4j/src/main/resources/META-INF/services/com.alibaba.csp.sentinel.log.Logger new file mode 100644 index 0000000000..77330c20c4 --- /dev/null +++ b/sentinel-extension/sentinel-logging-extension-slf4j/src/main/resources/META-INF/services/com.alibaba.csp.sentinel.log.Logger @@ -0,0 +1,2 @@ +com.alibaba.csp.sentinel.logging.slf4j.RecordLogLogger +com.alibaba.csp.sentinel.logging.slf4j.CommandCenterLogLogger \ No newline at end of file diff --git a/sentinel-extension/sentinel-logging-extension-slf4j/src/test/java/com/alibaba/csp/sentinel/logging/slf4j/CommandCenterLogTest.java b/sentinel-extension/sentinel-logging-extension-slf4j/src/test/java/com/alibaba/csp/sentinel/logging/slf4j/CommandCenterLogTest.java new file mode 100644 index 0000000000..4df72733d2 --- /dev/null +++ b/sentinel-extension/sentinel-logging-extension-slf4j/src/test/java/com/alibaba/csp/sentinel/logging/slf4j/CommandCenterLogTest.java @@ -0,0 +1,115 @@ +/* + * 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.logging.slf4j; + +import com.alibaba.csp.sentinel.transport.log.CommandCenterLog; +import org.junit.*; +import org.junit.contrib.java.lang.system.SystemOutRule; + +/** + * @author xue8 + */ +@Ignore("https://github.com/stefanbirkner/system-rules/issues/45. You can only run one @Test at a time") +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]); + } + + +} diff --git a/sentinel-extension/sentinel-logging-extension-slf4j/src/test/java/com/alibaba/csp/sentinel/logging/slf4j/RecordLogTest.java b/sentinel-extension/sentinel-logging-extension-slf4j/src/test/java/com/alibaba/csp/sentinel/logging/slf4j/RecordLogTest.java new file mode 100644 index 0000000000..735691cd6e --- /dev/null +++ b/sentinel-extension/sentinel-logging-extension-slf4j/src/test/java/com/alibaba/csp/sentinel/logging/slf4j/RecordLogTest.java @@ -0,0 +1,118 @@ +/* + * 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; +import org.junit.Assert; +import org.junit.Ignore; +import org.junit.Rule; +import org.junit.Test; +import org.junit.contrib.java.lang.system.SystemOutRule; + +/** + * @author xue8 + */ +@Ignore("https://github.com/stefanbirkner/system-rules/issues/45. You can only run one @Test at a time") +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]); + } +} diff --git a/sentinel-extension/sentinel-logging-extension-slf4j/src/test/resources/log4j2.xml b/sentinel-extension/sentinel-logging-extension-slf4j/src/test/resources/log4j2.xml new file mode 100644 index 0000000000..1ce6b1955c --- /dev/null +++ b/sentinel-extension/sentinel-logging-extension-slf4j/src/test/resources/log4j2.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file