├── src ├── test │ ├── resources │ │ ├── com │ │ │ └── alipay │ │ │ │ └── sofa │ │ │ │ ├── rpc │ │ │ │ ├── log │ │ │ │ │ ├── readme │ │ │ │ │ ├── logback │ │ │ │ │ │ ├── logback_b.xml │ │ │ │ │ │ └── log-conf.xml │ │ │ │ │ ├── log4j2 │ │ │ │ │ │ ├── log4j2_b.xml │ │ │ │ │ │ └── log-conf.xml │ │ │ │ │ └── log4j │ │ │ │ │ │ ├── log-conf.xml │ │ │ │ │ │ └── log4j_b.xml │ │ │ │ └── app │ │ │ │ │ └── log │ │ │ │ │ ├── log4j2 │ │ │ │ │ ├── config.properties │ │ │ │ │ └── log-conf.xml │ │ │ │ │ ├── log4j │ │ │ │ │ └── log-conf.xml │ │ │ │ │ └── logback │ │ │ │ │ └── log-conf.xml │ │ │ │ ├── log │ │ │ │ └── logback │ │ │ │ │ ├── log-conf.xml │ │ │ │ │ └── config.properties │ │ │ │ ├── testover2 │ │ │ │ └── log │ │ │ │ │ └── log4j │ │ │ │ │ ├── config.properties │ │ │ │ │ └── log-conf.xml │ │ │ │ ├── testover3 │ │ │ │ └── log │ │ │ │ │ └── log4j │ │ │ │ │ ├── config.properties │ │ │ │ │ └── log-conf.xml │ │ │ │ ├── concurrency │ │ │ │ └── log │ │ │ │ │ └── logback │ │ │ │ │ └── log-conf.xml │ │ │ │ └── testover1 │ │ │ │ └── log │ │ │ │ └── log4j │ │ │ │ └── log-conf.xml │ │ ├── test-resource-utils.properties │ │ ├── META-INF │ │ │ └── services │ │ │ │ ├── com.alipay.sofa.common.insight.Recorder │ │ │ │ └── com.alipay.sofa.common.tracer.TracerIdRetriever │ │ ├── SOFA-TEST │ │ │ ├── log-codes_zh_CN.properties │ │ │ └── log-codes.properties │ │ ├── sofa │ │ │ └── console │ │ │ │ └── log │ │ │ │ ├── log4j2 │ │ │ │ └── log-conf.xml │ │ │ │ └── logback │ │ │ │ └── log-conf.xml │ │ ├── lazy │ │ │ └── init │ │ │ │ └── log │ │ │ │ └── log4j2 │ │ │ │ └── log-conf.xml │ │ └── thread │ │ │ ├── sofaThreadPoolTaskScheduler.xml │ │ │ └── sofaThreadPoolTaskExecutor.xml │ └── java │ │ └── com │ │ └── alipay │ │ └── sofa │ │ └── common │ │ ├── tracer │ │ ├── SampleTracerIdRetriever.java │ │ └── TracerIdAdapterTest.java │ │ ├── utils │ │ ├── ReportUtilTest.java │ │ ├── TimeWaitRunnerTest.java │ │ ├── ResourceUtilTest.java │ │ ├── AssertUtilTest.java │ │ ├── ClassUtilTest.java │ │ └── CharsetUtilTest.java │ │ ├── recorder │ │ ├── RecorderManagerTest.java │ │ └── SampleRecorder.java │ │ ├── config │ │ ├── ConfigLoggerFactoryTest.java │ │ ├── ConfigSourceTest.java │ │ └── ConfigKeyTest.java │ │ ├── log │ │ ├── exception │ │ │ └── ExceptionTest.java │ │ ├── principle │ │ │ ├── Log4j2Test.java │ │ │ ├── LogbackTest.java │ │ │ └── Log4jTest.java │ │ ├── LoggerSpaceManagerUsage.java │ │ ├── factory │ │ │ ├── UpdateConsoleLoggerWhiteSetTest.java │ │ │ └── LoggerSpaceOverrideUsageTest.java │ │ ├── base │ │ │ └── AbstraceLogTestBase.java │ │ ├── LoggerSpaceConcurrencyTest.java │ │ ├── LoggerSpaceManagerTest.java │ │ ├── LazyLogFactoryInitializingTest.java │ │ ├── LogEnvUtilsTest.java │ │ └── LogbackConsoleLoggingTest.java │ │ ├── thread │ │ ├── bean │ │ │ └── TestTaskDecorator.java │ │ ├── TimeWaitRejectedExecutionHandlerTest.java │ │ ├── SofaTracerCommandFactoryTest.java │ │ └── SofaThreadPoolTaskExecutorTest.java │ │ └── security │ │ └── SensitiveDataUtilTest.java └── main │ ├── resources │ ├── META-INF │ │ └── MANIFEST.MF │ ├── sofa-common-tools │ │ └── log │ │ │ ├── log4j │ │ │ └── log-conf.xml │ │ │ ├── log4j2 │ │ │ └── log-conf.xml │ │ │ └── logback │ │ │ └── log-conf.xml │ └── sofa-thread │ │ └── log │ │ ├── log4j │ │ └── log-conf.xml │ │ ├── log4j2 │ │ └── log-conf.xml │ │ └── logback │ │ └── log-conf.xml │ ├── java │ └── com │ │ └── alipay │ │ └── sofa │ │ └── common │ │ ├── profile │ │ └── enumeration │ │ │ ├── Flags.java │ │ │ ├── internal │ │ │ ├── NumberType.java │ │ │ └── EnumConstant.java │ │ │ └── IntegerEnum.java │ │ ├── log │ │ ├── factory │ │ │ ├── LoggerSpaceFactoryBuilder.java │ │ │ ├── LoggerSpaceFactory4LogbackBuilder.java │ │ │ ├── LoggerSpaceFactory4Log4j2Builder.java │ │ │ └── AbstractLoggerSpaceFactory.java │ │ ├── SpaceId.java │ │ ├── LogSpace.java │ │ ├── adapter │ │ │ └── level │ │ │ │ └── AdapterLevel.java │ │ └── CommonLoggingConfigurations.java │ │ ├── config │ │ ├── source │ │ │ ├── ConfigSourceOrder.java │ │ │ ├── SystemEnvConfigSource.java │ │ │ ├── SystemPropertyConfigSource.java │ │ │ └── AbstractConfigSource.java │ │ ├── Converter.java │ │ ├── ConfigManager.java │ │ ├── converter │ │ │ └── GlobalConverterHolder.java │ │ ├── listener │ │ │ ├── AbstractConfigListener.java │ │ │ └── LogConfigListener.java │ │ ├── ConfigSource.java │ │ ├── ManagementListener.java │ │ ├── log │ │ │ └── ConfigLoggerFactory.java │ │ ├── SofaConfigs.java │ │ └── ConfigKey.java │ │ ├── tracer │ │ ├── TracerIdRetriever.java │ │ └── TracerIdAdapter.java │ │ ├── insight │ │ ├── NoopRecorder.java │ │ ├── Recorder.java │ │ ├── RecordScene.java │ │ └── RecorderManager.java │ │ ├── thread │ │ ├── SofaThreadPoolConstants.java │ │ ├── space │ │ │ ├── ThreadPoolSpace.java │ │ │ └── SpaceNamedThreadFactory.java │ │ ├── NamedThreadFactory.java │ │ └── log │ │ │ └── ThreadLogger.java │ │ ├── utils │ │ ├── ThreadLoggerFactory.java │ │ ├── OrderComparator.java │ │ ├── Ordered.java │ │ ├── ReportUtil.java │ │ ├── TimeWaitRunner.java │ │ └── ProcessIdUtil.java │ │ ├── CommonToolConfigKeys.java │ │ └── space │ │ └── SpaceId.java │ └── java21 │ └── com │ └── alipay │ └── sofa │ └── common │ └── thread │ └── virtual │ └── SofaVirtualThreadFactory.java ├── .gitignore ├── .travis.yml ├── .github ├── PULL_REQUEST_TEMPLATE.md ├── ISSUE_TEMPLATE │ ├── Ask_Question.md │ └── Bug_Report.md └── workflows │ ├── cloud_code_scan.yml │ ├── ci.yml │ └── release.yml ├── HEADER └── CONTRIBUTING.md /src/test/resources/com/alipay/sofa/rpc/log/readme: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/test/resources/com/alipay/sofa/log/logback/log-conf.xml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/test/resources/test-resource-utils.properties: -------------------------------------------------------------------------------- 1 | keyA=A 2 | keyB=B -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | *.iml 3 | target/ 4 | .DS_Store 5 | logs/ 6 | null/ -------------------------------------------------------------------------------- /src/test/resources/com/alipay/sofa/testover2/log/log4j/config.properties: -------------------------------------------------------------------------------- 1 | priority=200 -------------------------------------------------------------------------------- /src/test/resources/com/alipay/sofa/testover3/log/log4j/config.properties: -------------------------------------------------------------------------------- 1 | priority=100 -------------------------------------------------------------------------------- /src/test/resources/com/alipay/sofa/rpc/app/log/log4j2/config.properties: -------------------------------------------------------------------------------- 1 | console=com.foo.Bar -------------------------------------------------------------------------------- /src/test/resources/META-INF/services/com.alipay.sofa.common.insight.Recorder: -------------------------------------------------------------------------------- 1 | com.alipay.sofa.common.recorder.SampleRecorder 2 | -------------------------------------------------------------------------------- /src/test/resources/META-INF/services/com.alipay.sofa.common.tracer.TracerIdRetriever: -------------------------------------------------------------------------------- 1 | com.alipay.sofa.common.tracer.SampleTracerIdRetriever 2 | -------------------------------------------------------------------------------- /src/test/resources/com/alipay/sofa/log/logback/config.properties: -------------------------------------------------------------------------------- 1 | console=testLoggerName1,testLoggerName2 2 | console.prefix=com.test1,com.test2 3 | -------------------------------------------------------------------------------- /src/test/resources/SOFA-TEST/log-codes_zh_CN.properties: -------------------------------------------------------------------------------- 1 | logValueSuffix=, Please see doc http://www.sofaboot.com?errorcode=%s-%s 2 | 00-00000=一切都好 3 | 01-00001=出现了问题 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | sudo: false 3 | 4 | dist: trusty 5 | 6 | jdk: oraclejdk8 7 | 8 | script: mvn clean test 9 | 10 | after_success: 11 | - bash <(curl -s https://codecov.io/bash) 12 | -------------------------------------------------------------------------------- /src/test/resources/SOFA-TEST/log-codes.properties: -------------------------------------------------------------------------------- 1 | logValueSuffix=, Please see doc http://www.sofaboot.com?errorcode=%s-%s 2 | 00-00000=All is well 3 | 01-00001=Some things goes wrong 4 | 02-00000=%s with argument %s 5 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | ### Motivation: 2 | 3 | Explain the context, and why you're making that change. 4 | To make others understand what is the problem you're trying to solve. 5 | 6 | ### Modification: 7 | 8 | Describe the idea and modifications you've done. 9 | 10 | ### Result: 11 | 12 | Fixes #. 13 | 14 | If there is no issue then describe the changes introduced by this PR. 15 | 16 | -------------------------------------------------------------------------------- /src/test/resources/com/alipay/sofa/rpc/log/logback/logback_b.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | %msg%n 6 | 7 | 8 | 9 | 10 | 11 | 12 | 15 | 16 | -------------------------------------------------------------------------------- /src/test/resources/com/alipay/sofa/rpc/log/log4j2/log4j2_b.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/Ask_Question.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Ask Question 3 | about: Ask a question about usage or feature 4 | 5 | --- 6 | 7 | ### Your question 8 | 9 | describe your question clearly 10 | 11 | ### Your scenes 12 | 13 | describe your use scenes (why need this feature) 14 | 15 | ### Your advice 16 | 17 | describe the advice or solution you'd like 18 | 19 | ### Environment 20 | 21 | - sofa-common-tools version: 22 | - JVM version (e.g. `java -version`): 23 | - OS version (e.g. `uname -a`): 24 | - Maven version: 25 | - IDE version: 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/Bug_Report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug Report 3 | about: Create a report to help us improve 4 | 5 | --- 6 | 7 | ### Describe the bug 8 | 9 | A clear and concise description of what the bug is. 10 | 11 | ### Expected behavior 12 | 13 | ### Actual behavior 14 | 15 | ### Steps to reproduce 16 | 17 | ### Minimal yet complete reproducer code (or GitHub URL to code) 18 | 19 | ### Environment 20 | 21 | - sofa-common-tools version: 22 | - JVM version (e.g. `java -version`): 23 | - OS version (e.g. `uname -a`): 24 | - Maven version: 25 | - IDE version: 26 | 27 | -------------------------------------------------------------------------------- /src/test/resources/com/alipay/sofa/rpc/log/logback/log-conf.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 6 | 7 | 8 | 9 | 10 | 11 | 12 | 15 | 16 | -------------------------------------------------------------------------------- /src/test/resources/com/alipay/sofa/concurrency/log/logback/log-conf.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 6 | 7 | 8 | 9 | 10 | 11 | 12 | 15 | 16 | -------------------------------------------------------------------------------- /.github/workflows/cloud_code_scan.yml: -------------------------------------------------------------------------------- 1 | name: Alipay Cloud Devops Codescan 2 | on: 3 | pull_request_target: 4 | jobs: 5 | stc: # Code security scanning 6 | runs-on: ubuntu-latest 7 | steps: 8 | - name: codeScan 9 | uses: layotto/alipay-cloud-devops-codescan@main 10 | with: 11 | parent_uid: ${{ secrets.ALI_PID }} 12 | private_key: ${{ secrets.ALI_PK }} 13 | scan_type: stc 14 | sca: # Open source compliance scanning 15 | runs-on: ubuntu-latest 16 | steps: 17 | - name: codeScan 18 | uses: layotto/alipay-cloud-devops-codescan@main 19 | with: 20 | parent_uid: ${{ secrets.ALI_PID }} 21 | private_key: ${{ secrets.ALI_PK }} 22 | scan_type: sca 23 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a Java project with Maven 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven 3 | 4 | name: build 5 | 6 | on: 7 | push: 8 | branches: 9 | - '**' 10 | pull_request: 11 | branches: 12 | - '**' 13 | 14 | jobs: 15 | build: 16 | runs-on: ubuntu-latest 17 | strategy: 18 | matrix: 19 | jdk: [17, 21] 20 | steps: 21 | - uses: actions/checkout@v2 22 | - name: Set up JDK 23 | uses: actions/setup-java@v1 24 | with: 25 | java-version: ${{ matrix.jdk }} 26 | - name: Test with Maven 27 | run: mvn clean test 28 | - name: Codecov 29 | uses: codecov/codecov-action@v1 30 | -------------------------------------------------------------------------------- /src/test/resources/com/alipay/sofa/rpc/log/log4j2/log-conf.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /HEADER: -------------------------------------------------------------------------------- 1 | Licensed to the Apache Software Foundation (ASF) under one or more 2 | contributor license agreements. See the NOTICE file distributed with 3 | this work for additional information regarding copyright ownership. 4 | The ASF licenses this file to You under the Apache License, Version 2.0 5 | (the "License"); you may not use this file except in compliance with 6 | the License. You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | -------------------------------------------------------------------------------- /src/test/resources/com/alipay/sofa/testover1/log/log4j/log-conf.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /src/test/resources/com/alipay/sofa/testover2/log/log4j/log-conf.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /src/test/resources/com/alipay/sofa/testover3/log/log4j/log-conf.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /src/test/resources/com/alipay/sofa/rpc/app/log/log4j/log-conf.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /src/test/resources/sofa/console/log/log4j2/log-conf.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | %d %-5p %-32t - %m%n %throwable 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /src/main/resources/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Export-Package: com.alipay.sofa.common.log, 3 | com.alipay.sofa.common.log.global, 4 | com.alipay.sofa.common.log.env, 5 | com.alipay.sofa.common.log.factory 6 | Implementation-Title: sofa-middleware-log 7 | Implementation-Version: 1.6.1 8 | Built-By: shangyu 9 | Tool: Bundlor 1.0.0.RELEASE 10 | Bundle-Name: SLF4J API 11 | Created-By: Apache Maven 12 | Bundle-Vendor: SpringSource 13 | Build-Jdk: 1.6.0_16 14 | Bundle-Version: 1.6.1 15 | Bundle-ManifestVersion: 2 16 | Bundle-Description: sofa middleware log adapter 17 | Bundle-SymbolicName: sofa.middleware.log 18 | Import-Package: org.slf4j.impl;version="[1.6.1, 2.0.0)", 19 | org.slf4j;version="[1.6.1, 2.0.0)", 20 | org.apache.log4j, 21 | org.apache.log4j.config, 22 | org.apache.log4j.helpers, 23 | org.apache.log4j.xml, 24 | org.apache.log4j.spi 25 | Archiver-Version: Plexus Archiver 26 | -------------------------------------------------------------------------------- /src/test/resources/sofa/console/log/logback/log-conf.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | true 5 | ${logging.path}/console/common-default.log 6 | 7 | ${logging.path}/console/common-default.log.%d{yyyy-MM-dd} 8 | 30 9 | 10 | 11 | %d %-5p %-32t - %m%n 12 | ${file.encoding} 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /src/test/resources/com/alipay/sofa/rpc/log/log4j/log-conf.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /src/test/resources/com/alipay/sofa/rpc/log/log4j/log4j_b.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /src/test/resources/com/alipay/sofa/rpc/app/log/log4j2/log-conf.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | ${sys:logging.test.path} 6 | ${sys:appname} 7 | 8 | 9 | 10 | 11 | 13 | 14 | 15 | %d %-5p %-32t - %m%n 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /src/test/resources/lazy/init/log/log4j2/log-conf.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 9 | %d [%t] %-5p %c{2} - %m%n %throwable 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/profile/enumeration/Flags.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.profile.enumeration; 18 | 19 | /** 20 | * 21 | * @author luoguimu123 22 | * @version $Id: Flags.java, v 0.1 2017年08月01日 上午11:55 luoguimu123 Exp $ 23 | */ 24 | public interface Flags { 25 | } -------------------------------------------------------------------------------- /src/test/resources/com/alipay/sofa/rpc/app/log/logback/log-conf.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | true 7 | 8 | info 9 | ACCEPT 10 | DENY 11 | 12 | ${logging.test.path}/${appname}/common-default.log 13 | 14 | 15 | 16 | ${logging.test.path}/${appname}/common-default.log.%d{yyyy-MM-dd} 17 | 18 | 30 19 | 20 | 21 | %d %-5p %-32t - %m%n 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/log/factory/LoggerSpaceFactoryBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.log.factory; 18 | 19 | /** 20 | * Created by kevin.luy@alipay.com on 16/9/14. 21 | */ 22 | public interface LoggerSpaceFactoryBuilder { 23 | AbstractLoggerSpaceFactory build(String spaceName, ClassLoader spaceClassloader); 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/log/SpaceId.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.log; 18 | 19 | /** 20 | * 带有扩展属性的Space唯一Id 21 | * 22 | * @author xuanbei 23 | * @since 2017/07/03 24 | */ 25 | @Deprecated 26 | public class SpaceId extends com.alipay.sofa.common.space.SpaceId { 27 | public SpaceId(String spaceName) { 28 | super(spaceName); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/config/source/ConfigSourceOrder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.config.source; 18 | 19 | /** 20 | * @author zhaowang 21 | * @version : ConfigSourceOrder.java, v 0.1 2020年12月01日 1:50 下午 zhaowang Exp $ 22 | */ 23 | public class ConfigSourceOrder { 24 | 25 | public static final int SYSTEM_PROPERTY = 100; 26 | public static final int SYSTEM_ENV = 200; 27 | } -------------------------------------------------------------------------------- /src/test/java/com/alipay/sofa/common/tracer/SampleTracerIdRetriever.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.tracer; 18 | 19 | /** 20 | * @author huzijie 21 | * @version SampleTracerIdAdapter.java, v 0.1 2020年11月11日 5:40 下午 huzijie Exp $ 22 | */ 23 | public class SampleTracerIdRetriever implements TracerIdRetriever { 24 | 25 | @Override 26 | public String getTracerId(Thread thread) { 27 | return "sampleTraceId"; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build and release a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven 3 | 4 | name: Release 5 | 6 | on: 7 | workflow_dispatch: 8 | 9 | 10 | jobs: 11 | release: 12 | runs-on: ubuntu-latest 13 | steps: 14 | - uses: actions/checkout@v3 15 | - name: Set up JDK 21 16 | uses: actions/setup-java@v3 17 | with: 18 | java-version: '21' 19 | distribution: 'temurin' 20 | cache: maven 21 | server-id: ossrh 22 | server-username: MAVEN_USERNAME 23 | server-password: MAVEN_PASSWORD 24 | gpg-private-key: ${{ secrets.MAVEN_GPG_PRIVATE_KEY }} # Value of the GPG private key to import 25 | gpg-passphrase: MAVEN_GPG_PASSPHRASE # env variable for GPG private key passphrase 26 | - name: Build with Maven 27 | run: mvn --batch-mode deploy -DskipTests -Prelease 28 | env: 29 | MAVEN_USERNAME: ${{ secrets.MAVEN_USERNAME }} 30 | MAVEN_PASSWORD: ${{ secrets.MAVEN_PASSWORD }} 31 | MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }} 32 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/tracer/TracerIdRetriever.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.tracer; 18 | 19 | /** 20 | * Retriever for different tracer system 21 | * @author huzijie 22 | * @version TracerIdRetriever.java, v 0.1 2020年11月11日 5:31 下午 huzijie Exp $ 23 | */ 24 | public interface TracerIdRetriever { 25 | 26 | /** 27 | * get trace id from the thread 28 | * @param thread the thread 29 | * @return the trace Id 30 | */ 31 | String getTracerId(Thread thread); 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/config/Converter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.config; 18 | 19 | /** 20 | * @author zhaowang 21 | * @version : Converter.java, v 0.1 2020年12月09日 11:46 上午 zhaowang Exp $ 22 | */ 23 | public interface Converter { 24 | 25 | /** 26 | * Convert value to target type object. 27 | * @param value source value 28 | * @param targetType target type 29 | * @param 30 | * @return object of target type 31 | */ 32 | T convert(String value, Class targetType); 33 | } -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/insight/NoopRecorder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.insight; 18 | 19 | /** 20 | * @author muqingcai 21 | * @version 2024年4月19日 上午9:54:51 22 | */ 23 | public class NoopRecorder implements Recorder { 24 | public static final NoopRecorder INSTANCE = new NoopRecorder(); 25 | 26 | private NoopRecorder() { 27 | } 28 | 29 | @Override 30 | public void start(RecordScene scene, RecordContext context) { 31 | 32 | } 33 | 34 | @Override 35 | public void stop(RecordScene scene, RecordContext context) { 36 | 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/test/resources/thread/sofaThreadPoolTaskScheduler.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/insight/Recorder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.insight; 18 | 19 | /** 20 | * @author muqingcai 21 | * @version 2024年4月11日 下午9:56:03 22 | */ 23 | public interface Recorder { 24 | 25 | /** 26 | * Start record 27 | * 28 | * @param scene the scene 29 | * @param context the context 30 | */ 31 | void start(RecordScene scene, RecordContext context); 32 | 33 | /** 34 | * Stop record 35 | * 36 | * @param scene the scene 37 | * @param context the context 38 | */ 39 | void stop(RecordScene scene, RecordContext context); 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/config/ConfigManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.config; 18 | 19 | /** 20 | * @author zhaowang 21 | * @version : CommonConfig.java, v 0.1 2020年10月20日 7:55 下午 zhaowang Exp $ 22 | */ 23 | public interface ConfigManager { 24 | 25 | T getOrDefault(ConfigKey key); 26 | 27 | T getOrCustomDefault(ConfigKey key, T customDefault); 28 | 29 | T getOrDefaultWithCache(ConfigKey key); 30 | 31 | T getOrCustomDefaultWithCache(ConfigKey key, T customDefault); 32 | 33 | void addConfigSource(ConfigSource configSource); 34 | 35 | void addConfigListener(ManagementListener configListener); 36 | } -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/profile/enumeration/internal/NumberType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.profile.enumeration.internal; 18 | 19 | /** 20 | * 21 | * @author luoguimu123 22 | * @version $Id: NumberType.java, v 0.1 2017年08月01日 上午11:57 luoguimu123 Exp $ 23 | */ 24 | public interface NumberType { 25 | int RADIX_HEX = 16; 26 | int RADIX_OCT = 8; 27 | int RADIX_BIN = 2; 28 | 29 | int intValue(); 30 | 31 | long longValue(); 32 | 33 | float floatValue(); 34 | 35 | double doubleValue(); 36 | 37 | byte byteValue(); 38 | 39 | short shortValue(); 40 | 41 | String toHexString(); 42 | 43 | String toOctalString(); 44 | 45 | String toBinaryString(); 46 | } 47 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/sofa/common/utils/ReportUtilTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.utils; 18 | 19 | import org.junit.Test; 20 | 21 | import static org.junit.Assert.assertFalse; 22 | 23 | /** 24 | * ReportUtil Tester. 25 | * 26 | * @author 27 | * @version 1.0 28 | * @since 18/06/04 29 | */ 30 | public class ReportUtilTest { 31 | 32 | @Test 33 | public void testUtils() { 34 | String errMsg = "Some Error Msg"; 35 | boolean isException = false; 36 | try { 37 | ReportUtil.reportError("RuntimeException", new RuntimeException()); 38 | } catch (Exception ex) { 39 | isException = true; 40 | } 41 | assertFalse(isException); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/test/resources/thread/sofaThreadPoolTaskExecutor.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/config/converter/GlobalConverterHolder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.config.converter; 18 | 19 | import com.alipay.sofa.common.config.Converter; 20 | 21 | /** 22 | * @author zhaowang 23 | * @version : GlobalConverterHolder.java, v 0.1 2020年12月09日 11:51 上午 zhaowang Exp $ 24 | */ 25 | public class GlobalConverterHolder { 26 | 27 | public static final Converter DEFAULT_CONVERTER = new DefaultConverter(); 28 | private static volatile Converter converter = DEFAULT_CONVERTER; 29 | 30 | public static Converter getGlobalConverter() { 31 | return converter; 32 | } 33 | 34 | public static void setConverter(Converter converter) { 35 | synchronized (GlobalConverterHolder.class) { 36 | GlobalConverterHolder.converter = converter; 37 | } 38 | } 39 | } -------------------------------------------------------------------------------- /src/main/resources/sofa-common-tools/log/log4j/log-conf.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/sofa/common/recorder/RecorderManagerTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.recorder; 18 | 19 | import com.alipay.sofa.common.insight.NoopRecorder; 20 | import com.alipay.sofa.common.insight.Recorder; 21 | import com.alipay.sofa.common.insight.RecorderManager; 22 | import org.junit.Assert; 23 | import org.junit.Test; 24 | 25 | /** 26 | * @author muqingcai 27 | * @version 2024年5月13日 下午12:45:52 28 | */ 29 | public class RecorderManagerTest { 30 | @Test 31 | public void testGetRecorder() { 32 | Recorder recorderBeforeInit = RecorderManager.getRecorder(); 33 | Assert.assertTrue(recorderBeforeInit instanceof NoopRecorder); 34 | 35 | RecorderManager.init(); 36 | Recorder recorderAfterInit = RecorderManager.getRecorder(); 37 | Assert.assertTrue(recorderAfterInit instanceof SampleRecorder); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/thread/SofaThreadPoolConstants.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.thread; 18 | 19 | /** 20 | * The constants for the Sofa Thread Pool 21 | * @author Alaneuler 22 | * Created on 2020/3/19 23 | */ 24 | public class SofaThreadPoolConstants { 25 | 26 | public static final String SOFA_THREAD_POOL_LOGGING_CAPABILITY = "sofa_thread_pool_logging_capability"; 27 | 28 | public static final long DEFAULT_TASK_TIMEOUT = 30000; 29 | 30 | public static final long DEFAULT_PERIOD = 5000; 31 | 32 | public static final long DEFAULT_GOVERNOR_INTERVAL = 30; 33 | 34 | public static final boolean DEFAULT_GOVERNOR_LOGGER_ENABLE = false; 35 | 36 | public static final boolean DEFAULT_GLOBAL_MONITOR_LOGGER_ENABLE = true; 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/sofa/common/utils/TimeWaitRunnerTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.utils; 18 | 19 | import org.junit.Assert; 20 | import org.junit.Test; 21 | 22 | import java.util.concurrent.atomic.AtomicLong; 23 | 24 | /** 25 | * @author zhaowang 26 | * @version : TimeWaitRunnerTest.java, v 0.1 2020年08月20日 11:08 下午 zhaowang Exp $ 27 | */ 28 | public class TimeWaitRunnerTest { 29 | 30 | @Test 31 | public void testTimeWait() throws InterruptedException { 32 | TimeWaitRunner timeWaitLogger = new TimeWaitRunner(1000); 33 | AtomicLong atomicLong = new AtomicLong(); 34 | new Thread(()->{ 35 | while (true){ 36 | timeWaitLogger.doWithRunnable(atomicLong::incrementAndGet); 37 | } 38 | }).start(); 39 | Thread.sleep(1500); 40 | Assert.assertEquals(2L,atomicLong.get()); 41 | } 42 | } -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/config/source/SystemEnvConfigSource.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.config.source; 18 | 19 | import com.alipay.sofa.common.utils.StringUtil; 20 | 21 | /** 22 | * @author zhaowang 23 | * @version : SystemEnvConfigSource.java, v 0.1 2020年12月01日 1:42 下午 zhaowang Exp $ 24 | */ 25 | public class SystemEnvConfigSource extends AbstractConfigSource { 26 | @Override 27 | public String doGetConfig(String key) { 28 | return System.getenv(key); 29 | } 30 | 31 | @Override 32 | public boolean hasKey(String key) { 33 | String env = System.getenv(key); 34 | return StringUtil.isNotBlank(env); 35 | } 36 | 37 | @Override 38 | public String getName() { 39 | return "SystemEnv"; 40 | } 41 | 42 | @Override 43 | public int getOrder() { 44 | return ConfigSourceOrder.SYSTEM_ENV; 45 | } 46 | } -------------------------------------------------------------------------------- /src/test/java/com/alipay/sofa/common/utils/ResourceUtilTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.utils; 18 | 19 | import org.junit.Assert; 20 | import org.junit.Test; 21 | 22 | import java.io.File; 23 | import java.io.FileInputStream; 24 | import java.net.URL; 25 | import java.util.Properties; 26 | 27 | /** 28 | * @author qilong.zql 29 | * @since 1.0.17 30 | */ 31 | public class ResourceUtilTest { 32 | 33 | @Test 34 | public void testGetFile() throws Exception { 35 | URL url = this.getClass().getClassLoader().getResource("test-resource-utils.properties"); 36 | File file = ResourceUtil.getFile(url); 37 | Properties properties = new Properties(); 38 | properties.load(new FileInputStream(file)); 39 | Assert.assertEquals(properties.getProperty("keyA"), "A"); 40 | Assert.assertEquals(properties.getProperty("keyB"), "B"); 41 | } 42 | 43 | } -------------------------------------------------------------------------------- /src/test/java/com/alipay/sofa/common/recorder/SampleRecorder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.recorder; 18 | 19 | import com.alipay.sofa.common.insight.RecordContext; 20 | import com.alipay.sofa.common.insight.RecordScene; 21 | import com.alipay.sofa.common.insight.Recorder; 22 | 23 | import java.io.PrintStream; 24 | 25 | /** 26 | * @author muqingcai 27 | * @version 2024年5月13日 下午12:45:58 28 | */ 29 | public class SampleRecorder implements Recorder { 30 | private static final PrintStream TTY = System.out; 31 | 32 | @Override 33 | public void start(RecordScene scene, RecordContext context) { 34 | TTY.println("Recorder start, scene: " + scene + " context: " + context); 35 | } 36 | 37 | @Override 38 | public void stop(RecordScene scene, RecordContext context) { 39 | TTY.println("Recorder stop, scene: " + scene + " context: " + context); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/utils/ThreadLoggerFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.utils; 18 | 19 | import com.alipay.sofa.common.log.LoggerSpaceManager; 20 | import org.slf4j.Logger; 21 | 22 | /** 23 | * @author Alaneuler 24 | * Created on 2020/3/19 25 | */ 26 | public class ThreadLoggerFactory { 27 | private static final String COMMON_THREAD_LOG_SPACE = "sofa-thread"; 28 | 29 | public static Logger getLogger(String name) { 30 | if (StringUtil.isEmpty(name)) { 31 | return null; 32 | } 33 | 34 | return LoggerSpaceManager.getLoggerBySpace(name, COMMON_THREAD_LOG_SPACE); 35 | } 36 | 37 | public static Logger getLogger(Class klass) { 38 | if (klass == null) { 39 | return null; 40 | } 41 | 42 | return getLogger(klass.getCanonicalName()); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/config/source/SystemPropertyConfigSource.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.config.source; 18 | 19 | import com.alipay.sofa.common.utils.StringUtil; 20 | 21 | /** 22 | * @author zhaowang 23 | * @version : SystemPropertyConfigSource.java, v 0.1 2020年10月21日 2:39 下午 zhaowang Exp $ 24 | */ 25 | public class SystemPropertyConfigSource extends AbstractConfigSource { 26 | @Override 27 | public String doGetConfig(String key) { 28 | return System.getProperty(key); 29 | } 30 | 31 | @Override 32 | public boolean hasKey(String key) { 33 | String property = System.getProperty(key); 34 | return StringUtil.isNotBlank(property); 35 | } 36 | 37 | //数字越小,优先级越高 38 | @Override 39 | public int getOrder() { 40 | return ConfigSourceOrder.SYSTEM_PROPERTY; 41 | } 42 | 43 | @Override 44 | public String getName() { 45 | return "SystemProperty"; 46 | } 47 | } -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/config/listener/AbstractConfigListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.config.listener; 18 | 19 | import com.alipay.sofa.common.config.ConfigKey; 20 | import com.alipay.sofa.common.config.ConfigSource; 21 | import com.alipay.sofa.common.config.ManagementListener; 22 | 23 | import java.util.List; 24 | 25 | /** 26 | * AbstractConfigListener provides empty implements of ConfigListener. 27 | * @author zhaowang 28 | * @version : AbstractConfigListener.java, v 0.1 2020年12月07日 9:21 下午 zhaowang Exp $ 29 | */ 30 | public abstract class AbstractConfigListener implements ManagementListener { 31 | @Override 32 | public void beforeConfigLoading(ConfigKey configKey, List configSources) { 33 | 34 | } 35 | 36 | @Override 37 | public void afterConfigLoaded(ConfigKey key, ConfigSource configSource, 38 | List configSourceList) { 39 | } 40 | 41 | @Override 42 | public void onLoadDefaultValue(ConfigKey key, Object defaultValue) { 43 | } 44 | } -------------------------------------------------------------------------------- /src/main/resources/sofa-thread/log/log4j/log-conf.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/sofa/common/config/ConfigLoggerFactoryTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.config; 18 | 19 | import com.alipay.sofa.common.config.log.ConfigLoggerFactory; 20 | import org.junit.Assert; 21 | import org.junit.Test; 22 | import org.slf4j.Logger; 23 | 24 | /** 25 | * @author zhaowang 26 | * @version : ConfigLoggerFactoryTest.java, v 0.1 2020年12月02日 2:50 下午 zhaowang Exp $ 27 | */ 28 | public class ConfigLoggerFactoryTest { 29 | 30 | @Test 31 | public void test() { 32 | String name = null; 33 | Logger logger = ConfigLoggerFactory.getLogger(name); 34 | Assert.assertNull(logger); 35 | name = "com"; 36 | logger = ConfigLoggerFactory.getLogger(name); 37 | Assert.assertNotNull(logger); 38 | 39 | Class klass = null; 40 | logger = ConfigLoggerFactory.getLogger(klass); 41 | Assert.assertNull(logger); 42 | 43 | klass = ConfigLoggerFactoryTest.class; 44 | logger = ConfigLoggerFactory.getLogger(klass); 45 | Assert.assertNotNull(logger); 46 | 47 | } 48 | } -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/config/ConfigSource.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.config; 18 | 19 | import com.alipay.sofa.common.utils.Ordered; 20 | 21 | /** 22 | * @author zhaowang 23 | * @version : ConfigSource.java, v 0.1 2020年10月20日 7:57 下午 zhaowang Exp $ 24 | * 25 | */ 26 | public interface ConfigSource extends Ordered { 27 | 28 | /** 29 | * Get config by configKey. If no value exists, return null or empty string 30 | * @param key ConfigKey 31 | * @param 32 | * @return value of key 33 | */ 34 | T getConfig(ConfigKey key); 35 | 36 | /** 37 | * @return The name of ConfigSource 38 | */ 39 | String getName(); 40 | 41 | /** 42 | * Get the string value of key 43 | * @param key ConfigKey 44 | * @return string value of key 45 | */ 46 | String getStringConfig(ConfigKey key); 47 | 48 | /** 49 | * The actual effective key to get the value. 50 | * @param configKey ConfigKey 51 | * @return 52 | */ 53 | String getEffectiveKey(ConfigKey configKey); 54 | 55 | } -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/insight/RecordScene.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.insight; 18 | 19 | /** 20 | * @author muqingcai 21 | * @version 2024年4月19日 上午9:55:54 22 | */ 23 | public enum RecordScene { 24 | /** 25 | * record 场景 26 | */ 27 | BOLT_REQUEST_HANDLE("boltRequestHandle", "bolt 协议 RPC 请求处理"), 28 | 29 | TR_REQUEST_HANDLE("trRequestHandle", "tr 协议 RPC 请求处理"), 30 | 31 | SOFA_STARTUP("sofaStartup", "应用启动"), 32 | 33 | SPRING_BEAN_REFRESH("springBeanRefresh", "应用 bean 刷新"); 34 | 35 | private final String scene; 36 | private final String desc; 37 | 38 | RecordScene(String scene, String desc) { 39 | this.scene = scene; 40 | this.desc = desc; 41 | } 42 | 43 | /** 44 | * Gets get scene. 45 | * 46 | * @return the get scene 47 | */ 48 | public String getScene() { 49 | return scene; 50 | } 51 | 52 | /** 53 | * Gets get desc. 54 | * 55 | * @return the get desc 56 | */ 57 | public String getDesc() { 58 | return desc; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/sofa/common/log/exception/ExceptionTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.log.exception; 18 | 19 | import com.alipay.sofa.common.utils.ReportUtil; 20 | import com.alipay.sofa.common.log.base.AbstraceLogTestBase; 21 | import org.junit.Test; 22 | 23 | import java.io.ByteArrayOutputStream; 24 | import java.io.PrintStream; 25 | 26 | import static org.junit.Assert.assertTrue; 27 | 28 | /** 29 | * ExceptionTest 30 | * 31 | * @author yangguanchao 32 | * @since 2017/11/17 33 | */ 34 | public class ExceptionTest extends AbstraceLogTestBase { 35 | 36 | @Test 37 | public void testException() throws InterruptedException { 38 | 39 | final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); 40 | System.setErr(new PrintStream(outContent)); 41 | 42 | Exception exception = new RuntimeException("RuntimeException"); 43 | ReportUtil.reportError("hehe", exception); 44 | 45 | assertTrue(outContent.toString().contains( 46 | "com.alipay.sofa.common.log.exception.ExceptionTest.testException")); 47 | 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | ## Contributing to SOFA Common Tools 2 | 3 | SOFA Common Tools is released under the Apache 2.0 license, and follows a very 4 | standard Github development process, using Github tracker for issues and 5 | merging pull requests into master. If you would like to contribute something, 6 | or simply want to hack on the code this document should help you get started. 7 | 8 | ### Sign the Contributor License Agreement 9 | 10 | Before we accept a non-trivial patch or pull request we will need you to 11 | sign the Contributor License Agreement. Signing the contributor’s agreement 12 | does not grant anyone commit rights to the main repository, but it does mean 13 | that we can accept your contributions, and you will get an author credit if 14 | we do. Active contributors might be asked to join the core team, and given 15 | the ability to merge pull requests. 16 | 17 | ### Code Conventions 18 | 19 | None of these is essential for a pull request, but they will all help. 20 | 21 | 1. we provided a [code formatter file](./Formatter.xml), it will formatting 22 | automatically your project when during process of building. 23 | 24 | 2. Make sure all new `.java` files to have a simple Javadoc class comment 25 | with at least an `@author` tag identifying you, and preferably at least a 26 | paragraph on what the class is for. 27 | 28 | 3. Add the ASF license header comment to all new `.java` files (copy from existing files in the project) 29 | 30 | 4. Add yourself as an `@author` to the `.java` files that you modify substantially (more than cosmetic changes). 31 | 32 | 5. Add some Javadocs. 33 | 34 | 6. A few unit tests would help a lot as well — someone has to do it. 35 | 36 | 7. When writing a commit message please follow [these conventions](https://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html), if 37 | you are fixing an existing issue please add Fixes gh-XXXX at the end 38 | of the commit message (where XXXX is the issue number). 39 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/thread/space/ThreadPoolSpace.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.thread.space; 18 | 19 | import java.util.Collections; 20 | import java.util.HashSet; 21 | import java.util.Set; 22 | import java.util.concurrent.atomic.AtomicInteger; 23 | 24 | /** 25 | * @author huzijie 26 | * @version ThreadPoolNamespace.java, v 0.1 2020年11月11日 10:43 上午 huzijie Exp $ 27 | */ 28 | public class ThreadPoolSpace { 29 | 30 | private final Set threadPoolIdentities = Collections.synchronizedSet(new HashSet<>()); 31 | 32 | private final AtomicInteger threadPoolNumber = new AtomicInteger(0); 33 | 34 | public void addThreadPool(String identify) { 35 | this.threadPoolIdentities.add(identify); 36 | } 37 | 38 | public void removeThreadPool(String identify) { 39 | this.threadPoolIdentities.remove(identify); 40 | } 41 | 42 | public Set getThreadPoolIdentities() { 43 | return threadPoolIdentities; 44 | } 45 | 46 | public int getThreadPoolNumber() { 47 | return threadPoolNumber.getAndIncrement(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/sofa/common/thread/bean/TestTaskDecorator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.thread.bean; 18 | 19 | import org.springframework.core.task.TaskDecorator; 20 | 21 | import java.util.concurrent.atomic.AtomicInteger; 22 | 23 | /** 24 | * @author huzijie 25 | * @version TestTaskDecorator.java, v 0.1 2020年11月09日 5:06 下午 huzijie Exp $ 26 | */ 27 | public class TestTaskDecorator implements TaskDecorator { 28 | public static AtomicInteger count = new AtomicInteger(); 29 | 30 | @Override 31 | public Runnable decorate(Runnable runnable) { 32 | return new TestRunnable(runnable); 33 | } 34 | 35 | public static class TestRunnable implements Runnable { 36 | 37 | private final Runnable runnable; 38 | 39 | public TestRunnable(Runnable runnable) { 40 | this.runnable = runnable; 41 | } 42 | 43 | @Override 44 | public void run() { 45 | count.incrementAndGet(); 46 | runnable.run(); 47 | } 48 | } 49 | 50 | public static void clearCount() { 51 | count.set(0); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/sofa/common/tracer/TracerIdAdapterTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.tracer; 18 | 19 | import org.junit.Assert; 20 | import org.junit.Test; 21 | 22 | import java.lang.reflect.Field; 23 | 24 | /** 25 | * @author huzijie 26 | * @version TracerIdConverter.java, v 0.1 2020年11月11日 5:40 下午 huzijie Exp $ 27 | */ 28 | public class TracerIdAdapterTest { 29 | 30 | @Test 31 | public void testTraceIdConverter() throws NoSuchFieldException, IllegalAccessException { 32 | TracerIdAdapter converter = TracerIdAdapter.getInstance(); 33 | String traceId = converter.traceIdSafari(Thread.currentThread()); 34 | Assert.assertEquals("sampleTraceId", traceId); 35 | Field field = TracerIdAdapter.class.getDeclaredField("tracerIdRetriever"); 36 | field.setAccessible(true); 37 | TracerIdRetriever tracerIdRetriever = (TracerIdRetriever) field.get(converter); 38 | field.set(converter, null); 39 | traceId = converter.traceIdSafari(Thread.currentThread()); 40 | Assert.assertNull(traceId); 41 | field.set(converter, tracerIdRetriever); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/sofa/common/utils/AssertUtilTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.utils; 18 | 19 | import org.junit.Test; 20 | 21 | /** 22 | * AssertUtil Tester. 23 | * 24 | * @author 25 | * @version 1.0 26 | * @since 18/06/04 27 | */ 28 | public class AssertUtilTest { 29 | 30 | /** 31 | * Method: isTrue(boolean expression, String message) 32 | */ 33 | @Test 34 | public void testIsTrueForExpressionMessage() throws Exception { 35 | boolean isSuccess = false; 36 | AssertUtil.isTrue(!isSuccess, "isTrue"); 37 | boolean isException = false; 38 | try { 39 | AssertUtil.isTrue(isSuccess, "isTrue"); 40 | } catch (Exception ex) { 41 | isException = true; 42 | } 43 | AssertUtil.isTrue(isException); 44 | } 45 | 46 | /** 47 | * Method: isNull(Object object, String message) 48 | */ 49 | @Test 50 | public void testIsNullForObjectMessage() throws Exception { 51 | Object object = null; 52 | AssertUtil.isNull(object, "null"); 53 | AssertUtil.isNull(object); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/log/factory/LoggerSpaceFactory4LogbackBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.log.factory; 18 | 19 | import ch.qos.logback.classic.LoggerContext; 20 | import com.alipay.sofa.common.log.LogSpace; 21 | import com.alipay.sofa.common.space.SpaceId; 22 | 23 | import java.net.URL; 24 | 25 | /** 26 | * Created by kevin.luy@alipay.com on 16/9/14. 27 | * Updated by guanchao.ygc@alibaba-inc.com on 14/04/28. 28 | */ 29 | public class LoggerSpaceFactory4LogbackBuilder extends AbstractLoggerSpaceFactoryBuilder { 30 | 31 | public LoggerSpaceFactory4LogbackBuilder(SpaceId spaceId, LogSpace logSpace) { 32 | super(spaceId, logSpace); 33 | } 34 | 35 | @Override 36 | public AbstractLoggerSpaceFactory doBuild(String spaceName, ClassLoader spaceClassloader, 37 | URL url) { 38 | return new LogbackLoggerSpaceFactory(getSpaceId(), new LoggerContext(), getProperties(), 39 | url, getLoggingToolName()); 40 | } 41 | 42 | @Override 43 | protected String getLoggingToolName() { 44 | return "logback"; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/sofa/common/utils/ClassUtilTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.utils; 18 | 19 | import org.junit.Assert; 20 | import org.junit.Test; 21 | import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; 22 | 23 | /** 24 | * @author Alaneuler 25 | * Created on 2020/3/23 26 | */ 27 | public class ClassUtilTest { 28 | @Test 29 | public void getFieldTest() { 30 | ThreadPoolTaskExecutor threadPool = new ThreadPoolTaskExecutor(); 31 | Integer capacity = ClassUtil.getField("queueCapacity", threadPool); 32 | if (capacity != null) { 33 | Assert.assertEquals(Integer.MAX_VALUE, (long) capacity); 34 | } 35 | } 36 | 37 | @Test 38 | public void setFieldTest() { 39 | ThreadPoolTaskExecutor threadPool = new ThreadPoolTaskExecutor(); 40 | ClassUtil.setField("queueCapacity", threadPool, 10); 41 | Integer capacity = ClassUtil.getField("queueCapacity", threadPool); 42 | if (capacity != null) { 43 | Assert.assertEquals(10, (long) capacity); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/utils/OrderComparator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.utils; 18 | 19 | import java.util.Arrays; 20 | import java.util.Comparator; 21 | import java.util.List; 22 | 23 | /** 24 | * @author zhaowang 25 | * @version : OrderComparator.java, v 0.1 2020年10月23日 10:11 上午 zhaowang Exp $ 26 | */ 27 | public class OrderComparator implements Comparator { 28 | public static final OrderComparator INSTANCE = new OrderComparator(); 29 | 30 | public OrderComparator() { 31 | } 32 | 33 | public int compare(Ordered o1, Ordered o2) { 34 | return this.doCompare(o1, o2); 35 | } 36 | 37 | private int doCompare(Ordered o1, Ordered o2) { 38 | int i1 = o1.getOrder(); 39 | int i2 = o2.getOrder(); 40 | return Integer.compare(i1, i2); 41 | } 42 | 43 | public static void sort(List list) { 44 | if (list.size() > 1) { 45 | list.sort(INSTANCE); 46 | } 47 | } 48 | 49 | public static void sort(Ordered[] array) { 50 | if (array.length > 1) { 51 | Arrays.sort(array, INSTANCE); 52 | } 53 | 54 | } 55 | } -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/utils/Ordered.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.utils; 18 | 19 | /** 20 | * @author zhaowang 21 | * @version : Ordered.java, v 0.1 2020年10月23日 10:10 上午 zhaowang Exp $ 22 | */ 23 | public interface Ordered { 24 | 25 | /** 26 | * Useful constant for the highest precedence value. 27 | * @see java.lang.Integer#MIN_VALUE 28 | */ 29 | int HIGHEST_PRECEDENCE = Integer.MIN_VALUE; 30 | 31 | /** 32 | * Useful constant for the lowest precedence value. 33 | * @see java.lang.Integer#MAX_VALUE 34 | */ 35 | int LOWEST_PRECEDENCE = Integer.MAX_VALUE; 36 | 37 | /** 38 | * Get the order value of this object. 39 | *

