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

albumenj pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.0 by this push:
     new 8a7c360  remove StdErrLog from JettyHttpServer (#9052)
8a7c360 is described below

commit 8a7c360475a6fe33697fd4a1c9cae378f977f684
Author: huangwenkang <[email protected]>
AuthorDate: Mon Dec 6 13:34:55 2021 +0800

    remove StdErrLog from JettyHttpServer (#9052)
    
    * remove StdErrLog from JettyHttpServer (#8108)
    
    * add jetty logger adapter for JettyHttpServer(#8108)
    
    * add license
    
    * add unit test
    
    * change to English description
---
 .../dubbo/remoting/http/jetty/JettyHttpServer.java |   9 +-
 .../remoting/http/jetty/JettyLoggerAdapter.java    | 157 +++++++++++++++++++++
 .../http/jetty/JettyLoggerAdapterTest.java         | 127 +++++++++++++++++
 .../src/test/resources/log4j.xml                   |  41 ++++++
 4 files changed, 328 insertions(+), 6 deletions(-)

diff --git 
a/dubbo-remoting/dubbo-remoting-http/src/main/java/org/apache/dubbo/remoting/http/jetty/JettyHttpServer.java
 
b/dubbo-remoting/dubbo-remoting-http/src/main/java/org/apache/dubbo/remoting/http/jetty/JettyHttpServer.java
index 4a18896..d3577ac 100644
--- 
a/dubbo-remoting/dubbo-remoting-http/src/main/java/org/apache/dubbo/remoting/http/jetty/JettyHttpServer.java
+++ 
b/dubbo-remoting/dubbo-remoting-http/src/main/java/org/apache/dubbo/remoting/http/jetty/JettyHttpServer.java
@@ -31,8 +31,6 @@ import org.eclipse.jetty.server.ServerConnector;
 import org.eclipse.jetty.servlet.ServletContextHandler;
 import org.eclipse.jetty.servlet.ServletHandler;
 import org.eclipse.jetty.servlet.ServletHolder;
-import org.eclipse.jetty.util.log.Log;
-import org.eclipse.jetty.util.log.StdErrLog;
 import org.eclipse.jetty.util.thread.QueuedThreadPool;
 
 import static 
org.apache.dubbo.common.constants.CommonConstants.DEFAULT_THREADS;
@@ -49,10 +47,9 @@ public class JettyHttpServer extends AbstractHttpServer {
     public JettyHttpServer(URL url, final HttpHandler handler) {
         super(url, handler);
         this.url = url;
-        // TODO we should leave this setting to slf4j
-        // we must disable the debug logging for production use
-        Log.setLog(new StdErrLog());
-        Log.getLog().setDebugEnabled(false);
+
+        // set dubbo's logger
+        System.setProperty("org.eclipse.jetty.util.log.class", 
JettyLoggerAdapter.class.getName());
 
         
DispatcherServlet.addHttpHandler(url.getParameter(Constants.BIND_PORT_KEY, 
url.getPort()), handler);
 
diff --git 
a/dubbo-remoting/dubbo-remoting-http/src/main/java/org/apache/dubbo/remoting/http/jetty/JettyLoggerAdapter.java
 
b/dubbo-remoting/dubbo-remoting-http/src/main/java/org/apache/dubbo/remoting/http/jetty/JettyLoggerAdapter.java
new file mode 100644
index 0000000..123231a
--- /dev/null
+++ 
b/dubbo-remoting/dubbo-remoting-http/src/main/java/org/apache/dubbo/remoting/http/jetty/JettyLoggerAdapter.java
@@ -0,0 +1,157 @@
+/*
+ * 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.dubbo.remoting.http.jetty;
+
+import org.apache.dubbo.common.logger.LoggerFactory;
+import org.eclipse.jetty.util.log.AbstractLogger;
+import org.eclipse.jetty.util.log.Logger;
+
+/**
+ * logger adapter for jetty
+ */
+public class JettyLoggerAdapter extends AbstractLogger {
+    protected String name;
+
+    private final org.apache.dubbo.common.logger.Logger logger;
+
+    private static boolean debugEnabled = false;
+
+    public JettyLoggerAdapter(){
+        this("org.apache.dubbo.remoting.http.jetty");
+    }
+
+    public JettyLoggerAdapter(Class<?> clazz){
+        this(clazz.getName());
+    }
+
+    public JettyLoggerAdapter(String name) {
+        this.name = name;
+        this.logger = LoggerFactory.getLogger(name);
+    }
+
+    @Override
+    protected Logger newLogger(String name) {
+        return new JettyLoggerAdapter(name);
+    }
+
+    @Override
+    public String getName() {
+        return this.name;
+    }
+
+    @Override
+    public void warn(String msg, Object... objects) {
+        if (logger.isWarnEnabled()){
+            logger.warn(this.format(msg, objects));
+        }
+    }
+
+    @Override
+    public void warn(Throwable throwable) {
+        if (logger.isWarnEnabled()){
+            logger.warn(throwable);
+        }
+    }
+
+    @Override
+    public void warn(String msg, Throwable throwable) {
+        if (logger.isWarnEnabled()){
+            logger.warn(msg, throwable);
+        }
+    }
+
+    @Override
+    public void info(String msg, Object... objects) {
+        if (logger.isInfoEnabled()){
+            logger.info(this.format(msg, objects));
+        }
+    }
+
+    @Override
+    public void info(Throwable throwable) {
+        if (logger.isInfoEnabled()){
+            logger.info(throwable);
+        }
+    }
+
+    @Override
+    public void info(String msg, Throwable throwable) {
+        if (logger.isInfoEnabled()){
+            logger.info(msg, throwable);
+        }
+    }
+
+    @Override
+    public boolean isDebugEnabled() {
+        return debugEnabled;
+    }
+
+    @Override
+    public void setDebugEnabled(boolean enabled) {
+        debugEnabled = enabled;
+    }
+
+    @Override
+    public void debug(String msg, Object... objects) {
+        if (debugEnabled && logger.isDebugEnabled()){
+            logger.debug(this.format(msg, objects));
+        }
+    }
+
+    @Override
+    public void debug(Throwable throwable) {
+        if (debugEnabled && logger.isDebugEnabled()){
+            logger.debug(throwable);
+        }
+    }
+
+    @Override
+    public void debug(String msg, Throwable throwable) {
+        if (debugEnabled && logger.isDebugEnabled()){
+            logger.debug(msg, throwable);
+        }
+    }
+
+    @Override
+    public void ignore(Throwable throwable) {
+        if (logger.isWarnEnabled()){
+            logger.warn("IGNORED EXCEPTION ", throwable);
+        }
+    }
+
+    private String format(String msg, Object... args) {
+        msg = String.valueOf(msg); // Avoids NPE
+        String braces = "{}";
+        StringBuilder builder = new StringBuilder();
+        int start = 0;
+        for (Object arg : args) {
+            int bracesIndex = msg.indexOf(braces, start);
+            if (bracesIndex < 0) {
+                builder.append(msg.substring(start));
+                builder.append(" ");
+                builder.append(arg);
+                start = msg.length();
+            } else {
+                builder.append(msg, start, bracesIndex);
+                builder.append(arg);
+                start = bracesIndex + braces.length();
+            }
+        }
+        builder.append(msg.substring(start));
+        return builder.toString();
+    }
+}
diff --git 
a/dubbo-remoting/dubbo-remoting-http/src/test/java/org/apache/dubbo/remoting/http/jetty/JettyLoggerAdapterTest.java
 
b/dubbo-remoting/dubbo-remoting-http/src/test/java/org/apache/dubbo/remoting/http/jetty/JettyLoggerAdapterTest.java
new file mode 100644
index 0000000..8812ed1
--- /dev/null
+++ 
b/dubbo-remoting/dubbo-remoting-http/src/test/java/org/apache/dubbo/remoting/http/jetty/JettyLoggerAdapterTest.java
@@ -0,0 +1,127 @@
+/*
+ * 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.dubbo.remoting.http.jetty;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.logger.Logger;
+import org.apache.dubbo.common.url.component.ServiceConfigURL;
+import org.apache.dubbo.common.utils.NetUtils;
+import org.apache.dubbo.remoting.Constants;
+import org.apache.dubbo.remoting.http.HttpHandler;
+import org.apache.dubbo.remoting.http.HttpServer;
+
+import org.apache.http.client.fluent.Request;
+import org.eclipse.jetty.util.log.Log;
+import org.junit.jupiter.api.Test;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.verify;
+
+public class JettyLoggerAdapterTest {
+
+    @Test
+    public void testJettyUseDubboLogger() throws Exception{
+        int port = NetUtils.getAvailablePort();
+        URL url = new ServiceConfigURL("http", "localhost", port,
+            new String[]{Constants.BIND_PORT_KEY, String.valueOf(port)});
+        HttpServer httpServer = new JettyHttpServer(url, new HttpHandler() {
+            @Override
+            public void handle(HttpServletRequest request, HttpServletResponse 
response) throws IOException {
+                response.getWriter().write("Jetty is using Dubbo's logger");
+            }
+        });
+        
Request.Get(url.toJavaURL().toURI()).execute().returnContent().asString();
+
+        
assertThat(Log.getLog().getClass().isAssignableFrom(JettyLoggerAdapter.class), 
is(true));
+
+        httpServer.close();
+    }
+
+
+    @Test
+    public void testSuccessLogger() throws Exception{
+        Logger successLogger = mock(Logger.class);
+        Class<?> clazz = 
Class.forName("org.apache.dubbo.remoting.http.jetty.JettyLoggerAdapter");
+        JettyLoggerAdapter jettyLoggerAdapter = (JettyLoggerAdapter) 
clazz.newInstance();
+
+        Field loggerField = clazz.getDeclaredField("logger");
+        loggerField.setAccessible(true);
+        loggerField.set(jettyLoggerAdapter, successLogger);
+        jettyLoggerAdapter.setDebugEnabled(true);
+
+        when(successLogger.isDebugEnabled()).thenReturn(true);
+        when(successLogger.isWarnEnabled()).thenReturn(true);
+        when(successLogger.isInfoEnabled()).thenReturn(true);
+
+        jettyLoggerAdapter.warn("warn");
+        jettyLoggerAdapter.info("info");
+        jettyLoggerAdapter.debug("debug");
+
+        verify(successLogger).warn(anyString());
+        verify(successLogger).info(anyString());
+        verify(successLogger).debug(anyString());
+
+        jettyLoggerAdapter.warn(new Exception("warn"));
+        jettyLoggerAdapter.info(new Exception("info"));
+        jettyLoggerAdapter.debug(new Exception("debug"));
+        jettyLoggerAdapter.ignore(new Exception("ignore"));
+
+        jettyLoggerAdapter.warn("warn", new Exception("warn"));
+        jettyLoggerAdapter.info("info", new Exception("info"));
+        jettyLoggerAdapter.debug("debug", new Exception("debug"));
+    }
+
+
+    @Test
+    public void testNewLogger(){
+        JettyLoggerAdapter loggerAdapter = new JettyLoggerAdapter();
+        org.eclipse.jetty.util.log.Logger logger = 
loggerAdapter.newLogger(this.getClass().getName());
+        
assertThat(logger.getClass().isAssignableFrom(JettyLoggerAdapter.class), 
is(true));
+    }
+
+
+    @Test
+    public void testDebugEnabled(){
+        JettyLoggerAdapter loggerAdapter = new JettyLoggerAdapter();
+        loggerAdapter.setDebugEnabled(true);
+        assertThat(loggerAdapter.isDebugEnabled(), is(true));
+    }
+
+
+    @Test
+    public void testLoggerFormat() throws Exception{
+        Class<?> clazz = 
Class.forName("org.apache.dubbo.remoting.http.jetty.JettyLoggerAdapter");
+        Object newInstance = clazz.newInstance();
+
+        Method method = clazz.getDeclaredMethod("format", String.class, 
Object[].class);
+        method.setAccessible(true);
+
+        String print = (String) method.invoke(newInstance, "Hello,{}! I'am 
{}", new  String[]{"World","Jetty"});
+
+        assertThat(print, is("Hello,World! I'am Jetty"));
+    }
+}
diff --git a/dubbo-remoting/dubbo-remoting-http/src/test/resources/log4j.xml 
b/dubbo-remoting/dubbo-remoting-http/src/test/resources/log4j.xml
new file mode 100644
index 0000000..ef26f07
--- /dev/null
+++ b/dubbo-remoting/dubbo-remoting-http/src/test/resources/log4j.xml
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  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.
+-->
+<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
+<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/";>
+    <!-- ===================================================================== 
-->
+    <!-- appender config -->
+    <!-- ===================================================================== 
-->
+    <appender name="dubbo" class="org.apache.dubbo.common.utils.DubboAppender">
+        <param name="File" value="${user.dir}/dubbo.log"/>
+        <param name="encoding" value="GBK"/>
+        <layout class="org.apache.log4j.PatternLayout">
+            <param name="ConversionPattern" value="%d %p [%c:%M] - %m%n"/>
+        </layout>
+    </appender>
+
+    <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
+        <layout class="org.apache.log4j.PatternLayout">
+            <param name="ConversionPattern" value="[%d{dd/MM/yy HH:mm:ss:SSS 
z}] %t %5p %c{2}: %m%n"/>
+        </layout>
+    </appender>
+    <root>
+        <level value="INFO"/>
+        <appender-ref ref="dubbo"/>
+        <appender-ref ref="CONSOLE"/>
+    </root>
+</log4j:configuration>

Reply via email to