forked from alibaba/nacos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ISSUE alibaba#8622] sub pr of 9356 - logging module (alibaba#9450)
* [ISSUE alibaba#8622] sub pr of 9356 - logging module - Replace system.getProperties and system.getEnv with NacosClientProperties - log4j and logback support reading properties from NacosClientProperties relate alibaba#9356 * [ISSUE alibaba#8622] add unit tests for logging
- Loading branch information
Showing
9 changed files
with
309 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...nt/src/main/java/com/alibaba/nacos/client/logging/log4j2/NacosClientPropertiesLookup.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 1999-2022 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.alibaba.nacos.client.logging.log4j2; | ||
|
||
import com.alibaba.nacos.client.env.NacosClientProperties; | ||
import org.apache.logging.log4j.core.LogEvent; | ||
import org.apache.logging.log4j.core.config.plugins.Plugin; | ||
import org.apache.logging.log4j.core.lookup.AbstractLookup; | ||
import org.apache.logging.log4j.core.lookup.StrLookup; | ||
|
||
/** | ||
* support log4j2 read properties from NacosClientProperties. | ||
* for example: | ||
* <SizeBasedTriggeringPolicy size="${nacosClientProperty:JM.LOG.FILE.SIZE:-10MB}"/> | ||
* @author onewe | ||
*/ | ||
@Plugin(name = "nacosClientProperty", category = StrLookup.CATEGORY) | ||
public class NacosClientPropertiesLookup extends AbstractLookup { | ||
|
||
@Override | ||
public String lookup(LogEvent event, String key) { | ||
return NacosClientProperties.PROTOTYPE.getProperty(key); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
client/src/main/java/com/alibaba/nacos/client/logging/logback/NacosClientPropertyAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright 1999-2022 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.alibaba.nacos.client.logging.logback; | ||
|
||
import ch.qos.logback.core.joran.action.Action; | ||
import ch.qos.logback.core.joran.action.ActionUtil; | ||
import ch.qos.logback.core.joran.spi.ActionException; | ||
import ch.qos.logback.core.joran.spi.InterpretationContext; | ||
import ch.qos.logback.core.util.OptionHelper; | ||
import com.alibaba.nacos.client.env.NacosClientProperties; | ||
import org.xml.sax.Attributes; | ||
|
||
/** | ||
* support logback read properties from NacosClientProperties. just like springProperty. | ||
* for example: | ||
* <nacosClientProperty scope="context" name="logPath" source="system.log.path" defaultValue="/root" /> | ||
* @author onewe | ||
*/ | ||
class NacosClientPropertyAction extends Action { | ||
|
||
private static final String DEFAULT_VALUE_ATTRIBUTE = "defaultValue"; | ||
|
||
private static final String SOURCE_ATTRIBUTE = "source"; | ||
|
||
@Override | ||
public void begin(InterpretationContext ic, String elementName, Attributes attributes) throws ActionException { | ||
String name = attributes.getValue(NAME_ATTRIBUTE); | ||
String source = attributes.getValue(SOURCE_ATTRIBUTE); | ||
ActionUtil.Scope scope = ActionUtil.stringToScope(attributes.getValue(SCOPE_ATTRIBUTE)); | ||
String defaultValue = attributes.getValue(DEFAULT_VALUE_ATTRIBUTE); | ||
if (OptionHelper.isEmpty(name)) { | ||
addError("The \"name\" and \"source\" attributes of <nacosClientProperty> must be set"); | ||
} | ||
ActionUtil.setProperty(ic, name, getValue(source, defaultValue), scope); | ||
} | ||
|
||
@Override | ||
public void end(InterpretationContext ic, String name) throws ActionException { | ||
|
||
} | ||
|
||
private String getValue(String source, String defaultValue) { | ||
return NacosClientProperties.PROTOTYPE.getProperty(source, defaultValue); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.