Higher values are interpreted as lower priority. As a consequence, 40 | * the object with the lowest value has the highest priority (somewhat 41 | * analogous to Servlet {@code load-on-startup} values). 42 | *

Same order values will result in arbitrary sort positions for the 43 | * affected objects. 44 | * @return the order value 45 | * @see #HIGHEST_PRECEDENCE 46 | * @see #LOWEST_PRECEDENCE 47 | */ 48 | int getOrder(); 49 | 50 | } -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/config/ManagementListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.config; 18 | 19 | import com.alipay.sofa.common.utils.Ordered; 20 | 21 | import java.util.List; 22 | 23 | /** 24 | * @author zhaowang 25 | * @version : ConfigListener.java, v 0.1 2020年12月01日 11:43 上午 zhaowang Exp $ 26 | */ 27 | public interface ManagementListener extends Ordered { 28 | 29 | /** 30 | * Do some thing before loading config 31 | * @param configKey 32 | * @param configSources 33 | */ 34 | void beforeConfigLoading(ConfigKey configKey, List configSources); 35 | 36 | /** 37 | * Do something when loaded config 38 | * @param key key of config 39 | * @param configSource config source which config loads from 40 | * @param configSourceList all config sources 41 | */ 42 | void afterConfigLoaded(ConfigKey key, ConfigSource configSource, 43 | List configSourceList); 44 | 45 | /** 46 | * Do something when loaded defaultValue 47 | * @param key 48 | * @param defaultValue 49 | */ 50 | void onLoadDefaultValue(ConfigKey key, Object defaultValue); 51 | } -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/config/log/ConfigLoggerFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.config.log; 18 | 19 | import com.alipay.sofa.common.log.LoggerSpaceManager; 20 | import com.alipay.sofa.common.utils.StringUtil; 21 | import org.slf4j.Logger; 22 | 23 | /** 24 | * @author zhaowang 25 | * @version : ConfigLoggerFactory.java, v 0.1 2020年12月01日 2:33 下午 zhaowang Exp $ 26 | */ 27 | public class ConfigLoggerFactory { 28 | 29 | public static final Logger CONFIG_COMMON_DIGEST_LOGGER = ConfigLoggerFactory 30 | .getLogger("com.alipay.sofa.common.config.digest"); 31 | 32 | private static final String COMMON_CONFIG_LOG_SPACE = "sofa-common-tools"; 33 | 34 | public static Logger getLogger(String name) { 35 | if (StringUtil.isEmpty(name)) { 36 | return null; 37 | } 38 | 39 | return LoggerSpaceManager.getLoggerBySpace(name, COMMON_CONFIG_LOG_SPACE); 40 | } 41 | 42 | public static Logger getLogger(Class klass) { 43 | if (klass == null) { 44 | return null; 45 | } 46 | 47 | return getLogger(klass.getCanonicalName()); 48 | } 49 | 50 | } -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/utils/ReportUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.utils; 18 | 19 | import com.alipay.sofa.common.log.LogLog; 20 | 21 | /** 22 | * ReportUtil 23 | *

