This is an automated email from the ASF dual-hosted git repository. liubao pushed a commit to branch logger-example in repository https://gitbox.apache.org/repos/asf/servicecomb-samples.git
commit 9b52a39bbc107e1903d72abe03e93f995dbe25f5 Author: liubao68 <[email protected]> AuthorDate: Wed Jul 7 17:04:03 2021 +0800 add logger example --- .../servicecomb/samples/ConsumerApplication.java | 29 ++++++++- basic/consumer/src/main/resources/application.yml | 2 + basic/consumer/src/main/resources/log4j2.xml | 45 -------------- basic/consumer/src/main/resources/logback.xml | 71 ++++++++++++++++++++++ basic/gateway/src/main/resources/application.yml | 3 +- basic/gateway/src/main/resources/log4j2.xml | 45 -------------- basic/gateway/src/main/resources/logback.xml | 71 ++++++++++++++++++++++ .../servicecomb/samples/ProviderController.java | 5 ++ basic/provider/src/main/resources/application.yml | 2 + basic/provider/src/main/resources/log4j2.xml | 45 -------------- basic/provider/src/main/resources/logback.xml | 71 ++++++++++++++++++++++ 11 files changed, 252 insertions(+), 137 deletions(-) diff --git a/basic/consumer/src/main/java/org/apache/servicecomb/samples/ConsumerApplication.java b/basic/consumer/src/main/java/org/apache/servicecomb/samples/ConsumerApplication.java index 0010f13..964173e 100644 --- a/basic/consumer/src/main/java/org/apache/servicecomb/samples/ConsumerApplication.java +++ b/basic/consumer/src/main/java/org/apache/servicecomb/samples/ConsumerApplication.java @@ -17,20 +17,47 @@ package org.apache.servicecomb.samples; +import java.util.concurrent.atomic.AtomicLong; + +import org.apache.servicecomb.provider.pojo.RpcReference; import org.apache.servicecomb.springboot2.starter.EnableServiceComb; -import org.springframework.boot.SpringApplication; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.boot.WebApplicationType; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.stereotype.Component; @SpringBootApplication @EnableServiceComb +@Component public class ConsumerApplication { + private static final Logger LOGGER = LoggerFactory.getLogger(ConsumerApplication.class); + + @RpcReference(microserviceName = "provider", schemaId = "ProviderController") + private static ProviderService providerService; + public static void main(String[] args) throws Exception { try { new SpringApplicationBuilder().web(WebApplicationType.NONE).sources(ConsumerApplication.class).run(args); } catch (Exception e) { e.printStackTrace(); } + + new Thread() { + public void run() { + AtomicLong index = new AtomicLong(0); + + while (true) { + try { + LOGGER.info("call service: name=" + index.get()); + providerService.sayHello("hello" + index.getAndIncrement()); + Thread.sleep(2000); + } catch (InterruptedException e) { + LOGGER.error("", e); + } + } + } + }.start(); } } diff --git a/basic/consumer/src/main/resources/application.yml b/basic/consumer/src/main/resources/application.yml index 9a98590..713fec6 100644 --- a/basic/consumer/src/main/resources/application.yml +++ b/basic/consumer/src/main/resources/application.yml @@ -43,6 +43,8 @@ servicecomb: rest: address: 0.0.0.0:9092 # should be same with server.port to use web container + metrics: + window_time: 5000 # Configure AK/SK credentials if needed. Default not enabled. # credentials: # akskEnabled: false diff --git a/basic/consumer/src/main/resources/log4j2.xml b/basic/consumer/src/main/resources/log4j2.xml deleted file mode 100644 index 313d1fc..0000000 --- a/basic/consumer/src/main/resources/log4j2.xml +++ /dev/null @@ -1,45 +0,0 @@ -<?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. - --> - -<!--this is sample configuration, please modify as your wish --> - -<configuration> - <Properties> - <Property name="log_path">./file/log/</Property> - </Properties> - - <Appenders> - <Console name="Console" target="SYSTEM_OUT"> - <PatternLayout pattern="[%d][%t][%p][%c:%L] %m%n" /> - </Console> - <RollingFile name="DailyRollingFile" fileName="${log_path}/output.log" - filePattern="${log_path}/zcrTest%d{yyyy-MM-dd}.log"> - <PatternLayout pattern="[%d][%t][%p][%c:%L] %m%n" /> - <Policies> - <TimeBasedTriggeringPolicy interval="1" /> - <SizeBasedTriggeringPolicy size="10 MB" /> - </Policies> - </RollingFile> - </Appenders> - <Loggers> - <Root level="info"> - <AppenderRef ref="Console" /> - <AppenderRef ref="DailyRollingFile" /> - </Root> - </Loggers> -</configuration> \ No newline at end of file diff --git a/basic/consumer/src/main/resources/logback.xml b/basic/consumer/src/main/resources/logback.xml new file mode 100644 index 0000000..72c6fbf --- /dev/null +++ b/basic/consumer/src/main/resources/logback.xml @@ -0,0 +1,71 @@ +<?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. + --> + +<configuration scan="true"> + <appender name="metrics" class="ch.qos.logback.core.rolling.RollingFileAppender"> + <file>metrics.log</file> + <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> + <!-- 按天回滚 daily --> + <fileNamePattern>metrics.%d{yyyy-MM-dd}.log</fileNamePattern> + <!-- 日志最大的历史 30天 --> + <maxHistory>60</maxHistory> + <!--用来指定日志文件的上限大小,那么到了这个值,就会删除旧的日志--> + <!--<totalSizeCap>1GB</totalSizeCap>--> + </rollingPolicy> + <encoder> + <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{80} - %msg%n</pattern> + </encoder> + </appender> + + <appender name="servicecomb" class="ch.qos.logback.core.rolling.RollingFileAppender"> + <file>servicecomb.log</file> + <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> + <!-- 按天回滚 daily --> + <fileNamePattern>servicecomb.%d{yyyy-MM-dd}.log</fileNamePattern> + <!-- 日志最大的历史 30天 --> + <maxHistory>60</maxHistory> + <!--用来指定日志文件的上限大小,那么到了这个值,就会删除旧的日志--> + <!--<totalSizeCap>1GB</totalSizeCap>--> + </rollingPolicy> + <encoder> + <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{80} - %msg%n</pattern> + </encoder> + </appender> + + <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> + <!-- If applicable, can use ch.qos.logback.classic.filter.MarkerFilter --> + <filter class="org.apache.servicecomb.foundation.logback.MarkerFilter"> + <Marker>SERVICECOMB_MARKER</Marker> + <OnMismatch>ACCEPT</OnMismatch> + <OnMatch>DENY</OnMatch> + </filter> + + <encoder> + <pattern>%d [%level] [%thread] - %msg (%F:%L\)%n</pattern> + </encoder> + </appender> + + <logger name="org.apache.servicecomb.metrics.core.publish.DefaultLogPublisher" level="INFO" additivity="false"> + <appender-ref ref="metrics"/> + </logger> + + <root level="info"> + <appender-ref ref="STDOUT"/> + <appender-ref ref="servicecomb"/> + </root> +</configuration> \ No newline at end of file diff --git a/basic/gateway/src/main/resources/application.yml b/basic/gateway/src/main/resources/application.yml index b625f41..f60fd3e 100644 --- a/basic/gateway/src/main/resources/application.yml +++ b/basic/gateway/src/main/resources/application.yml @@ -54,7 +54,8 @@ servicecomb: path: "/.*" microserviceName: consumer versionRule: 0.0.0+ - + metrics: + window_time: 5000 # Configure AK/SK credentials if needed. Default not enabled. # credentials: # akskEnabled: false diff --git a/basic/gateway/src/main/resources/log4j2.xml b/basic/gateway/src/main/resources/log4j2.xml deleted file mode 100644 index e9bb265..0000000 --- a/basic/gateway/src/main/resources/log4j2.xml +++ /dev/null @@ -1,45 +0,0 @@ -<?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. - --> - -<!--this is sample configuration, please modify as your wish --> - -<configuration> - <Properties> - <Property name="log_path">./gateway/log/</Property> - </Properties> - - <Appenders> - <Console name="Console" target="SYSTEM_OUT"> - <PatternLayout pattern="[%d][%t][%p][%c:%L] %m%n" /> - </Console> - <RollingFile name="DailyRollingFile" fileName="${log_path}/output.log" - filePattern="${log_path}/zcrTest%d{yyyy-MM-dd}.log"> - <PatternLayout pattern="[%d][%t][%p][%c:%L] %m%n" /> - <Policies> - <TimeBasedTriggeringPolicy interval="1" /> - <SizeBasedTriggeringPolicy size="10 MB" /> - </Policies> - </RollingFile> - </Appenders> - <Loggers> - <Root level="info"> - <AppenderRef ref="Console" /> - <AppenderRef ref="DailyRollingFile" /> - </Root> - </Loggers> -</configuration> \ No newline at end of file diff --git a/basic/gateway/src/main/resources/logback.xml b/basic/gateway/src/main/resources/logback.xml new file mode 100644 index 0000000..72c6fbf --- /dev/null +++ b/basic/gateway/src/main/resources/logback.xml @@ -0,0 +1,71 @@ +<?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. + --> + +<configuration scan="true"> + <appender name="metrics" class="ch.qos.logback.core.rolling.RollingFileAppender"> + <file>metrics.log</file> + <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> + <!-- 按天回滚 daily --> + <fileNamePattern>metrics.%d{yyyy-MM-dd}.log</fileNamePattern> + <!-- 日志最大的历史 30天 --> + <maxHistory>60</maxHistory> + <!--用来指定日志文件的上限大小,那么到了这个值,就会删除旧的日志--> + <!--<totalSizeCap>1GB</totalSizeCap>--> + </rollingPolicy> + <encoder> + <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{80} - %msg%n</pattern> + </encoder> + </appender> + + <appender name="servicecomb" class="ch.qos.logback.core.rolling.RollingFileAppender"> + <file>servicecomb.log</file> + <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> + <!-- 按天回滚 daily --> + <fileNamePattern>servicecomb.%d{yyyy-MM-dd}.log</fileNamePattern> + <!-- 日志最大的历史 30天 --> + <maxHistory>60</maxHistory> + <!--用来指定日志文件的上限大小,那么到了这个值,就会删除旧的日志--> + <!--<totalSizeCap>1GB</totalSizeCap>--> + </rollingPolicy> + <encoder> + <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{80} - %msg%n</pattern> + </encoder> + </appender> + + <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> + <!-- If applicable, can use ch.qos.logback.classic.filter.MarkerFilter --> + <filter class="org.apache.servicecomb.foundation.logback.MarkerFilter"> + <Marker>SERVICECOMB_MARKER</Marker> + <OnMismatch>ACCEPT</OnMismatch> + <OnMatch>DENY</OnMatch> + </filter> + + <encoder> + <pattern>%d [%level] [%thread] - %msg (%F:%L\)%n</pattern> + </encoder> + </appender> + + <logger name="org.apache.servicecomb.metrics.core.publish.DefaultLogPublisher" level="INFO" additivity="false"> + <appender-ref ref="metrics"/> + </logger> + + <root level="info"> + <appender-ref ref="STDOUT"/> + <appender-ref ref="servicecomb"/> + </root> +</configuration> \ No newline at end of file diff --git a/basic/provider/src/main/java/org/apache/servicecomb/samples/ProviderController.java b/basic/provider/src/main/java/org/apache/servicecomb/samples/ProviderController.java index 6eea777..cc21be0 100644 --- a/basic/provider/src/main/java/org/apache/servicecomb/samples/ProviderController.java +++ b/basic/provider/src/main/java/org/apache/servicecomb/samples/ProviderController.java @@ -18,6 +18,8 @@ package org.apache.servicecomb.samples; import org.apache.servicecomb.provider.rest.common.RestSchema; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @@ -25,9 +27,12 @@ import org.springframework.web.bind.annotation.RequestParam; @RestSchema(schemaId = "ProviderController") @RequestMapping(path = "/") public class ProviderController { + private static final Logger LOGGER = LoggerFactory.getLogger(ProviderController.class); + // a very simple service to echo the request parameter @GetMapping("/sayHello") public String sayHello(@RequestParam("name") String name) { + LOGGER.info("receive request, name={}", name); return "Hello " + name; } } diff --git a/basic/provider/src/main/resources/application.yml b/basic/provider/src/main/resources/application.yml index ab73b03..3047a61 100644 --- a/basic/provider/src/main/resources/application.yml +++ b/basic/provider/src/main/resources/application.yml @@ -43,6 +43,8 @@ servicecomb: rest: address: 0.0.0.0:9093 # should be same with server.port to use web container + metrics: + window_time: 5000 # Configure AK/SK credentials if needed. Default not enabled. # credentials: # akskEnabled: false diff --git a/basic/provider/src/main/resources/log4j2.xml b/basic/provider/src/main/resources/log4j2.xml deleted file mode 100644 index 56fc9a0..0000000 --- a/basic/provider/src/main/resources/log4j2.xml +++ /dev/null @@ -1,45 +0,0 @@ -<?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. - --> - -<!--this is sample configuration, please modify as your wish --> - -<configuration> - <Properties> - <Property name="log_path">./user/log/</Property> - </Properties> - - <Appenders> - <Console name="Console" target="SYSTEM_OUT"> - <PatternLayout pattern="[%d][%t][%p][%c:%L] %m%n" /> - </Console> - <RollingFile name="DailyRollingFile" fileName="${log_path}/output.log" - filePattern="${log_path}/zcrTest%d{yyyy-MM-dd}.log"> - <PatternLayout pattern="[%d][%t][%p][%c:%L] %m%n" /> - <Policies> - <TimeBasedTriggeringPolicy interval="1" /> - <SizeBasedTriggeringPolicy size="10MB" /> - </Policies> - </RollingFile> - </Appenders> - <Loggers> - <Root level="info"> - <AppenderRef ref="Console" /> - <AppenderRef ref="DailyRollingFile" /> - </Root> - </Loggers> -</configuration> \ No newline at end of file diff --git a/basic/provider/src/main/resources/logback.xml b/basic/provider/src/main/resources/logback.xml new file mode 100644 index 0000000..72c6fbf --- /dev/null +++ b/basic/provider/src/main/resources/logback.xml @@ -0,0 +1,71 @@ +<?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. + --> + +<configuration scan="true"> + <appender name="metrics" class="ch.qos.logback.core.rolling.RollingFileAppender"> + <file>metrics.log</file> + <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> + <!-- 按天回滚 daily --> + <fileNamePattern>metrics.%d{yyyy-MM-dd}.log</fileNamePattern> + <!-- 日志最大的历史 30天 --> + <maxHistory>60</maxHistory> + <!--用来指定日志文件的上限大小,那么到了这个值,就会删除旧的日志--> + <!--<totalSizeCap>1GB</totalSizeCap>--> + </rollingPolicy> + <encoder> + <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{80} - %msg%n</pattern> + </encoder> + </appender> + + <appender name="servicecomb" class="ch.qos.logback.core.rolling.RollingFileAppender"> + <file>servicecomb.log</file> + <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> + <!-- 按天回滚 daily --> + <fileNamePattern>servicecomb.%d{yyyy-MM-dd}.log</fileNamePattern> + <!-- 日志最大的历史 30天 --> + <maxHistory>60</maxHistory> + <!--用来指定日志文件的上限大小,那么到了这个值,就会删除旧的日志--> + <!--<totalSizeCap>1GB</totalSizeCap>--> + </rollingPolicy> + <encoder> + <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{80} - %msg%n</pattern> + </encoder> + </appender> + + <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> + <!-- If applicable, can use ch.qos.logback.classic.filter.MarkerFilter --> + <filter class="org.apache.servicecomb.foundation.logback.MarkerFilter"> + <Marker>SERVICECOMB_MARKER</Marker> + <OnMismatch>ACCEPT</OnMismatch> + <OnMatch>DENY</OnMatch> + </filter> + + <encoder> + <pattern>%d [%level] [%thread] - %msg (%F:%L\)%n</pattern> + </encoder> + </appender> + + <logger name="org.apache.servicecomb.metrics.core.publish.DefaultLogPublisher" level="INFO" additivity="false"> + <appender-ref ref="metrics"/> + </logger> + + <root level="info"> + <appender-ref ref="STDOUT"/> + <appender-ref ref="servicecomb"/> + </root> +</configuration> \ No newline at end of file
