This is an automated email from the ASF dual-hosted git repository.

jianglongtao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 724d3831b62 Refactor logback config by Java spi (#22003)
724d3831b62 is described below

commit 724d3831b621efca0f3d798b2fc4f43d3ac91270
Author: ChenJiaHao <[email protected]>
AuthorDate: Thu Nov 10 16:39:06 2022 +0800

    Refactor logback config by Java spi (#22003)
---
 proxy/bootstrap/pom.xml                            |  2 +-
 .../proxy/config/LogbackConfiguration.java         | 85 ++++++++++++++++++++++
 .../ch.qos.logback.classic.spi.Configurator        | 18 +++++
 proxy/bootstrap/src/main/resources/logback.xml     | 39 ----------
 .../shardingsphere/proxy/config/LogbackTest.java   | 57 +++++++++++++++
 5 files changed, 161 insertions(+), 40 deletions(-)

diff --git a/proxy/bootstrap/pom.xml b/proxy/bootstrap/pom.xml
index ab496d42237..0e0d5147beb 100644
--- a/proxy/bootstrap/pom.xml
+++ b/proxy/bootstrap/pom.xml
@@ -139,7 +139,7 @@
         <dependency>
             <groupId>ch.qos.logback</groupId>
             <artifactId>logback-classic</artifactId>
-            <scope>runtime</scope>
+            <scope>compile</scope>
         </dependency>
     </dependencies>
     <build>
diff --git 
a/proxy/bootstrap/src/main/java/org/apache/shardingsphere/proxy/config/LogbackConfiguration.java
 
b/proxy/bootstrap/src/main/java/org/apache/shardingsphere/proxy/config/LogbackConfiguration.java
new file mode 100644
index 00000000000..1bf80ce89d0
--- /dev/null
+++ 
b/proxy/bootstrap/src/main/java/org/apache/shardingsphere/proxy/config/LogbackConfiguration.java
@@ -0,0 +1,85 @@
+/*
+ * 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 org.apache.shardingsphere.proxy.config;
+
+import ch.qos.logback.classic.BasicConfigurator;
+import ch.qos.logback.classic.Level;
+import ch.qos.logback.classic.Logger;
+import ch.qos.logback.classic.LoggerContext;
+import ch.qos.logback.classic.PatternLayout;
+import ch.qos.logback.classic.spi.ILoggingEvent;
+import ch.qos.logback.core.ConsoleAppender;
+import ch.qos.logback.core.encoder.LayoutWrappingEncoder;
+
+public class LogbackConfiguration extends BasicConfigurator {
+    
+    public static final String DEFAULT_PATTERN = "[%-5level] %d{yyyy-MM-dd 
HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n";
+    
+    public static final String SHARDINGSPHERE_LOGGER_NAME = 
"org.apache.shardingsphere";
+    
+    public static final String HIKARI_LOGGER_NAME = "com.zaxxer.hikari";
+    
+    public static final String ATOMIKOS_LOGGER_NAME = "com.atomikos";
+    
+    public static final String NETTY_LOGGER_NAME = "io.netty";
+    
+    @Override
+    public void configure(final LoggerContext loggerContext) {
+        ConsoleAppender<ILoggingEvent> consoleAppender = 
createConsoleAppender(loggerContext);
+        Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
+        rootLogger.setLevel(Level.INFO);
+        rootLogger.addAppender(consoleAppender);
+        Logger shardingsphereLogger = 
loggerContext.getLogger(SHARDINGSPHERE_LOGGER_NAME);
+        shardingsphereLogger.setLevel(Level.INFO);
+        shardingsphereLogger.setAdditive(false);
+        shardingsphereLogger.addAppender(consoleAppender);
+        initBasicLogger(loggerContext);
+    }
+    
+    private ConsoleAppender<ILoggingEvent> createConsoleAppender(final 
LoggerContext loggerContext) {
+        ConsoleAppender<ILoggingEvent> consoleAppender = new 
ConsoleAppender<>();
+        consoleAppender.setContext(loggerContext);
+        consoleAppender.setName("console");
+        LayoutWrappingEncoder<ILoggingEvent> encoder = 
createEncoder(loggerContext);
+        consoleAppender.setEncoder(encoder);
+        consoleAppender.start();
+        return consoleAppender;
+    }
+    
+    private LayoutWrappingEncoder<ILoggingEvent> createEncoder(final 
LoggerContext loggerContext) {
+        LayoutWrappingEncoder<ILoggingEvent> encoder = new 
LayoutWrappingEncoder<>();
+        encoder.setContext(loggerContext);
+        PatternLayout layout = createConsolePatternLayout(loggerContext);
+        encoder.setLayout(layout);
+        return encoder;
+    }
+    
+    private PatternLayout createConsolePatternLayout(final LoggerContext 
loggerContext) {
+        PatternLayout layout = new PatternLayout();
+        layout.setPattern(DEFAULT_PATTERN);
+        layout.setContext(loggerContext);
+        layout.start();
+        return layout;
+    }
+    
+    private void initBasicLogger(final LoggerContext loggerContext) {
+        loggerContext.getLogger(HIKARI_LOGGER_NAME).setLevel(Level.ERROR);
+        loggerContext.getLogger(ATOMIKOS_LOGGER_NAME).setLevel(Level.ERROR);
+        loggerContext.getLogger(NETTY_LOGGER_NAME).setLevel(Level.ERROR);
+    }
+}
diff --git 
a/proxy/bootstrap/src/main/resources/META-INF/services/ch.qos.logback.classic.spi.Configurator
 
b/proxy/bootstrap/src/main/resources/META-INF/services/ch.qos.logback.classic.spi.Configurator
new file mode 100644
index 00000000000..591f5991b35
--- /dev/null
+++ 
b/proxy/bootstrap/src/main/resources/META-INF/services/ch.qos.logback.classic.spi.Configurator
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.shardingsphere.proxy.config.LogbackConfiguration
diff --git a/proxy/bootstrap/src/main/resources/logback.xml 
b/proxy/bootstrap/src/main/resources/logback.xml
deleted file mode 100644
index efeb323b7d0..00000000000
--- a/proxy/bootstrap/src/main/resources/logback.xml
+++ /dev/null
@@ -1,39 +0,0 @@
-<?xml version="1.0"?>
-<!--
-  ~ 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.
-  -->
-
-<configuration>
-    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
-        <encoder>
-            <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] 
%logger{36} - %msg%n</pattern>
-        </encoder>
-    </appender>
-    <logger name="org.apache.shardingsphere" level="info" additivity="false">
-        <appender-ref ref="console" />
-    </logger>
-    
-    <logger name="com.zaxxer.hikari" level="error" />
-    
-    <logger name="com.atomikos" level="error" />
-    
-    <logger name="io.netty" level="error" />
-    
-    <root>
-        <level value="info" />
-        <appender-ref ref="console" />
-    </root>
-</configuration> 
diff --git 
a/proxy/bootstrap/src/test/java/org/apache/shardingsphere/proxy/config/LogbackTest.java
 
b/proxy/bootstrap/src/test/java/org/apache/shardingsphere/proxy/config/LogbackTest.java
new file mode 100644
index 00000000000..a42edbe6319
--- /dev/null
+++ 
b/proxy/bootstrap/src/test/java/org/apache/shardingsphere/proxy/config/LogbackTest.java
@@ -0,0 +1,57 @@
+/*
+ * 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 org.apache.shardingsphere.proxy.config;
+
+import ch.qos.logback.classic.Level;
+import ch.qos.logback.classic.Logger;
+import ch.qos.logback.classic.LoggerContext;
+import ch.qos.logback.classic.PatternLayout;
+import ch.qos.logback.classic.spi.ILoggingEvent;
+import ch.qos.logback.core.ConsoleAppender;
+import ch.qos.logback.core.encoder.LayoutWrappingEncoder;
+import lombok.extern.slf4j.Slf4j;
+import org.junit.Test;
+import org.slf4j.LoggerFactory;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+@Slf4j
+public final class LogbackTest {
+    
+    public static final String DEFAULT_PATTERN = "[%-5level] %d{yyyy-MM-dd 
HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n";
+    
+    @Test
+    public void assertLogConfiguration() {
+        LoggerContext loggerContext = (LoggerContext) 
LoggerFactory.getILoggerFactory();
+        Logger rootLogger = loggerContext.getLogger("ROOT");
+        assertThat(rootLogger.getLevel(), is(Level.INFO));
+        assertTrue(rootLogger.getAppender("console") instanceof 
ConsoleAppender);
+        ConsoleAppender<ILoggingEvent> consoleAppender = 
(ConsoleAppender<ILoggingEvent>) rootLogger.getAppender("console");
+        assertTrue(consoleAppender.getEncoder() instanceof 
LayoutWrappingEncoder);
+        LayoutWrappingEncoder<ILoggingEvent> encoder = 
(LayoutWrappingEncoder<ILoggingEvent>) consoleAppender.getEncoder();
+        assertTrue(encoder.getLayout() instanceof PatternLayout);
+        PatternLayout layout = (PatternLayout) encoder.getLayout();
+        assertThat(layout.getPattern(), is(DEFAULT_PATTERN));
+        
assertThat(loggerContext.getLogger("org.apache.shardingsphere").getLevel(), 
is(Level.INFO));
+        assertThat(loggerContext.getLogger("com.zaxxer.hikari").getLevel(), 
is(Level.ERROR));
+        assertThat(loggerContext.getLogger("com.atomikos").getLevel(), 
is(Level.ERROR));
+        assertThat(loggerContext.getLogger("io.netty").getLevel(), 
is(Level.ERROR));
+    }
+}

Reply via email to