24 | * 参照:slf4j 输出信息到控制台及默认(业务app中配置的)日志 25 | *

26 | * Created by yangguanchao on 16/9/24. 27 | */ 28 | public class ReportUtil { 29 | /** 30 | * print Debug message 31 | * @param msg message 32 | */ 33 | public static void reportDebug(String msg) { 34 | LogLog.debug(msg); 35 | } 36 | 37 | /** 38 | * print Info message 39 | * @param msg message 40 | */ 41 | public static void reportInfo(String msg) { 42 | LogLog.info(msg); 43 | } 44 | 45 | /** 46 | * print Warn message 47 | * @param msg message 48 | */ 49 | public static void reportWarn(String msg) { 50 | LogLog.warn(msg); 51 | } 52 | 53 | public static void reportWarn(String msg, Throwable e) { 54 | LogLog.warn(msg, e); 55 | } 56 | 57 | public static void reportError(String msg, Throwable throwable) { 58 | LogLog.error(msg, throwable); 59 | } 60 | 61 | public static void reportError(String msg) { 62 | LogLog.error(msg, null); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/log/factory/LoggerSpaceFactory4Log4j2Builder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.log.factory; 18 | 19 | import com.alipay.sofa.common.log.LogSpace; 20 | import com.alipay.sofa.common.space.SpaceId; 21 | 22 | import java.net.URL; 23 | 24 | /** 25 | * Created by kevin.luy@alipay.com on 16/9/14. 26 | * Updated by guanchao.ygc@alibaba-inc.com on 14/04/28. 27 | */ 28 | public class LoggerSpaceFactory4Log4j2Builder extends AbstractLoggerSpaceFactoryBuilder { 29 | 30 | public LoggerSpaceFactory4Log4j2Builder(SpaceId spaceId, LogSpace logSpace) { 31 | super(spaceId, logSpace); 32 | } 33 | 34 | @Override 35 | protected String getLoggingToolName() { 36 | return "log4j2"; 37 | } 38 | 39 | @Override 40 | public AbstractLoggerSpaceFactory doBuild(String spaceName, ClassLoader spaceClassloader, 41 | URL url) { 42 | try { 43 | return new Log4j2LoggerSpaceFactory(getSpaceId(), getProperties(), url, 44 | getLoggingToolName()); 45 | } catch (Throwable e) { 46 | throw new IllegalStateException("Log4j2 loggerSpaceFactory build error!", e); 47 | } 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/main/resources/sofa-common-tools/log/log4j2/log-conf.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ${sys:logging.level.sofa-common-tools} 5 | ${sys:logging.path} 6 | ${sys:file.encoding} 7 | 8 | 9 | 10 | 12 | 13 | 14 | %d %-5p %-5t - %m%n 15 | 16 | 17 | 18 | 19 | 20 | 21 | 23 | 24 | 25 | %d %-5p %-5t - %m%n 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/utils/TimeWaitRunner.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.utils; 18 | 19 | /** 20 | * @author zhaowang 21 | * @version : TimeWaitLogger.java, v 0.1 2020年08月20日 7:46 下午 zhaowang Exp $ 22 | */ 23 | public class TimeWaitRunner { 24 | private final long waitTime; 25 | private final boolean runImmediately; 26 | 27 | private volatile long lastLogTime; 28 | 29 | public TimeWaitRunner(long waitTimeMills) { 30 | this.waitTime = waitTimeMills; 31 | this.runImmediately = false; 32 | } 33 | 34 | public TimeWaitRunner(long waitTimeMills, boolean runImmediately) { 35 | this.waitTime = waitTimeMills; 36 | this.runImmediately = runImmediately; 37 | } 38 | 39 | public void doWithRunnable(Runnable runnable) { 40 | long currentTimeMillis = System.currentTimeMillis(); 41 | if (runImmediately) { 42 | runnable.run(); 43 | } else if (currentTimeMillis > lastLogTime + waitTime) { 44 | synchronized (this) { 45 | if (currentTimeMillis > lastLogTime + waitTime) { 46 | lastLogTime = currentTimeMillis; 47 | runnable.run(); 48 | } 49 | } 50 | 51 | } 52 | 53 | } 54 | 55 | } -------------------------------------------------------------------------------- /src/test/java/com/alipay/sofa/common/config/ConfigSourceTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.config; 18 | 19 | import com.alipay.sofa.common.config.source.AbstractConfigSource; 20 | import org.junit.Assert; 21 | import org.junit.Test; 22 | 23 | import static com.alipay.sofa.common.CommonToolConfigKeys.COMMON_THREAD_LOG_PERIOD; 24 | 25 | /** 26 | * @author zhaowang 27 | * @version : ConfigSourceTest.java, v 0.1 2020年12月09日 11:17 上午 zhaowang Exp $ 28 | */ 29 | public class ConfigSourceTest { 30 | 31 | @Test 32 | public void testGetEffectiveKey() { 33 | TestConfigSource testConfigSource = new TestConfigSource(); 34 | String effectiveKey = testConfigSource.getEffectiveKey(COMMON_THREAD_LOG_PERIOD); 35 | Assert.assertEquals("", effectiveKey); 36 | 37 | } 38 | 39 | public static class TestConfigSource extends AbstractConfigSource { 40 | 41 | @Override 42 | public String doGetConfig(String key) { 43 | return null; 44 | } 45 | 46 | @Override 47 | public boolean hasKey(String key) { 48 | return false; 49 | } 50 | 51 | @Override 52 | public String getName() { 53 | return "TestConfigSource"; 54 | } 55 | 56 | @Override 57 | public int getOrder() { 58 | return -1; 59 | } 60 | } 61 | } -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/thread/NamedThreadFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.thread; 18 | 19 | import java.util.concurrent.ThreadFactory; 20 | import java.util.concurrent.atomic.AtomicInteger; 21 | 22 | /** 23 | * @author qilong.zql 24 | * @author ruoshan 25 | * @since 2.6.0 26 | */ 27 | public class NamedThreadFactory implements ThreadFactory { 28 | 29 | private static final AtomicInteger POOL_NUMBER = new AtomicInteger(1); 30 | 31 | final AtomicInteger threadNumber = new AtomicInteger(1); 32 | 33 | final ThreadGroup group; 34 | 35 | final String namePrefix; 36 | 37 | final boolean isDaemon; 38 | 39 | public NamedThreadFactory(String name) { 40 | this(name, false); 41 | } 42 | 43 | public NamedThreadFactory(String prefix, boolean daemon) { 44 | group = Thread.currentThread().getThreadGroup(); 45 | namePrefix = prefix + "-" + POOL_NUMBER.getAndIncrement() + "-thread-"; 46 | isDaemon = daemon; 47 | } 48 | 49 | @Override 50 | public Thread newThread(Runnable r) { 51 | Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(), 0); 52 | t.setDaemon(isDaemon); 53 | if (t.getPriority() != Thread.NORM_PRIORITY) { 54 | t.setPriority(Thread.NORM_PRIORITY); 55 | } 56 | return t; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/sofa/common/log/principle/Log4j2Test.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.log.principle; 18 | 19 | import org.apache.logging.log4j.core.LoggerContext; 20 | import org.junit.Assert; 21 | import org.junit.Test; 22 | 23 | import java.net.URISyntaxException; 24 | import java.net.URL; 25 | 26 | /** 27 | * Created by kevin.luy@alipay.com on 16/9/13. 28 | */ 29 | public class Log4j2Test { 30 | 31 | @Test 32 | public void testIndependentSpaceLog4j2() throws URISyntaxException { 33 | // LoggerFactory.getLogger(LogbackTest.class).info("xxx"); 34 | URL url1 = LogbackTest.class.getResource("/com/alipay/sofa/rpc/log/log4j2/log-conf.xml"); 35 | LoggerContext lc1 = new LoggerContext("rest", null, url1.toURI()); 36 | lc1.start(); 37 | org.apache.logging.log4j.core.Logger logger1 = lc1.getLogger("com.foo.Bar"); 38 | logger1.info("log4j2 - 1"); 39 | Assert.assertNotNull(logger1); 40 | 41 | //log4j2 logger2 42 | URL url2 = LogbackTest.class.getResource("/com/alipay/sofa/rpc/log/log4j2/log4j2_b.xml"); 43 | LoggerContext lc2 = new LoggerContext("rpc", null, url2.toURI()); 44 | lc2.start(); 45 | org.apache.logging.log4j.core.Logger logger2 = lc2.getLogger("com.foo.Bar2"); 46 | logger2.info("log4j2 - 2"); 47 | Assert.assertNotNull(logger2); 48 | Assert.assertNotSame(logger1, logger2); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/main/resources/sofa-common-tools/log/logback/log-conf.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | true 5 | 6 | WARN 7 | ACCEPT 8 | DENY 9 | 10 | ${logging.path}/sofa-config/common-default.log 11 | 12 | ${logging.path}/sofa-config/common-default.log.%d{yyyy-MM-dd} 13 | 30 14 | 15 | 16 | %d %-5p %-5t - %m%n 17 | ${file.encoding} 18 | 19 | 20 | 21 | 22 | true 23 | 24 | ${logging.level.sofa-common-tools} 25 | ACCEPT 26 | DENY 27 | 28 | ${logging.path}/sofa-config/config-digest.log 29 | 30 | ${logging.path}/sofa-config/config-digest.log.%d{yyyy-MM-dd} 31 | 30 32 | 33 | 34 | %d %-5p %-5t - %m%n 35 | ${file.encoding} 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /src/main/resources/sofa-thread/log/log4j2/log-conf.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ${sys:logging.level.sofa-thread} 5 | ${sys:logging.path} 6 | ${sys:file.encoding} 7 | 8 | 9 | 10 | 12 | 13 | 14 | %d %-5p %-5t - %m%n 15 | 16 | 17 | 18 | 19 | 20 | 21 | 23 | 24 | 25 | %d %-5p %-5t - %m%n 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/log/factory/AbstractLoggerSpaceFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.log.factory; 18 | 19 | import com.alipay.sofa.common.utils.ReportUtil; 20 | import com.alipay.sofa.common.log.adapter.level.AdapterLevel; 21 | import org.slf4j.ILoggerFactory; 22 | import org.slf4j.Logger; 23 | 24 | import java.util.Map; 25 | 26 | /** 27 | * updated by yangguanchao on 17/04/28. 28 | */ 29 | public abstract class AbstractLoggerSpaceFactory implements ILoggerFactory { 30 | 31 | protected static final String CONSOLE = "CONSOLE"; 32 | 33 | private final String source; 34 | 35 | /*** 36 | * 改变指定日志的级别,子类需要复写此方法来实现动态改变日志级别 37 | * @param loggerName 指定的日志对象 38 | * @param adapterLevel 要修改为的日志级别 39 | */ 40 | public Logger setLevel(String loggerName, AdapterLevel adapterLevel) throws Exception { 41 | ReportUtil.reportWarn("Unsupported change logger level in " + this.getClass() 42 | + ", loggerName[" + loggerName + "]"); 43 | return this.getLogger(loggerName); 44 | } 45 | 46 | public String getSource() { 47 | return source; 48 | } 49 | 50 | /** 51 | * @param source logback,log4j2,log4j,temp,nop 52 | */ 53 | public AbstractLoggerSpaceFactory(String source) { 54 | this.source = source; 55 | } 56 | 57 | @Override 58 | public String toString() { 59 | return "LoggerSpaceFactory{" + "source='" + source + '\'' + '}'; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/sofa/common/log/LoggerSpaceManagerUsage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.log; 18 | 19 | import com.alipay.sofa.common.log.LoggerSpaceManagerUsage.RpcLoggerFactory.RpcLogger; 20 | import org.slf4j.Logger; 21 | 22 | /** 23 | * 演示中间件自己的logger,LoggerFactory api如何使用 LoggerSpaceManager 进行独立日志打印; 24 | *

25 | * Created by kevin.luy@alipay.com on 16/9/13. 26 | */ 27 | public class LoggerSpaceManagerUsage { 28 | public static final String RPC_LOG_SPACE = "com.alipay.sofa.rpc"; 29 | 30 | public static void main(String[] args) { 31 | // MultiAppLoggerSpaceManager.init(RPC_LOG_SPACE, null); 32 | RpcLogger rpcLogger = RpcLoggerFactory.getLogger("com.alipay.foo"); 33 | rpcLogger.debug("hello world"); 34 | 35 | } 36 | 37 | /** 38 | * rpcLoggerFactory 39 | */ 40 | public static class RpcLoggerFactory { 41 | 42 | public static RpcLogger getLogger(String name) { 43 | //从"com/alipay/sofa/rpc/log"中获取rpc的日志配置并寻找对应logger对象 44 | final Logger logger = LoggerSpaceManager.getLoggerBySpace(name, RPC_LOG_SPACE); 45 | return new RpcLogger() { 46 | @Override 47 | public void debug(String msg) { 48 | logger.debug(msg); 49 | } 50 | }; 51 | } 52 | 53 | /** 54 | * rpcLogger 55 | */ 56 | public interface RpcLogger { 57 | void debug(String msg); 58 | 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/thread/log/ThreadLogger.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.thread.log; 18 | 19 | import com.alipay.sofa.common.utils.ThreadLoggerFactory; 20 | import org.slf4j.Logger; 21 | 22 | /** 23 | * @author Alaneuler 24 | * Created on 2020/3/19 25 | */ 26 | public class ThreadLogger { 27 | public static final Logger INFO_THREAD_LOGGER = ThreadLoggerFactory 28 | .getLogger("com.alipay.sofa.thread"); 29 | public static final Logger WARN_THREAD_LOGGER = ThreadLoggerFactory 30 | .getLogger("com.alipay.sofa.thread.warn"); 31 | 32 | public static void debug(String format, Object... arguments) { 33 | if (INFO_THREAD_LOGGER.isDebugEnabled()) { 34 | INFO_THREAD_LOGGER.debug(format, arguments); 35 | } 36 | } 37 | 38 | public static void info(String format, Object... arguments) { 39 | if (INFO_THREAD_LOGGER.isInfoEnabled()) { 40 | INFO_THREAD_LOGGER.info(format, arguments); 41 | } 42 | } 43 | 44 | public static void warn(String format, Object... arguments) { 45 | if (WARN_THREAD_LOGGER.isWarnEnabled()) { 46 | WARN_THREAD_LOGGER.warn(format, arguments); 47 | } 48 | } 49 | 50 | public static void error(String format, Object... arguments) { 51 | if (WARN_THREAD_LOGGER.isErrorEnabled()) { 52 | WARN_THREAD_LOGGER.error(format, arguments); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/insight/RecorderManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.insight; 18 | 19 | import com.alipay.sofa.common.config.log.ConfigLoggerFactory; 20 | import org.slf4j.Logger; 21 | 22 | import java.util.Iterator; 23 | import java.util.ServiceLoader; 24 | import java.util.concurrent.atomic.AtomicReference; 25 | 26 | /** 27 | * @author muqingcai 28 | * @version 2024年4月11日 下午2:32:31 29 | */ 30 | public class RecorderManager { 31 | private static final Logger LOGGER = ConfigLoggerFactory.getLogger(RecorderManager.class); 32 | private static final AtomicReference RECORDER = new AtomicReference<>(NoopRecorder.INSTANCE); 33 | 34 | public static Recorder getRecorder() { 35 | return RECORDER.get(); 36 | } 37 | 38 | public static void init() { 39 | Recorder targetRecorder = null; 40 | ServiceLoader loader = ServiceLoader.load(Recorder.class); 41 | Iterator iterator = loader.iterator(); 42 | if (iterator.hasNext()) { 43 | targetRecorder = iterator.next(); 44 | } 45 | if (targetRecorder != null) { 46 | boolean state = RECORDER.compareAndSet(NoopRecorder.INSTANCE, targetRecorder); 47 | if (state) { 48 | if (LOGGER.isInfoEnabled()) { 49 | LOGGER.info("Init recorder successfully, class: {}", targetRecorder.getClass().getName()); 50 | } 51 | } else { 52 | LOGGER.warn("Cannot init recorder repeatedly "); 53 | } 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/thread/space/SpaceNamedThreadFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.thread.space; 18 | 19 | import com.alipay.sofa.common.thread.ThreadPoolGovernor; 20 | 21 | import java.util.concurrent.ThreadFactory; 22 | import java.util.concurrent.atomic.AtomicInteger; 23 | 24 | /** 25 | * @author huzijie 26 | * @version spaceNameNamedThreadFactory.java, v 0.1 2020年11月10日 8:24 下午 huzijie Exp $ 27 | */ 28 | public class SpaceNamedThreadFactory implements ThreadFactory { 29 | 30 | private final AtomicInteger threadNumber = new AtomicInteger(1); 31 | 32 | private final ThreadGroup group; 33 | 34 | private final String namePrefix; 35 | 36 | private final boolean isDaemon; 37 | 38 | public SpaceNamedThreadFactory(String threadPoolName, String spaceName) { 39 | this(threadPoolName, spaceName, false); 40 | } 41 | 42 | public SpaceNamedThreadFactory(String threadPoolName, String spaceName, boolean daemon) { 43 | group = Thread.currentThread().getThreadGroup(); 44 | namePrefix = spaceName + "-" + threadPoolName + "-" 45 | + ThreadPoolGovernor.getInstance().getSpaceNameThreadPoolNumber(spaceName) 46 | + "-thread-"; 47 | isDaemon = daemon; 48 | } 49 | 50 | @Override 51 | public Thread newThread(Runnable r) { 52 | Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(), 0); 53 | t.setDaemon(isDaemon); 54 | if (t.getPriority() != Thread.NORM_PRIORITY) { 55 | t.setPriority(Thread.NORM_PRIORITY); 56 | } 57 | return t; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/resources/sofa-thread/log/logback/log-conf.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | true 5 | 6 | WARN 7 | ACCEPT 8 | DENY 9 | 10 | ${logging.path}/sofa-thread/aberrant-digest.log 11 | 12 | ${logging.path}/sofa-thread/aberrant-digest.log.%d{yyyy-MM-dd} 13 | 30 14 | 15 | 16 | %d %-5p %-5t - %m%n 17 | ${file.encoding} 18 | 19 | 20 | 21 | 22 | true 23 | 24 | ${logging.level.sofa-thread} 25 | ACCEPT 26 | DENY 27 | 28 | ${logging.path}/sofa-thread/thread-digest.log 29 | 30 | ${logging.path}/sofa-thread/thread-digest.log.%d{yyyy-MM-dd} 31 | 30 32 | 33 | 34 | %d %-5p %-5t - %m%n 35 | ${file.encoding} 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/sofa/common/log/factory/UpdateConsoleLoggerWhiteSetTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.log.factory; 18 | 19 | import com.alipay.sofa.common.log.CommonLoggingConfigurations; 20 | import com.alipay.sofa.common.log.LogSpace; 21 | import com.alipay.sofa.common.space.SpaceId; 22 | import org.junit.Assert; 23 | import org.junit.Test; 24 | 25 | /** 26 | * @author Alaneuler 27 | * Created on 2020/11/30 28 | */ 29 | public class UpdateConsoleLoggerWhiteSetTest { 30 | private static final String spaceName = "com.alipay.sofa"; 31 | 32 | @Test 33 | public void test() { 34 | SpaceId spaceId = new SpaceId(spaceName); 35 | LogSpace spaceInfo = new LogSpace(); 36 | new LoggerSpaceFactory4LogbackBuilder(spaceId, spaceInfo).getSpaceLogConfigFileURL(this 37 | .getClass().getClassLoader(), "com.alipay.sofa"); 38 | //name config 39 | Assert.assertEquals(2, CommonLoggingConfigurations.getLoggerConsoleWhiteSet().size()); 40 | Assert.assertTrue(CommonLoggingConfigurations.getLoggerConsoleWhiteSet().contains( 41 | "testLoggerName1")); 42 | Assert.assertTrue(CommonLoggingConfigurations.getLoggerConsoleWhiteSet().contains( 43 | "testLoggerName2")); 44 | //prefix config 45 | Assert.assertEquals(2, CommonLoggingConfigurations.getLoggerConsolePrefixWhiteSet().size()); 46 | Assert.assertTrue(CommonLoggingConfigurations.getLoggerConsolePrefixWhiteSet().contains( 47 | "com.test1")); 48 | Assert.assertTrue(CommonLoggingConfigurations.getLoggerConsolePrefixWhiteSet().contains( 49 | "com.test2")); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/sofa/common/log/principle/LogbackTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.log.principle; 18 | 19 | import ch.qos.logback.classic.Logger; 20 | import ch.qos.logback.classic.LoggerContext; 21 | import ch.qos.logback.classic.util.ContextInitializer; 22 | import ch.qos.logback.core.joran.spi.JoranException; 23 | import com.alipay.sofa.common.log.factory.LogbackLoggerSpaceFactory; 24 | import org.junit.Assert; 25 | import org.junit.Test; 26 | 27 | import java.net.URL; 28 | 29 | /** 30 | * Created by kevin.luy@alipay.com on 16/9/12. 31 | */ 32 | public class LogbackTest { 33 | 34 | @Test 35 | public void testIndependentSpaceLogback() throws JoranException { 36 | 37 | URL url1 = LogbackTest.class.getResource("/com/alipay/sofa/rpc/log/logback/log-conf.xml"); 38 | LoggerContext loggerContext1 = new LoggerContext(); 39 | new ContextInitializer(loggerContext1); 40 | LogbackLoggerSpaceFactory.configureByResource(url1, loggerContext1); 41 | ch.qos.logback.classic.Logger logger1 = loggerContext1.getLogger("com.foo.Bar"); 42 | logger1.info("log4j2 - 1"); 43 | Assert.assertNotNull(logger1); 44 | 45 | //logback logger 2 46 | 47 | URL url2 = LogbackTest.class.getResource("/com/alipay/sofa/rpc/log/logback/logback_b.xml"); 48 | LoggerContext loggerContext2 = new LoggerContext(); 49 | new ContextInitializer(loggerContext2); 50 | LogbackLoggerSpaceFactory.configureByResource(url2, loggerContext2); 51 | Logger logger2 = loggerContext2.getLogger("com.foo.Bar2"); 52 | logger2.info("log4j2 - 222"); 53 | Assert.assertNotNull(logger2); 54 | 55 | Assert.assertNotSame(logger1, logger2); 56 | 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/sofa/common/log/base/AbstraceLogTestBase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.log.base; 18 | 19 | import com.alipay.sofa.common.log.Constants; 20 | import com.alipay.sofa.common.log.LoggerSpaceManager; 21 | 22 | /** 23 | * AbstraceLogTestBase 24 | *

25 | * Created by yangguanchao on 17/4/28. 26 | */ 27 | public abstract class AbstraceLogTestBase { 28 | 29 | /*** 30 | * 这个和配置文件配置关联 31 | */ 32 | public static final String LOG_SPACE_TEST = "com.alipay.sofa.rpc"; 33 | 34 | public static String logLevel = Constants.LOG_LEVEL_PREFIX + LOG_SPACE_TEST; 35 | 36 | public void before() throws Exception { 37 | System.getProperties().put("logging.path", "./logs"); 38 | System.setProperty(Constants.LOG_ENV_SUFFIX, ""); 39 | } 40 | 41 | public void after() throws Exception { 42 | 43 | System.err.println("\n " + Constants.LOG_ENCODING_PROP_KEY + " : " 44 | + System.getProperty("file.encoding")); 45 | System.err.println("\n " + Constants.LOG_PATH + " : " + System.getProperty("logging.path")); 46 | System.err.println("\n " + logLevel + " : " + System.getProperty(logLevel)); 47 | 48 | System.clearProperty(Constants.LOG_PATH); 49 | System.clearProperty(Constants.OLD_LOG_PATH); 50 | System.clearProperty(Constants.LOG_ENV_SUFFIX); 51 | System.clearProperty(logLevel); 52 | //关闭禁用开关 53 | System.clearProperty(Constants.LOGBACK_MIDDLEWARE_LOG_DISABLE_PROP_KEY); 54 | System.clearProperty(Constants.LOG4J2_MIDDLEWARE_LOG_DISABLE_PROP_KEY); 55 | System.clearProperty(Constants.LOG4J_MIDDLEWARE_LOG_DISABLE_PROP_KEY); 56 | LoggerSpaceManager.removeILoggerFactoryBySpaceName(LOG_SPACE_TEST); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/tracer/TracerIdAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.tracer; 18 | 19 | import org.slf4j.Logger; 20 | import org.slf4j.LoggerFactory; 21 | 22 | import java.util.Iterator; 23 | import java.util.ServiceLoader; 24 | 25 | /** 26 | * use custom {@link TracerIdRetriever} to find trace id 27 | * @author huzijie 28 | * @version TracerAdapter.java, v 0.1 2020年11月11日 5:29 下午 huzijie Exp $ 29 | */ 30 | public class TracerIdAdapter { 31 | 32 | private static final Logger logger = LoggerFactory.getLogger(TracerIdAdapter.class); 33 | 34 | private static final TracerIdAdapter INSTANCE = new TracerIdAdapter(); 35 | 36 | private TracerIdRetriever tracerIdRetriever; 37 | 38 | private TracerIdAdapter() { 39 | ServiceLoader serviceLoader = ServiceLoader 40 | .load(TracerIdRetriever.class); 41 | Iterator tracerIdAdapterIterator = serviceLoader.iterator(); 42 | if (tracerIdAdapterIterator.hasNext()) { 43 | this.tracerIdRetriever = tracerIdAdapterIterator.next(); 44 | logger.info("TracerIdConverter use tracerIdAdapter '{}'", tracerIdRetriever.getClass() 45 | .getName()); 46 | } else { 47 | logger.info("TracerIdConverter can not find any tracerIdAdapter"); 48 | } 49 | } 50 | 51 | public static TracerIdAdapter getInstance() { 52 | return INSTANCE; 53 | } 54 | 55 | public String traceIdSafari(Thread t) { 56 | if (tracerIdRetriever == null) { 57 | return null; 58 | } 59 | try { 60 | return tracerIdRetriever.getTracerId(t); 61 | } catch (Exception e) { 62 | //ignore 63 | return null; 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/sofa/common/thread/TimeWaitRejectedExecutionHandlerTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.thread; 18 | 19 | import org.junit.Assert; 20 | import org.junit.Test; 21 | 22 | import java.util.concurrent.LinkedBlockingDeque; 23 | import java.util.concurrent.RejectedExecutionException; 24 | import java.util.concurrent.TimeUnit; 25 | 26 | /** 27 | * @author zhangchengxi 28 | */ 29 | public class TimeWaitRejectedExecutionHandlerTest extends ThreadPoolTestBase { 30 | 31 | @Test 32 | public void test() throws InterruptedException { 33 | 34 | SofaThreadPoolExecutor executor = new SofaThreadPoolExecutor(1,1,1, TimeUnit.HOURS,new LinkedBlockingDeque<>(1)); 35 | TimeWaitRejectedExecutionHandler handler = new TimeWaitRejectedExecutionHandler(executor, 2, TimeUnit.SECONDS); 36 | executor.setRejectedExecutionHandler(handler); 37 | 38 | Runnable run10s = () -> { 39 | try { 40 | Thread.sleep(10000); 41 | } catch (InterruptedException e) { 42 | e.printStackTrace(); 43 | } 44 | }; 45 | boolean exceptionOccur = false; 46 | try{ 47 | executor.execute(run10s); 48 | executor.execute(run10s); 49 | try{ 50 | executor.execute(run10s); 51 | Assert.fail(); 52 | }catch (RejectedExecutionException e){ 53 | // ignore 54 | exceptionOccur = true; 55 | } 56 | Assert.assertTrue(exceptionOccur); 57 | Thread.sleep(3000); 58 | } catch (InterruptedException e) { 59 | e.printStackTrace(); 60 | } 61 | executor.shutdownNow(); 62 | executor.awaitTermination(100, TimeUnit.SECONDS); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/sofa/common/utils/CharsetUtilTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.utils; 18 | 19 | import org.junit.Assert; 20 | import org.junit.Test; 21 | 22 | import java.io.UnsupportedEncodingException; 23 | import java.nio.charset.StandardCharsets; 24 | 25 | /** 26 | * @author huzijie 27 | * @version CharsetUtilTest.java, v 0.1 2023年05月26日 10:29 AM huzijie Exp $ 28 | */ 29 | public class CharsetUtilTest { 30 | 31 | private final String input = "一个中文字符串"; 32 | private final byte[] utf8 = input.getBytes(StandardCharsets.UTF_8); 33 | private final byte[] gbk = input.getBytes("GBK"); 34 | 35 | public CharsetUtilTest() throws UnsupportedEncodingException { 36 | } 37 | 38 | @Test 39 | public void testAssertUtf8WellFormed() { 40 | CharsetUtil.assertUTF8WellFormed(utf8); 41 | Assert.assertThrows(IllegalArgumentException.class, () -> CharsetUtil.assertUTF8WellFormed(gbk)); 42 | } 43 | 44 | @Test 45 | public void testMonitorUtf8WellFormed() { 46 | CharsetUtil.monitorUTF8WellFormed(utf8); 47 | CharsetUtil.monitorUTF8WellFormed(gbk); 48 | } 49 | 50 | @Test 51 | public void testCheckUtf8WellFormed() { 52 | CharsetUtil.checkUTF8WellFormed(utf8, 0); 53 | Assert.assertThrows(IllegalArgumentException.class, () -> CharsetUtil.checkUTF8WellFormed(gbk, 0)); 54 | 55 | CharsetUtil.checkUTF8WellFormed(utf8, 1); 56 | CharsetUtil.checkUTF8WellFormed(gbk, 1); 57 | } 58 | 59 | @Test 60 | public void testIsUtf8WellFormed() { 61 | Assert.assertTrue(CharsetUtil.isUTF8WellFormed(utf8)); 62 | Assert.assertFalse(CharsetUtil.isUTF8WellFormed(gbk)); 63 | 64 | Assert.assertTrue(CharsetUtil.isUTF8WellFormed(utf8, 0, 3)); 65 | Assert.assertFalse(CharsetUtil.isUTF8WellFormed(gbk, 0, 3)); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/config/listener/LogConfigListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.config.listener; 18 | 19 | import com.alipay.sofa.common.config.ConfigKey; 20 | import com.alipay.sofa.common.config.ConfigSource; 21 | import com.alipay.sofa.common.utils.Ordered; 22 | import org.slf4j.Logger; 23 | 24 | import java.util.List; 25 | 26 | import static com.alipay.sofa.common.config.log.ConfigLoggerFactory.CONFIG_COMMON_DIGEST_LOGGER; 27 | 28 | /** 29 | * @author zhaowang 30 | * @version : ConfigLogListener.java, v 0.1 2020年12月01日 2:06 下午 zhaowang Exp $ 31 | */ 32 | public class LogConfigListener extends AbstractConfigListener { 33 | 34 | private static final Logger LOGGER = CONFIG_COMMON_DIGEST_LOGGER; 35 | 36 | @Override 37 | public void afterConfigLoaded(ConfigKey configKey, ConfigSource configSource, 38 | List configSourceList) { 39 | String keyStr = configKey.getKey(); 40 | String configName = configSource.getName(); 41 | String value = configSource.getStringConfig(configKey); 42 | String effectKey = configSource.getEffectiveKey(configKey); 43 | LOGGER.info("Load {} from {} ,effect key is {}, value is \"{}\"", keyStr, configName, 44 | effectKey, value); 45 | } 46 | 47 | @Override 48 | public void onLoadDefaultValue(ConfigKey key, Object defaultValue) { 49 | if (key.getDefaultValue().equals(defaultValue)) { 50 | LOGGER.info("Load {} according defaultValue ,default value is \"{}\"", key.getKey(), 51 | defaultValue); 52 | } else { 53 | LOGGER.warn("Config {}'s defaultValue {} does not equals to actually defaultValue {}", 54 | key, key.getDefaultValue(), defaultValue); 55 | 56 | } 57 | } 58 | 59 | @Override 60 | public int getOrder() { 61 | return Ordered.LOWEST_PRECEDENCE; 62 | } 63 | } -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/utils/ProcessIdUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.utils; 18 | 19 | import java.io.File; 20 | import java.io.IOException; 21 | import java.lang.reflect.Method; 22 | 23 | /** 24 | * {@literal org.apache.logging.log4j.util.ProcessIdUtil} 25 | * 26 | * @author qilong.zql 27 | * @since 1.0.15 28 | */ 29 | public class ProcessIdUtil { 30 | public static final String DEFAULT_PROCESSID = "-"; 31 | private static volatile String processId; 32 | 33 | public static String getProcessId() { 34 | try { 35 | if (!StringUtil.isBlank(processId)) { 36 | return processId; 37 | } 38 | // LOG4J2-2126 use reflection to improve compatibility with Android Platform which does not support JMX extensions 39 | Class managementFactoryClass = Class 40 | .forName("java.lang.management.ManagementFactory"); 41 | Method getRuntimeMXBean = managementFactoryClass.getDeclaredMethod("getRuntimeMXBean"); 42 | Class runtimeMXBeanClass = Class.forName("java.lang.management.RuntimeMXBean"); 43 | Method getName = runtimeMXBeanClass.getDeclaredMethod("getName"); 44 | 45 | Object runtimeMXBean = getRuntimeMXBean.invoke(null); 46 | String name = (String) getName.invoke(runtimeMXBean); 47 | //String name = ManagementFactory.getRuntimeMXBean().getName(); //JMX not allowed on Android 48 | processId = name.split("@")[0]; // likely works on most platforms 49 | return processId; 50 | } catch (final Exception ex) { 51 | try { 52 | processId = new File("/proc/self").getCanonicalFile().getName(); // try a Linux-specific way 53 | return processId; 54 | } catch (final IOException ignoredUseDefault) { 55 | // Ignore exception. 56 | } 57 | } 58 | return DEFAULT_PROCESSID; 59 | } 60 | } -------------------------------------------------------------------------------- /src/test/java/com/alipay/sofa/common/thread/SofaTracerCommandFactoryTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.thread; 18 | 19 | import com.alipay.common.tracer.core.async.SofaTracerCallable; 20 | import com.alipay.common.tracer.core.async.SofaTracerRunnable; 21 | import org.junit.Assert; 22 | import org.junit.Test; 23 | 24 | import java.util.concurrent.Callable; 25 | 26 | /** 27 | * @author huzijie 28 | * @version SofaTracerCommandFactoryTest.java, v 0.1 2023年09月26日 3:13 PM huzijie Exp $ 29 | */ 30 | public class SofaTracerCommandFactoryTest { 31 | 32 | @Test 33 | public void ofExecutingRunnable() { 34 | Runnable runnable = () -> {}; 35 | Assert.assertTrue(SofaTracerCommandFactory.ofExecutingRunnable(runnable) instanceof SofaTracerCommandFactory.SofaTracerExecutingRunnable); 36 | } 37 | 38 | @Test 39 | public void ofRunnable() { 40 | Runnable runnable = () -> {}; 41 | Runnable newRunnable = SofaTracerCommandFactory.ofRunnable(runnable); 42 | Assert.assertTrue(newRunnable instanceof SofaTracerRunnable); 43 | Assert.assertNotEquals(runnable, newRunnable); 44 | 45 | Runnable duplicateWrapRunnable = SofaTracerCommandFactory.ofRunnable(newRunnable); 46 | Assert.assertTrue(duplicateWrapRunnable instanceof SofaTracerRunnable); 47 | Assert.assertEquals(duplicateWrapRunnable, newRunnable); 48 | } 49 | 50 | @Test 51 | public void ofCallable() { 52 | Callable callable = () -> null; 53 | Callable newCallable = SofaTracerCommandFactory.ofCallable(callable); 54 | Assert.assertTrue(newCallable instanceof SofaTracerCallable); 55 | Assert.assertNotEquals(callable, newCallable); 56 | 57 | Callable duplicateWrapCallable = SofaTracerCommandFactory.ofCallable(newCallable); 58 | Assert.assertTrue(duplicateWrapCallable instanceof SofaTracerCallable); 59 | Assert.assertEquals(duplicateWrapCallable, duplicateWrapCallable); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/log/LogSpace.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.log; 18 | 19 | import com.alipay.sofa.common.log.factory.AbstractLoggerSpaceFactory; 20 | 21 | import java.util.Map; 22 | import java.util.Properties; 23 | 24 | /** 25 | * @author Alaneuler 26 | * Created on 2020/12/4 27 | */ 28 | public class LogSpace { 29 | private final Properties properties; 30 | private AbstractLoggerSpaceFactory abstractLoggerSpaceFactory; 31 | private ClassLoader spaceClassloader; 32 | 33 | public LogSpace() { 34 | properties = new Properties(); 35 | } 36 | 37 | public LogSpace(Map map, ClassLoader spaceClassloader) { 38 | this(); 39 | this.spaceClassloader = spaceClassloader; 40 | putAll(map); 41 | } 42 | 43 | public ClassLoader getSpaceClassloader() { 44 | return spaceClassloader; 45 | } 46 | 47 | public void setSpaceClassloader(ClassLoader spaceClassloader) { 48 | this.spaceClassloader = spaceClassloader; 49 | } 50 | 51 | public AbstractLoggerSpaceFactory getAbstractLoggerSpaceFactory() { 52 | return abstractLoggerSpaceFactory; 53 | } 54 | 55 | public void setAbstractLoggerSpaceFactory(AbstractLoggerSpaceFactory abstractLoggerSpaceFactory) { 56 | this.abstractLoggerSpaceFactory = abstractLoggerSpaceFactory; 57 | } 58 | 59 | public Properties properties() { 60 | return properties; 61 | } 62 | 63 | public LogSpace putAll(Map properties) { 64 | if (properties != null) { 65 | this.properties.putAll(properties); 66 | } 67 | return this; 68 | } 69 | 70 | public LogSpace setProperty(String key, String value) { 71 | properties.setProperty(key, value); 72 | return this; 73 | } 74 | 75 | public String getProperty(String key) { 76 | return properties.getProperty(key); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/log/adapter/level/AdapterLevel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.log.adapter.level; 18 | 19 | import com.alipay.sofa.common.utils.AssertUtil; 20 | 21 | /** 22 | * AdapterLevel 23 | *

24 | * 适配日志级别,通过这个日志级别来适配映射 log4j、log4j2 和 logback 的日志级别,通过统一接口来屏蔽差异 25 | *

26 | *

27 | * Created by yangguanchao on 17/4/28. 28 | */ 29 | public enum AdapterLevel { 30 | 31 | /** 32 | * An error in the application, possibly recoverable. 33 | */ 34 | ERROR("error"), 35 | 36 | /** 37 | * An event that might possible lead to an error. 38 | */ 39 | WARN("warn"), 40 | 41 | /** 42 | * An event for informational purposes. 43 | */ 44 | INFO("info"), 45 | 46 | /** 47 | * A general debugging event. 48 | */ 49 | DEBUG("debug"), 50 | 51 | /** 52 | * A fine-grained debug message, typically capturing the flow through the application. 53 | */ 54 | TRACE("trace"); 55 | 56 | private String logLevelMsg; 57 | 58 | private AdapterLevel(String logLevelMsg) { 59 | this.logLevelMsg = logLevelMsg; 60 | } 61 | 62 | public static AdapterLevel getAdapterLevel(String adapterLogLevelMsg) { 63 | AssertUtil.hasText(adapterLogLevelMsg, "Input Param of AdapterLevel Type can't be blank!"); 64 | for (AdapterLevel adapterLevel : AdapterLevel.values()) { 65 | if (adapterLevel.getLogLevelMsg().equals(adapterLogLevelMsg)) { 66 | return adapterLevel; 67 | } 68 | } 69 | throw new IllegalArgumentException( 70 | "Input Level Message[" + adapterLogLevelMsg 71 | + "] can't match any [com.alipay.sofa.common.log.adapter.level.AdapterLevel]"); 72 | } 73 | 74 | public String getLogLevelMsg() { 75 | return logLevelMsg; 76 | } 77 | 78 | @Override 79 | public String toString() { 80 | return "AdapterLevel{" + "logLevelMsg='" + logLevelMsg + '\'' + '}'; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/sofa/common/log/LoggerSpaceConcurrencyTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.log; 18 | 19 | import org.junit.Assert; 20 | import org.junit.Test; 21 | import org.slf4j.Logger; 22 | 23 | import java.util.ArrayList; 24 | import java.util.List; 25 | import java.util.concurrent.CopyOnWriteArrayList; 26 | import java.util.concurrent.CountDownLatch; 27 | import java.util.concurrent.LinkedBlockingQueue; 28 | import java.util.concurrent.ThreadPoolExecutor; 29 | import java.util.concurrent.TimeUnit; 30 | 31 | /** 32 | * @author huzijie 33 | * @version LoggerSpaceConcurrencyTest.java, v 0.1 2023年11月07日 3:57 PM huzijie Exp $ 34 | */ 35 | public class LoggerSpaceConcurrencyTest { 36 | private static final ThreadPoolExecutor EXECUTOR = new ThreadPoolExecutor( 37 | 5, 50, 60, TimeUnit.SECONDS, 38 | new LinkedBlockingQueue<>(1000)); 39 | 40 | 41 | @Test 42 | public void testLogUtilGetLoggerConcurrently() throws InterruptedException { 43 | List loggers = new CopyOnWriteArrayList<>(); 44 | 45 | CountDownLatch countDownLatch = new CountDownLatch(10); 46 | for (int i = 0; i < 10; i++) { 47 | EXECUTOR.submit(() -> { 48 | try { 49 | Logger logger = getLogger(); 50 | loggers.add(logger); 51 | } catch (Throwable t) { 52 | t.printStackTrace(); 53 | } finally { 54 | countDownLatch.countDown(); 55 | } 56 | }); 57 | } 58 | 59 | Assert.assertTrue(countDownLatch.await(10, TimeUnit.SECONDS)); 60 | Assert.assertEquals(loggers.size(), 10); 61 | 62 | for (Logger logger : loggers) { 63 | Assert.assertNotEquals("Constants.DEFAULT_LOG is not expected", logger, Constants.DEFAULT_LOG); 64 | } 65 | 66 | } 67 | 68 | private Logger getLogger() { 69 | return LoggerSpaceManager.getLoggerBySpace("TestLogger", "com.alipay.sofa.concurrency"); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/sofa/common/log/factory/LoggerSpaceOverrideUsageTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.log.factory; 18 | 19 | import com.alipay.sofa.common.log.LogSpace; 20 | import com.alipay.sofa.common.space.SpaceId; 21 | import org.junit.Assert; 22 | import org.junit.Test; 23 | 24 | import java.io.IOException; 25 | import java.net.URL; 26 | import java.util.ArrayList; 27 | import java.util.List; 28 | 29 | /** 30 | * @author GengZhang 31 | */ 32 | public class LoggerSpaceOverrideUsageTest { 33 | 34 | @Test 35 | public void testGetResource() throws IOException { 36 | LoggerSpaceFactory4LogbackBuilder builder = new LoggerSpaceFactory4LogbackBuilder( 37 | new SpaceId("test"), new LogSpace()); 38 | ClassLoader classLoader = LoggerSpaceOverrideUsageTest.class.getClassLoader(); 39 | 40 | Assert.assertNull(builder.getResourceByPriority(null, null)); 41 | 42 | List configFileUrls = new ArrayList(); 43 | 44 | List propertyFileUrls = new ArrayList(); 45 | Assert.assertNull(builder.getResourceByPriority(configFileUrls, null)); 46 | 47 | URL url1 = classLoader.getResource("com/alipay/sofa/testover1/log/log4j/log-conf.xml"); 48 | configFileUrls.add(url1); 49 | Assert.assertEquals(url1, builder.getResourceByPriority(configFileUrls, null)); 50 | 51 | URL url2 = classLoader.getResource("com/alipay/sofa/testover2/log/log4j/log-conf.xml"); 52 | URL p2 = classLoader.getResource("com/alipay/sofa/testover2/log/log4j/config.properties"); 53 | configFileUrls.add(url2); 54 | propertyFileUrls.add(p2); 55 | Assert.assertEquals(url2, builder.getResourceByPriority(configFileUrls, propertyFileUrls)); 56 | 57 | URL url3 = classLoader.getResource("com/alipay/sofa/testover3/log/log4j/log-conf.xml"); 58 | URL p3 = classLoader.getResource("com/alipay/sofa/testover3/log/log4j/config.properties"); 59 | configFileUrls.add(url3); 60 | propertyFileUrls.add(p3); 61 | Assert.assertEquals(url2, builder.getResourceByPriority(configFileUrls, propertyFileUrls)); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/profile/enumeration/internal/EnumConstant.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.profile.enumeration.internal; 18 | 19 | /** 20 | * 21 | * @author luoguimu123 22 | * @version $Id: EnumConstant.java, v 0.1 2017年08月01日 上午11:57 luoguimu123 Exp $ 23 | */ 24 | public interface EnumConstant { 25 | String CREATE_ENUM_TYPE_METHOD_NAME = "createEnumType"; 26 | String FLAG_SET_INNER_CLASS_NAME = "FlagSet"; 27 | String ENUM_NAME_IS_EMPTY = "The Enum name must not be empty"; 28 | String ENUM_VALUE_IS_NULL = "The Enum value must not be null"; 29 | String DUPLICATED_ENUM_NAME = "Duplicated name \"{0}\" of Enum class \"{1}\""; 30 | String ENUM_CLASS_IS_NULL = "The Enum class must not be null"; 31 | String CLASS_IS_NOT_ENUM = "Class \"{0}\" is not a subclass of Enum"; 32 | String FAILED_CREATING_ENUM_TYPE = "Could not create EnumType for class \"{0}\""; 33 | String COMPARE_TYPE_MISMATCH = "Could not compare object of \"{0}\" with object of \"{1}\""; 34 | String VALUE_OUT_OF_RANGE = "The flag value is out of range"; 35 | String ENUM_IS_NOT_A_FLAG = "The Enum class \"{0}\" is not an implementation of Flags"; 36 | String CREATE_FLAG_SET_IS_UNSUPPORTED = "Creating FlagSet is not supported by Enum class \"{0}\". Make sure there is a static inner class \"{0}.FlagSet\" with a constructor without parameters"; 37 | String ILLEGAL_CLASS = "Class \"{0}\" is not a subclass of \"{1}\""; 38 | String ILLEGAL_INTERFACE = "Class \"{0}\" is not an implementation of \"{1}\""; 39 | String CLONE_NOT_SUPPORTED = "Clone not supported by class \"{0}\""; 40 | String COMPARE_UNDERLYING_CLASS_MISMATCH = "Could not compare FlagSet of underlying class \"{0}\" with FlagSet of underlying class \"{1}\""; 41 | String FLAGS_IS_NULL = "Flags must not be null"; 42 | String FLAT_SET_VALUE_IS_NULL = "FlagSet value must not be null"; 43 | String ILLEGAL_FLAGS_OBJECT = "Flags must be of \"{0}\" or \"{1}\""; 44 | String FLAG_SET_IS_IMMUTABLE = "FlagSet is immutable"; 45 | } -------------------------------------------------------------------------------- /src/test/java/com/alipay/sofa/common/log/LoggerSpaceManagerTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.log; 18 | 19 | import com.alipay.sofa.common.log.base.AbstraceLogTestBase; 20 | import org.junit.After; 21 | import org.junit.Assert; 22 | import org.junit.Before; 23 | import org.junit.BeforeClass; 24 | import org.junit.Test; 25 | import org.slf4j.Logger; 26 | 27 | import static com.alipay.sofa.common.log.Constants.SOFA_MIDDLEWARE_LOG_DISABLE_PROP_KEY; 28 | 29 | /** 30 | * 注释:随机选择 31 | *

32 | * SLF4J: Class path contains multiple SLF4J bindings. 33 | * SLF4J: Found binding in [jar:file:/Users/yangguanchao/.m2/org/slf4j/slf4j-log4j12/1.7.21/slf4j-log4j12-1.7.21.jar!/org/slf4j/impl/StaticLoggerBinder.class] 34 | * SLF4J: Found binding in [jar:file:/Users/yangguanchao/.m2/org/apache/logging/log4j/log4j-slf4j-impl/2.6.2/log4j-slf4j-impl-2.6.2.jar!/org/slf4j/impl/StaticLoggerBinder.class] 35 | * SLF4J: Found binding in [jar:file:/Users/yangguanchao/.m2/ch/qos/logback/logback-classic/1.1.7/logback-classic-1.1.7.jar!/org/slf4j/impl/StaticLoggerBinder.class] 36 | * SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. 37 | * SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory] 38 | *

39 | *

40 | * Created by kevin.luy@alipay.com on 16/9/19. 41 | */ 42 | public class LoggerSpaceManagerTest extends AbstraceLogTestBase { 43 | 44 | @BeforeClass 45 | public static void beforeClass() { 46 | System.setProperty(SOFA_MIDDLEWARE_LOG_DISABLE_PROP_KEY, "true"); 47 | } 48 | 49 | @Before 50 | @Override 51 | public void before() throws Exception { 52 | super.before(); 53 | } 54 | 55 | @After 56 | @Override 57 | public void after() throws Exception { 58 | super.after(); 59 | } 60 | 61 | @Test 62 | public void TestDisableLoggerSpaceFactory() { 63 | Logger logger = LoggerSpaceManager.getLoggerBySpace("com.foo.Bar", "com.alipay.sofa.rpc"); 64 | Logger logger2 = LoggerSpaceManager.getLoggerBySpace("com.foo.Bar", "com.alipay.sofa.rpc"); 65 | Assert.assertSame(logger, logger2); 66 | Assert.assertSame(logger, Constants.DEFAULT_LOG); 67 | System.setProperty(SOFA_MIDDLEWARE_LOG_DISABLE_PROP_KEY, "false"); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/config/source/AbstractConfigSource.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.config.source; 18 | 19 | import com.alipay.sofa.common.config.ConfigKey; 20 | import com.alipay.sofa.common.config.ConfigSource; 21 | import com.alipay.sofa.common.config.Converter; 22 | import com.alipay.sofa.common.config.converter.GlobalConverterHolder; 23 | import com.alipay.sofa.common.utils.StringUtil; 24 | 25 | /** 26 | * @author zhaowang 27 | * @version : AbstractConfigSource.java, v 0.1 2020年10月21日 2:38 下午 zhaowang Exp $ 28 | */ 29 | public abstract class AbstractConfigSource implements ConfigSource { 30 | 31 | @Override 32 | public T getConfig(ConfigKey key) { 33 | String value = getStringConfig(key); 34 | return changeValueType(value, key.getType()); 35 | 36 | } 37 | 38 | @Override 39 | public String getStringConfig(ConfigKey key) { 40 | String value = doGetConfig(key.getKey()); 41 | if (StringUtil.isNotBlank(value)) { 42 | return value; 43 | } 44 | 45 | for (String alias : key.getAlias()) { 46 | value = doGetConfig(alias); 47 | if (StringUtil.isNotBlank(value)) { 48 | return value; 49 | } 50 | } 51 | return value; 52 | } 53 | 54 | @Override 55 | public String getEffectiveKey(ConfigKey configKey) { 56 | String key = configKey.getKey(); 57 | if (hasKey(key)) { 58 | return key; 59 | } 60 | String[] alias = configKey.getAlias(); 61 | for (String alia : alias) { 62 | if (hasKey(alia)) { 63 | return alia; 64 | } 65 | } 66 | return ""; 67 | } 68 | 69 | @SuppressWarnings("unchecked") 70 | protected T changeValueType(String value, Class targetType) { 71 | if (value == null) { 72 | return null; 73 | } 74 | if (targetType == null) { 75 | return (T) value; 76 | } 77 | 78 | Converter converter = GlobalConverterHolder.getGlobalConverter(); 79 | if (converter == null) { 80 | converter = GlobalConverterHolder.DEFAULT_CONVERTER; 81 | } 82 | return converter.convert(value, targetType); 83 | } 84 | 85 | public abstract String doGetConfig(String key); 86 | 87 | public abstract boolean hasKey(String key); 88 | } -------------------------------------------------------------------------------- /src/test/java/com/alipay/sofa/common/log/principle/Log4jTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.log.principle; 18 | 19 | import org.apache.log4j.Hierarchy; 20 | import org.apache.log4j.Level; 21 | import org.apache.log4j.Logger; 22 | import org.apache.log4j.helpers.OptionConverter; 23 | import org.apache.log4j.spi.LoggerRepository; 24 | import org.apache.log4j.spi.RootLogger; 25 | import org.apache.log4j.xml.DOMConfigurator; 26 | import org.junit.Assert; 27 | import org.junit.Test; 28 | 29 | import java.lang.reflect.Field; 30 | import java.net.URL; 31 | import java.util.Properties; 32 | 33 | /** 34 | * Created by kevin.luy@alipay.com on 16/9/13. 35 | */ 36 | public class Log4jTest { 37 | 38 | @Test 39 | public void testIndependentSpaceLog4j() { 40 | LoggerRepository repo1 = new Hierarchy(new RootLogger((Level) Level.DEBUG)); 41 | URL url1 = LogbackTest.class.getResource("/com/alipay/sofa/rpc/log/log4j/log-conf.xml"); 42 | OptionConverter.selectAndConfigure(url1, null, repo1); 43 | Logger logger1 = repo1.getLogger("com.foo.Bar"); 44 | Assert.assertNotNull(logger1); 45 | 46 | //log4j logger 2 47 | 48 | LoggerRepository repo2 = new Hierarchy(new RootLogger((Level) Level.DEBUG)); 49 | URL url2 = LogbackTest.class.getResource("/com/alipay/sofa/rpc/log/log4j/log4j_b.xml"); 50 | OptionConverter.selectAndConfigure(url2, null, repo2); 51 | Logger logger2 = repo1.getLogger("com.foo.Bar2"); 52 | Assert.assertNotNull(logger2); 53 | 54 | Assert.assertNotSame(logger1, logger2); 55 | } 56 | 57 | @Test 58 | public void testLocalProperties() throws NoSuchFieldException, IllegalAccessException { 59 | 60 | LoggerRepository repo2 = new Hierarchy(new RootLogger((Level) Level.DEBUG)); 61 | URL url2 = LogbackTest.class.getResource("/com/alipay/sofa/rpc/log/log4j/log4j_b.xml"); 62 | DOMConfigurator domConfigurator = new DOMConfigurator(); 63 | 64 | Field field = DOMConfigurator.class.getDeclaredField("props"); 65 | field.setAccessible(true); 66 | Properties props = new Properties(); 67 | field.set(domConfigurator, props); 68 | props.put("hello", "defaultb"); 69 | 70 | domConfigurator.doConfigure(url2, repo2); 71 | 72 | Logger logger2 = repo2.getLogger("com.foo.Bar3"); 73 | Assert.assertTrue(logger2.getAllAppenders().hasMoreElements()); 74 | 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/CommonToolConfigKeys.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common; 18 | 19 | import com.alipay.sofa.common.config.ConfigKey; 20 | 21 | /** 22 | * @author zhaowang 23 | * @version : CommonToolsConfig.java, v 0.1 2020年10月20日 8:53 下午 zhaowang Exp $ 24 | */ 25 | public class CommonToolConfigKeys { 26 | 27 | public static final String LOG_PREFIX = "sofa.common.log."; 28 | 29 | public static final ConfigKey COMMON_LOG_LEVEL = ConfigKey 30 | .build( 31 | LOG_PREFIX + "level", 32 | "info", 33 | true, 34 | "控制日志输出级别", 35 | new String[] { "__cloud_engine__LOG_LEVEL" }); 36 | 37 | public static final ConfigKey COMMON_LOG_FILE = ConfigKey 38 | .build( 39 | LOG_PREFIX + "file", 40 | "./logs", 41 | false, 42 | "控制输出路径", 43 | new String[] { "__cloud_engine__LOG_FILE" }); 44 | 45 | public static final String THREAD_PREFIX = "com.alipay.sofa.common.thread."; 46 | 47 | public static final ConfigKey COMMON_THREAD_LOG_PERIOD = ConfigKey 48 | .build( 49 | THREAD_PREFIX + "period", 50 | 10L, 51 | false, 52 | "控制线程信息打印间隔", 53 | new String[] { "thread_log_period" }); 54 | 55 | } -------------------------------------------------------------------------------- /src/main/java21/com/alipay/sofa/common/thread/virtual/SofaVirtualThreadFactory.java: -------------------------------------------------------------------------------- 1 | package com.alipay.sofa.common.thread.virtual; 2 | 3 | import java.util.concurrent.ExecutorService; 4 | import java.util.concurrent.Executors; 5 | import java.util.concurrent.ThreadFactory; 6 | 7 | /** 8 | * Delegate for virtual thread handling on JDK 21. 9 | * This is the actual version compiled against JDK 21. 10 | * 11 | * @author huzijie 12 | * @version SofaVirtualThreadFactory.java, v 0.1 2023年11月20日 3:57 PM huzijie Exp $ 13 | */ 14 | public class SofaVirtualThreadFactory { 15 | 16 | /** 17 | * Create a virtual thread with name. 18 | * 19 | * @param name thread name 20 | * @param runnable task to run with thread 21 | * @return a virtual thread with runnable 22 | * @since 2.1.0 23 | */ 24 | public static Thread ofThread(String name, Runnable runnable) { 25 | return Thread.ofVirtual().name(name).unstarted(runnable); 26 | } 27 | 28 | /** 29 | * Create a virtual thread with name. 30 | * 31 | * @param name thread name 32 | * @param inheritInheritableThreadLocals {@code true} to inherit, {@code false} to not inherit 33 | * @param uncaughtExceptionHandler uncaught exception handler 34 | * @param runnable task to run with thread 35 | * @return a virtual thread with runnable 36 | * @since 2.1.0 37 | */ 38 | 39 | public static Thread ofThread(String name, boolean inheritInheritableThreadLocals, Thread.UncaughtExceptionHandler uncaughtExceptionHandler , Runnable runnable) { 40 | return Thread.ofVirtual().name(name).inheritInheritableThreadLocals(inheritInheritableThreadLocals) 41 | .uncaughtExceptionHandler(uncaughtExceptionHandler).unstarted(runnable); 42 | } 43 | 44 | /** 45 | * Creates an Executor that starts a new virtual Thread for each task. 46 | * The number of threads created by the Executor is unbounded. 47 | * 48 | * @param prefix thread prefix 49 | * @return a new executor that creates a new virtual Thread for each task 50 | * @since 2.1.0 51 | */ 52 | public static ExecutorService ofExecutorService(String prefix) { 53 | return Executors.newThreadPerTaskExecutor(Thread.ofVirtual().name(prefix, 0).factory()); 54 | } 55 | 56 | /** 57 | * Creates an Executor that starts a new virtual Thread for each task. 58 | * The number of threads created by the Executor is unbounded. 59 | * 60 | * @param prefix thread prefix 61 | * @param start the starting value of the counter 62 | * @param inheritInheritableThreadLocals {@code true} to inherit, {@code false} to not inherit 63 | * @param uncaughtExceptionHandler uncaught exception handler 64 | * @return a new executor that creates a new virtual Thread for each task 65 | * @since 2.1.0 66 | */ 67 | public static ExecutorService ofExecutorService(String prefix, long start, boolean inheritInheritableThreadLocals, Thread.UncaughtExceptionHandler uncaughtExceptionHandler) { 68 | ThreadFactory threadFactory = Thread.ofVirtual().name(prefix, start) 69 | .inheritInheritableThreadLocals(inheritInheritableThreadLocals) 70 | .uncaughtExceptionHandler(uncaughtExceptionHandler) 71 | .factory(); 72 | return Executors.newThreadPerTaskExecutor(threadFactory); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/sofa/common/security/SensitiveDataUtilTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.security; 18 | 19 | import org.junit.After; 20 | import org.junit.Assert; 21 | import org.junit.Before; 22 | import org.junit.Test; 23 | 24 | /** 25 | * SensitiveDataUtil Tester. 26 | * 27 | * Repo : git@gitlab.alipay-inc.com:alipay-sofa/alipay-service-security.git 28 | * 29 | * @author 30 | * @version 1.0 31 | * @since

八月 11, 2017
32 | */ 33 | public class SensitiveDataUtilTest { 34 | 35 | @Before 36 | public void before() throws Exception { 37 | } 38 | 39 | @After 40 | public void after() throws Exception { 41 | } 42 | 43 | @Test 44 | public void test() { 45 | Assert.assertEquals("130******58", SensitiveDataUtil.alipayLogonIdHide("13012345658")); 46 | Assert.assertEquals("tit***@qq.com", 47 | SensitiveDataUtil.alipayLogonIdHide("titxue.yang@qq.com")); 48 | Assert.assertEquals("13****23", SensitiveDataUtil.alipayLogonIdHide("13123423")); 49 | Assert.assertEquals("130*58", SensitiveDataUtil.alipayLogonIdHideSMS("13012345658")); 50 | Assert 51 | .assertEquals("tit*@qq.*", SensitiveDataUtil.alipayLogonIdHideSMS("tit1.2345@qq.com")); 52 | Assert.assertEquals("13*23", SensitiveDataUtil.alipayLogonIdHideSMS("13123423")); 53 | Assert.assertEquals("6****************8", 54 | SensitiveDataUtil.idCardNoHide("612345678901234568")); 55 | Assert.assertEquals("622575******1496", 56 | SensitiveDataUtil.bankCardNoHide("6225751234561496")); 57 | Assert.assertEquals("62257****1496", SensitiveDataUtil.bankCardNoHide("6225712341496")); 58 | Assert.assertEquals("130******58", SensitiveDataUtil.phoneOrTelNoHide("13012345658")); 59 | Assert.assertEquals("0571-****8888", SensitiveDataUtil.phoneOrTelNoHide("0571-12348888")); 60 | Assert.assertEquals("130******58", SensitiveDataUtil.cellphoneHide("13012345658")); 61 | Assert.assertEquals("07****358", SensitiveDataUtil.cellphoneHide("071234358")); 62 | Assert.assertEquals("130*58", SensitiveDataUtil.cellphoneHideSMS("13012345658")); 63 | Assert.assertEquals("07*358", SensitiveDataUtil.cellphoneHideSMS("071234358")); 64 | Assert.assertEquals("*雷", SensitiveDataUtil.nameHide("李雷")); 65 | Assert.assertEquals("*梅梅", SensitiveDataUtil.nameHide("韩梅梅")); 66 | Assert.assertEquals("**相如", SensitiveDataUtil.nameHide("司马相如")); 67 | Assert.assertEquals("穆*****耶稣", SensitiveDataUtil.nameHide("穆罕穆德-约耶稣")); 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/log/CommonLoggingConfigurations.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.log; 18 | 19 | import com.alipay.sofa.common.utils.StringUtil; 20 | 21 | import java.util.Map; 22 | import java.util.Set; 23 | import java.util.concurrent.ConcurrentHashMap; 24 | 25 | /** 26 | * @author Alaneuler 27 | * Created on 2020/11/9 28 | */ 29 | public class CommonLoggingConfigurations { 30 | 31 | private final static Set LOGGER_CONSOLE_WHITE_SET = ConcurrentHashMap.newKeySet(); 32 | 33 | private final static Set LOGGER_CONSOLE_PREFIX_WHITE_SET = ConcurrentHashMap.newKeySet(); 34 | 35 | /** 36 | * For configurations from outside, especially Spring Boot 37 | */ 38 | private final static Map EXTERNAL_CONFIGURATIONS = new ConcurrentHashMap<>(); 39 | 40 | /** 41 | * Subsequent same invocation will override previous value 42 | */ 43 | public static void loadExternalConfiguration(String key, String value) { 44 | if (StringUtil.isNotEmpty(key) && StringUtil.isNotEmpty(value)) { 45 | EXTERNAL_CONFIGURATIONS.put(key, value); 46 | } 47 | } 48 | 49 | public static Map getExternalConfigurations() { 50 | return EXTERNAL_CONFIGURATIONS; 51 | } 52 | 53 | public static void appendConsoleLoggerName(String loggerName) { 54 | LOGGER_CONSOLE_WHITE_SET.add(loggerName); 55 | } 56 | 57 | public static void appendConsolePrefixWhiteLoggerName(String loggerName) { 58 | LOGGER_CONSOLE_PREFIX_WHITE_SET.add(loggerName); 59 | } 60 | 61 | public static void addAllConsoleLogger(Set set) { 62 | LOGGER_CONSOLE_WHITE_SET.addAll(set); 63 | } 64 | 65 | public static void addAllConsolePrefixWhiteLoggerName(Set set) { 66 | LOGGER_CONSOLE_PREFIX_WHITE_SET.addAll(set); 67 | } 68 | 69 | public static Set getLoggerConsoleWhiteSet() { 70 | return LOGGER_CONSOLE_WHITE_SET; 71 | } 72 | 73 | public static Set getLoggerConsolePrefixWhiteSet() { 74 | return LOGGER_CONSOLE_PREFIX_WHITE_SET; 75 | } 76 | 77 | public static boolean shouldAttachConsoleAppender(String loggerName) { 78 | return LOGGER_CONSOLE_WHITE_SET.contains(loggerName) || LOGGER_CONSOLE_PREFIX_WHITE_SET.stream().anyMatch(loggerName::startsWith); 79 | } 80 | 81 | public static void clearLoggerConsoleProperties() { 82 | LOGGER_CONSOLE_WHITE_SET.clear(); 83 | LOGGER_CONSOLE_PREFIX_WHITE_SET.clear(); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/config/SofaConfigs.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.config; 18 | 19 | import com.alipay.sofa.common.config.listener.LogConfigListener; 20 | import com.alipay.sofa.common.config.source.SystemEnvConfigSource; 21 | import com.alipay.sofa.common.config.source.SystemPropertyConfigSource; 22 | 23 | import static com.alipay.sofa.common.log.Constants.SOFA_MIDDLEWARE_CONFIG_CACHE_EXPIRE_TIME; 24 | import static com.alipay.sofa.common.log.Constants.SOFA_MIDDLEWARE_CONFIG_CACHE_MAX_SIZE; 25 | 26 | /** 27 | * @author zhaowang 28 | * @version : SofaCommonConfig.java, v 0.1 2020年12月01日 11:54 上午 zhaowang Exp $ 29 | */ 30 | public class SofaConfigs { 31 | 32 | private static final DefaultConfigManager INSTANCE; 33 | public static final String DEFAULT_EXPIRE_AFTER_SECOND = "5"; 34 | public static final String DEFAULT_MAX_SIZE = "1000"; 35 | 36 | static { 37 | long expireAfterSecond = Long.parseLong(System.getProperty( 38 | SOFA_MIDDLEWARE_CONFIG_CACHE_EXPIRE_TIME, DEFAULT_EXPIRE_AFTER_SECOND)); 39 | long maxSize = Long.parseLong(System.getProperty(SOFA_MIDDLEWARE_CONFIG_CACHE_MAX_SIZE, 40 | DEFAULT_MAX_SIZE)); 41 | 42 | INSTANCE = new DefaultConfigManager(expireAfterSecond, maxSize); 43 | // add ConfigSource 44 | INSTANCE.addConfigSource(new SystemPropertyConfigSource()); 45 | INSTANCE.addConfigSource(new SystemEnvConfigSource()); 46 | 47 | // add ConfigListener 48 | INSTANCE.addConfigListener(new LogConfigListener()); 49 | } 50 | 51 | public static T getOrDefault(ConfigKey key) { 52 | return INSTANCE.getOrDefault(key); 53 | } 54 | 55 | public static T getOrCustomDefault(ConfigKey key, T customDefault) { 56 | return INSTANCE.getOrCustomDefault(key, customDefault); 57 | } 58 | 59 | public static T getOrCustomDefaultWithCache(ConfigKey key, T customDefault) { 60 | return INSTANCE.getOrCustomDefaultWithCache(key, customDefault); 61 | } 62 | 63 | public static T getOrDefaultWithCache(ConfigKey key) { 64 | return INSTANCE.getOrDefaultWithCache(key); 65 | } 66 | 67 | public static void addConfigSource(ConfigSource configSource) { 68 | INSTANCE.addConfigSource(configSource); 69 | } 70 | 71 | public static void addConfigListener(ManagementListener configListener) { 72 | INSTANCE.addConfigListener(configListener); 73 | } 74 | 75 | public static DefaultConfigManager getInstance() { 76 | return INSTANCE; 77 | } 78 | } -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/config/ConfigKey.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.config; 18 | 19 | import java.util.Arrays; 20 | import java.util.Objects; 21 | 22 | /** 23 | * @author zhaowang 24 | * @version : SofaConfig.java, v 0.1 2020年10月20日 8:01 下午 zhaowang Exp $ 25 | *

26 | * 通过String key 拿到 ConfigKey。 27 | */ 28 | public class ConfigKey { 29 | 30 | private final String key; 31 | /** 32 | * alias of sofa config, previous alias has higher priority 33 | */ 34 | private final String[] alias; 35 | 36 | private final T defaultValue; 37 | 38 | private final String description; 39 | 40 | /** 41 | * Indicate that if change this configSource will take effect at runtime 42 | */ 43 | private final boolean modifiable; 44 | 45 | public ConfigKey(String key, String[] alias, T defaultValue, boolean modifiable, 46 | String description) { 47 | preCheckNotNull("key", key); 48 | preCheckNotNull("defaultValue", defaultValue); 49 | preCheckNotNull("description", description); 50 | 51 | this.key = key; 52 | this.alias = Objects.requireNonNullElseGet(alias, () -> new String[0]); 53 | this.defaultValue = defaultValue; 54 | this.modifiable = modifiable; 55 | this.description = description; 56 | } 57 | 58 | public static ConfigKey build(String key, T defaultValue, boolean modifiable, String description) { 59 | return new ConfigKey<>(key, null, defaultValue, modifiable, description); 60 | } 61 | 62 | public static ConfigKey build(String key, T defaultValue, boolean modifiable, String description, String[] alias) { 63 | return new ConfigKey<>(key, alias, defaultValue, modifiable, description); 64 | } 65 | 66 | private void preCheckNotNull(String key, Object value) { 67 | if (value == null) { 68 | throw new NullPointerException("\"" + key 69 | + "\" of ConfigKey cannot be null,please check it"); 70 | } 71 | } 72 | 73 | public String getKey() { 74 | return key; 75 | } 76 | 77 | public String[] getAlias() { 78 | return Arrays.copyOf(alias, alias.length); 79 | } 80 | 81 | public T getDefaultValue() { 82 | return defaultValue; 83 | } 84 | 85 | public String getDescription() { 86 | return description; 87 | } 88 | 89 | public Class getType() { 90 | return (Class) defaultValue.getClass(); 91 | } 92 | 93 | public boolean isModifiable() { 94 | return modifiable; 95 | } 96 | } -------------------------------------------------------------------------------- /src/test/java/com/alipay/sofa/common/log/LazyLogFactoryInitializingTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.log; 18 | 19 | import org.junit.Assert; 20 | import org.junit.Test; 21 | import org.slf4j.Logger; 22 | 23 | import java.io.File; 24 | import java.nio.charset.StandardCharsets; 25 | import java.nio.file.Files; 26 | import java.nio.file.Paths; 27 | import java.util.HashMap; 28 | import java.util.List; 29 | import java.util.Map; 30 | 31 | /** 32 | * @author Alaneuler 33 | * Created on 2021/3/3 34 | */ 35 | public class LazyLogFactoryInitializingTest { 36 | public static final String SPACE_NAME = "lazy.init"; 37 | 38 | static { 39 | System.setProperty(Constants.LOGBACK_MIDDLEWARE_LOG_DISABLE_PROP_KEY, "true"); 40 | } 41 | 42 | public static Logger logger = MultiAppLoggerSpaceManager.getLoggerBySpace( 43 | "LAZY-INIT", SPACE_NAME); 44 | 45 | @Test 46 | public void test() throws Exception { 47 | logger.info("test1"); 48 | logger.info("test2"); 49 | logger.info("test3"); 50 | logger.info("test4"); 51 | 52 | File directory = new File(Constants.LOGGING_PATH_DEFAULT + "/lazy"); 53 | if (directory.isDirectory()) { 54 | File[] files = directory.listFiles(); 55 | if (files != null) { 56 | for (File f : files) { 57 | if (f.getName().startsWith("monitor.log")) { 58 | Assert.fail(); 59 | } 60 | } 61 | } 62 | } 63 | 64 | Map props = new HashMap<>(); 65 | props.put("logging.path." + SPACE_NAME, "./logs/"); 66 | props.put("logging.level." + SPACE_NAME, "INFO"); 67 | props.put("date.pattern." + SPACE_NAME, "yyyy-MM-dd"); 68 | MultiAppLoggerSpaceManager.init(SPACE_NAME, props); 69 | 70 | try { 71 | Files.write(Paths.get("./logs/lazy/monitor.log"), "".getBytes(StandardCharsets.UTF_8)); 72 | } catch (Throwable e) { 73 | // just ignore 74 | } 75 | logger.info("test1"); 76 | logger.info("test2"); 77 | logger.info("test3"); 78 | logger.info("test4"); 79 | 80 | List lines = Files.readAllLines(Paths.get("./logs/lazy/monitor.log"), StandardCharsets.UTF_8); 81 | int count = 0; 82 | for (String line : lines) { 83 | if (line.contains("LAZY-INIT - test")) { 84 | ++count; 85 | } 86 | } 87 | Assert.assertTrue(count >= 4); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/sofa/common/log/LogEnvUtilsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.log; 18 | 19 | import com.alipay.sofa.common.log.base.AbstraceLogTestBase; 20 | import com.alipay.sofa.common.log.env.LogEnvUtils; 21 | import org.apache.logging.log4j.util.Strings; 22 | import org.junit.After; 23 | import org.junit.Assert; 24 | import org.junit.Before; 25 | import org.junit.Test; 26 | 27 | import java.util.Map; 28 | 29 | import static com.alipay.sofa.common.log.Constants.*; 30 | 31 | /** 32 | * @author qilong.zql 17/11/15-上午11:00 33 | */ 34 | public class LogEnvUtilsTest extends AbstraceLogTestBase { 35 | 36 | @Before 37 | @Override 38 | public void before() { 39 | System.setProperty(Constants.LOG_ENV_SUFFIX, "app:dev&app2:test&app3:"); 40 | } 41 | 42 | @After 43 | @Override 44 | public void after() { 45 | System.clearProperty(Constants.LOG_ENV_SUFFIX); 46 | } 47 | 48 | @Test 49 | public void test() { 50 | Assert.assertEquals(".dev", LogEnvUtils.getLogConfEnvSuffix("app")); 51 | Assert.assertEquals(".test", LogEnvUtils.getLogConfEnvSuffix("app2")); 52 | Assert.assertEquals(Strings.EMPTY, LogEnvUtils.getLogConfEnvSuffix("app3")); 53 | Assert.assertEquals(Strings.EMPTY, LogEnvUtils.getLogConfEnvSuffix("app4")); 54 | } 55 | 56 | @Test 57 | public void testClearGlobalSystemProperties() { 58 | String oldLogPath = System.getProperty(LOG_PATH); 59 | 60 | Map properties = LogEnvUtils.processGlobalSystemLogProperties(); 61 | Assert.assertEquals(properties.get(LOG_PATH), LOGGING_PATH_DEFAULT); 62 | Assert.assertEquals(properties.get(OLD_LOG_PATH), LOGGING_PATH_DEFAULT); 63 | Assert.assertTrue(LogEnvUtils.isUseDefaultSystemProperties()); 64 | Assert.assertNull(properties.get("abc")); 65 | 66 | LogEnvUtils.clearGlobalSystemProperties(); 67 | CommonLoggingConfigurations.loadExternalConfiguration("abc", "efg"); 68 | System.setProperty(LOG_PATH, LOGGING_PATH_DEFAULT + "test"); 69 | System.setProperty(OLD_LOG_PATH, LOGGING_PATH_DEFAULT + "test"); 70 | properties = LogEnvUtils.processGlobalSystemLogProperties(); 71 | Assert.assertEquals(properties.get(LOG_PATH), LOGGING_PATH_DEFAULT + "test"); 72 | Assert.assertEquals(properties.get(OLD_LOG_PATH), LOGGING_PATH_DEFAULT + "test"); 73 | Assert.assertFalse(LogEnvUtils.isUseDefaultSystemProperties()); 74 | Assert.assertEquals(properties.get("abc"), "efg"); 75 | 76 | if (oldLogPath != null) { 77 | System.setProperty(LOG_PATH, oldLogPath); 78 | } 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/sofa/common/config/ConfigKeyTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.config; 18 | 19 | import org.junit.Assert; 20 | import org.junit.Test; 21 | 22 | /** 23 | * @author zhaowang 24 | * @version : ConfigKeyTest.java, v 0.1 2020年12月09日 10:48 上午 zhaowang Exp $ 25 | */ 26 | public class ConfigKeyTest { 27 | 28 | @Test 29 | public void testInit() { 30 | 31 | try { 32 | ConfigKey configKey = new ConfigKey(null, null, null, false, null); 33 | Assert.fail(); 34 | } catch (Exception e) { 35 | Assert.assertTrue(e instanceof NullPointerException); 36 | Assert.assertTrue(e.getMessage().contains("key")); 37 | } 38 | 39 | try { 40 | ConfigKey configKey = new ConfigKey("a", null, null, false, null); 41 | Assert.fail(); 42 | } catch (Exception e) { 43 | Assert.assertTrue(e instanceof NullPointerException); 44 | Assert.assertTrue(e.getMessage().contains("defaultValue")); 45 | } 46 | 47 | try { 48 | ConfigKey configKey = new ConfigKey("a", null, "", false, null); 49 | Assert.fail(); 50 | } catch (Exception e) { 51 | Assert.assertTrue(e instanceof NullPointerException); 52 | Assert.assertTrue(e.getMessage().contains("description")); 53 | } 54 | 55 | ConfigKey configKey = new ConfigKey("a", null, "", false, "desc"); 56 | Assert.assertNotNull(configKey); 57 | String[] alias = configKey.getAlias(); 58 | Assert.assertNotNull(alias); 59 | Assert.assertEquals(0, alias.length); 60 | Assert.assertFalse(configKey.isModifiable()); 61 | Assert.assertEquals("desc", configKey.getDescription()); 62 | Assert.assertEquals(String.class, configKey.getType()); 63 | } 64 | 65 | @Test 66 | public void testBuild() { 67 | ConfigKey key = ConfigKey.build("a", "default", true, "desc"); 68 | Assert.assertEquals("a", key.getKey()); 69 | Assert.assertEquals("default", key.getDefaultValue()); 70 | Assert.assertTrue(key.isModifiable()); 71 | Assert.assertEquals("desc", key.getDescription()); 72 | Assert.assertEquals(0, key.getAlias().length); 73 | 74 | key = ConfigKey.build("a", "default", true, "desc", new String[] { "b", "c" }); 75 | Assert.assertEquals("a", key.getKey()); 76 | Assert.assertEquals("default", key.getDefaultValue()); 77 | Assert.assertTrue(key.isModifiable()); 78 | Assert.assertEquals("desc", key.getDescription()); 79 | Assert.assertEquals(2, key.getAlias().length); 80 | 81 | } 82 | } -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/profile/enumeration/IntegerEnum.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.profile.enumeration; 18 | 19 | /** 20 | * 21 | * @author luoguimu123 22 | * @version $Id: IntegerEnum.java, v 0.1 2017年08月01日 上午11:47 luoguimu123 Exp $ 23 | */ 24 | public abstract class IntegerEnum extends Enum { 25 | private static final long serialVersionUID = 343392921439669443L; 26 | 27 | public IntegerEnum() { 28 | } 29 | 30 | protected static final Enum create(int value) { 31 | return (Enum) createEnum(new Integer(value)); 32 | } 33 | 34 | protected static final Enum create(Number value) { 35 | return (Enum) createEnum(new Integer(value.intValue())); 36 | } 37 | 38 | protected static final Enum create(String name, int value) { 39 | return (Enum) createEnum(name, new Integer(value)); 40 | } 41 | 42 | protected static final Enum create(String name, Number value) { 43 | return (Enum) createEnum(name, new Integer(value.intValue())); 44 | } 45 | 46 | protected static Object createEnumType() { 47 | return new EnumType() { 48 | protected Class getUnderlyingClass() { 49 | return Integer.class; 50 | } 51 | 52 | protected Number getNextValue(Number value, boolean flagMode) { 53 | if (value == null) { 54 | return flagMode ? new Integer(1) : new Integer(0); 55 | } else { 56 | int intValue = ((Integer) value).intValue(); 57 | return flagMode ? new Integer(intValue << 1) : new Integer(intValue + 1); 58 | } 59 | } 60 | 61 | protected boolean isZero(Number value) { 62 | return ((Integer) value).intValue() == 0; 63 | } 64 | }; 65 | } 66 | 67 | public int intValue() { 68 | return ((Integer) this.getValue()).intValue(); 69 | } 70 | 71 | public long longValue() { 72 | return ((Integer) this.getValue()).longValue(); 73 | } 74 | 75 | public double doubleValue() { 76 | return ((Integer) this.getValue()).doubleValue(); 77 | } 78 | 79 | public float floatValue() { 80 | return ((Integer) this.getValue()).floatValue(); 81 | } 82 | 83 | public String toHexString() { 84 | return Integer.toHexString(((Integer) this.getValue()).intValue()); 85 | } 86 | 87 | public String toOctalString() { 88 | return Integer.toOctalString(((Integer) this.getValue()).intValue()); 89 | } 90 | 91 | public String toBinaryString() { 92 | return Integer.toBinaryString(((Integer) this.getValue()).intValue()); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/sofa/common/space/SpaceId.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.space; 18 | 19 | import com.alipay.sofa.common.utils.AssertUtil; 20 | 21 | import java.util.*; 22 | import java.util.concurrent.ConcurrentHashMap; 23 | 24 | /** 25 | * Unified Space Id in sofa-common-tools to identify: 26 | * 1. LoggerFactory 27 | * 2. ThreadPoolSpace 28 | * 3. LogCode2Description 29 | * 30 | * SpaceId contains properties for the space it points. 31 | * @author xuanbei 32 | * @since 2017/07/03 33 | */ 34 | public class SpaceId { 35 | private static final Map GLOBAL_SPACE_ID_CACHE = new ConcurrentHashMap<>(); 36 | 37 | private final Map tags = new HashMap<>(); 38 | 39 | private final String spaceName; 40 | 41 | public SpaceId(String spaceName) { 42 | AssertUtil.notNull(spaceName); 43 | this.spaceName = spaceName; 44 | } 45 | 46 | public static SpaceId withSpaceName(String spaceName) { 47 | return GLOBAL_SPACE_ID_CACHE.computeIfAbsent(spaceName, SpaceId::new); 48 | } 49 | 50 | public SpaceId withTag(String key, String value) { 51 | tags.put(key, value); 52 | return this; 53 | } 54 | 55 | public String getSpaceName() { 56 | return spaceName; 57 | } 58 | 59 | @Override 60 | public boolean equals(Object o) { 61 | if (this == o) 62 | return true; 63 | if (o == null || getClass() != o.getClass()) 64 | return false; 65 | 66 | SpaceId spaceId = (SpaceId) o; 67 | 68 | if (!tags.equals(spaceId.tags)) 69 | return false; 70 | return Objects.equals(spaceName, spaceId.spaceName); 71 | } 72 | 73 | @Override 74 | public int hashCode() { 75 | int result = tags.hashCode(); 76 | result = 31 * result + (spaceName != null ? spaceName.hashCode() : 0); 77 | return result; 78 | } 79 | 80 | @Override 81 | public String toString() { 82 | 83 | if (tags.size() == 0) { 84 | return spaceName; 85 | } 86 | 87 | StringBuilder sb = new StringBuilder(spaceName); 88 | sb.append("["); 89 | Iterator> iterator = tags.entrySet().iterator(); 90 | while (iterator.hasNext()) { 91 | Map.Entry entry = iterator.next(); 92 | sb.append(entry.getKey()).append("=").append(entry.getValue()); 93 | if (iterator.hasNext()) { 94 | sb.append(", "); 95 | } 96 | } 97 | sb.append("]"); 98 | return sb.toString(); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/sofa/common/thread/SofaThreadPoolTaskExecutorTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.thread; 18 | 19 | import org.junit.Assert; 20 | import org.junit.Test; 21 | 22 | /** 23 | * @author Alaneuler 24 | * Created on 2020/3/23 25 | */ 26 | public class SofaThreadPoolTaskExecutorTest extends ThreadPoolTestBase { 27 | @Test 28 | public void test() throws Exception { 29 | SofaThreadPoolTaskExecutor threadPool = new SofaThreadPoolTaskExecutor(); 30 | threadPool.initialize(); 31 | Assert.assertTrue(isMatch(getInfoViaIndex(0), INFO, String.format( 32 | "Thread pool '%s\\S+' started with period: %s %s", 33 | SofaThreadPoolTaskExecutor.SIMPLE_CLASS_NAME, threadPool.getPeriod(), 34 | threadPool.getTimeUnit()))); 35 | Assert.assertTrue(isLastInfoMatch(String.format( 36 | "Thread pool with name '%s\\S+' registered", 37 | SofaThreadPoolTaskExecutor.SIMPLE_CLASS_NAME))); 38 | 39 | threadPool.setPeriod(1000); 40 | Assert.assertTrue(isLastInfoMatch(String.format( 41 | "Restart thread pool '%s\\S+' with period: %s %s", 42 | SofaThreadPoolTaskExecutor.SIMPLE_CLASS_NAME, threadPool.getPeriod(), 43 | threadPool.getTimeUnit()))); 44 | 45 | threadPool.setTaskTimeout(2200); 46 | Assert.assertTrue(isLastInfoMatch(String.format("Updated '%s\\S+' taskTimeout to %s %s", 47 | SofaThreadPoolTaskExecutor.SIMPLE_CLASS_NAME, threadPool.getTaskTimeout(), 48 | threadPool.getTimeUnit()))); 49 | threadPool.execute(new SleepTask(4200)); 50 | threadPool.execute(new SleepTask(4200)); 51 | Thread.sleep(9500); 52 | 53 | Assert.assertEquals(15, infoListAppender.list.size()); 54 | Assert.assertEquals(2, aberrantListAppender.list.size()); 55 | Assert.assertTrue(consecutiveInfoPattern(4, "1,1,0,1,0", "1,1,0,1,0", "1,1,0,1,1", 56 | "1,1,0,1,1", "0,1,0,1,0", "\\d,420\\d", "0,1,0,1,0", "0,1,0,1,1", "0,1,0,1,1", 57 | "0,0,1,1,0", "420\\d,420\\d")); 58 | Assert.assertTrue(isMatch(lastWarnString().split("\n")[0], WARN, String.format( 59 | "Task .+ in thread pool (%s\\S+) started on \\S+ \\S+ with traceId \\S+ " 60 | + "exceeds the limit of \\S+ execution time with stack trace:", 61 | SofaThreadPoolTaskExecutor.SIMPLE_CLASS_NAME))); 62 | threadPool.setWaitForTasksToCompleteOnShutdown(true); 63 | threadPool.setAwaitTerminationSeconds(100); 64 | threadPool.shutdown(); 65 | Assert.assertTrue(isLastInfoMatch(String.format( 66 | "Thread pool with name '%s\\S+' unregistered", 67 | SofaThreadPoolTaskExecutor.SIMPLE_CLASS_NAME))); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/sofa/common/log/LogbackConsoleLoggingTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.sofa.common.log; 18 | 19 | import com.alipay.sofa.common.log.base.AbstraceLogTestBase; 20 | import com.alipay.sofa.common.log.env.LogEnvUtils; 21 | import com.alipay.sofa.common.log.factory.AbstractLoggerSpaceFactory; 22 | import com.alipay.sofa.common.log.factory.LoggerSpaceFactory4LogbackBuilder; 23 | import com.alipay.sofa.common.space.SpaceId; 24 | import org.junit.After; 25 | import org.junit.Assert; 26 | import org.junit.Before; 27 | import org.junit.Test; 28 | import org.slf4j.Logger; 29 | 30 | import java.io.ByteArrayOutputStream; 31 | import java.io.PrintStream; 32 | 33 | public class LogbackConsoleLoggingTest extends AbstraceLogTestBase { 34 | @Before 35 | @Override 36 | public void before() throws Exception { 37 | super.before(); 38 | } 39 | 40 | @After 41 | @Override 42 | public void after() throws Exception { 43 | super.after(); 44 | } 45 | 46 | @Test 47 | public void testConsoleLogLevel() throws Exception { 48 | String loggerName = "com.foo.bar.console"; 49 | String spaceName = "sofa.console"; 50 | ByteArrayOutputStream outContent = new ByteArrayOutputStream(); 51 | System.setOut(new PrintStream(outContent)); 52 | 53 | LogSpace spaceInfo = new LogSpace() 54 | .setProperty(Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_SWITCH, "true") 55 | .setProperty(Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_LEVEL, "WARN") 56 | .putAll(LogEnvUtils.processGlobalSystemLogProperties()); 57 | CommonLoggingConfigurations.appendConsoleLoggerName(loggerName); 58 | 59 | LoggerSpaceFactory4LogbackBuilder loggerSpaceFactory4LogbackBuilder = new LoggerSpaceFactory4LogbackBuilder( 60 | new SpaceId(spaceName), spaceInfo); 61 | AbstractLoggerSpaceFactory loggerSpaceFactory = loggerSpaceFactory4LogbackBuilder.build( 62 | spaceName, this.getClass().getClassLoader()); 63 | 64 | Logger logger = loggerSpaceFactory.getLogger(loggerName); 65 | 66 | String traceLog = "test trace info"; 67 | String debugLog = "test debug info"; 68 | String infoLog = "test info info"; 69 | String warnLog = "test warn info"; 70 | String errorLog = "test error info"; 71 | logger.trace(traceLog); 72 | logger.debug(debugLog); 73 | logger.info(infoLog); 74 | logger.warn(warnLog); 75 | logger.error(errorLog); 76 | 77 | String logString = outContent.toString(); 78 | Assert.assertTrue(logString.contains(warnLog)); 79 | Assert.assertTrue(logString.contains(errorLog)); 80 | Assert.assertFalse(logString.contains(traceLog)); 81 | Assert.assertFalse(logString.contains(debugLog)); 82 | Assert.assertFalse(logString.contains(infoLog)); 83 | } 84 | } 85 | --------------------------------------------------------------------------------