Skip to content

Commit

Permalink
[ISSUE alibaba#8622] support logging read properties from NacosClient…
Browse files Browse the repository at this point in the history
…Properties

- add NacosClientPropertiesLookup for log4j2
- add NacosClientPropertyAction for logback
- add a new constructor method to adapt NacosClientProperties for NacosNamingMaintainService
- add a new constructor method to adapt NacosClientProperties for NacosNamingService
- add a new constructor method to adapt NacosClientProperties for NacosConfigService
  • Loading branch information
onewe committed Oct 20, 2022
1 parent 9bdd489 commit 6332bf4
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,20 @@ public class NacosConfigService implements ConfigService {
private final ConfigFilterChainManager configFilterChainManager;

public NacosConfigService(Properties properties) throws NacosException {
NacosClientProperties clientProperties = NacosClientProperties.PROTOTYPE.derive(properties);

this(NacosClientProperties.PROTOTYPE.derive(properties));
}

public NacosConfigService(NacosClientProperties clientProperties) throws NacosException {
ValidatorUtils.checkInitParam(clientProperties);

initNamespace(clientProperties);
this.configFilterChainManager = new ConfigFilterChainManager(clientProperties);
ServerListManager serverListManager = new ServerListManager(clientProperties);
serverListManager.start();

this.worker = new ClientWorker(this.configFilterChainManager, serverListManager, clientProperties);
// will be deleted in 2.0 later versions
agent = new ServerHttpAgent(serverListManager);

}

private void initNamespace(NacosClientProperties properties) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class Log4J2NacosLogging extends AbstractNacosLogging {

private static final String NACOS_LOGGER_PREFIX = "com.alibaba.nacos";

private static final String NACOS_LOG4J2_PLUGIN_PACKAGE = "com.alibaba.nacos.client.logging.log4j2";

private final String location = getLocation(NACOS_LOG4J2_LOCATION);

@Override
Expand All @@ -59,6 +61,7 @@ public void loadConfiguration() {

// load and start nacos configuration
Configuration configuration = loadConfiguration(loggerContext, location);
configuration.getPluginPackages().add(NACOS_LOG4J2_PLUGIN_PACKAGE);
configuration.start();

// append loggers and appenders to contextConfiguration
Expand Down
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);
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.event.SaxEvent;
import ch.qos.logback.core.joran.spi.ElementSelector;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.joran.spi.RuleStore;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -43,6 +45,12 @@ public class NacosJoranConfigurator extends JoranConfigurator {
public void registerSafeConfiguration(List<SaxEvent> eventList) {
}

@Override
public void addInstanceRules(RuleStore rs) {
super.addInstanceRules(rs);
rs.addRule(new ElementSelector("configuration/nacosClientProperty"), new NacosClientPropertyAction());
}

/**
* ensure that Nacos configuration does not affect user configuration scanning url.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,11 @@ public NacosNamingMaintainService(String serverList) throws NacosException {
}

public NacosNamingMaintainService(Properties properties) throws NacosException {
NacosClientProperties clientProperties = NacosClientProperties.PROTOTYPE.derive(properties);
init(clientProperties);
this(NacosClientProperties.PROTOTYPE.derive(properties));
}

public NacosNamingMaintainService(NacosClientProperties properties) throws NacosException {
init(properties);
}

private void init(NacosClientProperties properties) throws NacosException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,11 @@ public NacosNamingService(String serverList) throws NacosException {
}

public NacosNamingService(Properties properties) throws NacosException {
final NacosClientProperties clientProperties = NacosClientProperties.PROTOTYPE.derive(properties);
init(clientProperties);
this(NacosClientProperties.PROTOTYPE.derive(properties));
}

public NacosNamingService(NacosClientProperties properties) throws NacosException {
init(properties);
}

private void init(NacosClientProperties properties) throws NacosException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.alibaba.nacos.client.naming.remote.http;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.client.env.NacosClientProperties;
import com.alibaba.nacos.common.http.AbstractHttpClientFactory;
import com.alibaba.nacos.common.http.HttpClientBeanHolder;
import com.alibaba.nacos.common.http.HttpClientConfig;
Expand All @@ -38,12 +39,12 @@
*/
public class NamingHttpClientManager implements Closeable {

private static final int READ_TIME_OUT_MILLIS = Integer
.getInteger("com.alibaba.nacos.client.naming.rtimeout", 50000);
private static final int READ_TIME_OUT_MILLIS = NacosClientProperties.PROTOTYPE
.getInteger("com.alibaba.nacos.client.naming.rtimeout");

private static final int CON_TIME_OUT_MILLIS = Integer.getInteger("com.alibaba.nacos.client.naming.ctimeout", 3000);
private static final int CON_TIME_OUT_MILLIS = NacosClientProperties.PROTOTYPE.getInteger("com.alibaba.nacos.client.naming.ctimeout");

private static final boolean ENABLE_HTTPS = Boolean.getBoolean(TlsSystemConfig.TLS_ENABLE);
private static final boolean ENABLE_HTTPS = NacosClientProperties.PROTOTYPE.getBoolean(TlsSystemConfig.TLS_ENABLE);

private static final int MAX_REDIRECTS = 5;

Expand Down
44 changes: 22 additions & 22 deletions client/src/main/resources/nacos-log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,82 +17,82 @@

<Configuration status="WARN">
<Appenders>
<RollingFile name="CONFIG_LOG_FILE" fileName="${sys:JM.LOG.PATH}/nacos/config.log"
filePattern="${sys:JM.LOG.PATH}/nacos/config.log.%d{yyyy-MM-dd}.%i">
<RollingFile name="CONFIG_LOG_FILE" fileName="${nacosClientProperty:JM.LOG.PATH}/nacos/config.log"
filePattern="${nacosClientProperty:JM.LOG.PATH}/nacos/config.log.%d{yyyy-MM-dd}.%i">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %p [%-5t:%c{2}] %m%n</Pattern>
</PatternLayout>

<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="${sys:JM.LOG.FILE.SIZE:-10MB}"/>
<SizeBasedTriggeringPolicy size="${nacosClientProperty:JM.LOG.FILE.SIZE:-10MB}"/>
</Policies>

<DefaultRolloverStrategy max="${sys:JM.LOG.RETAIN.COUNT:-7}">
<Delete basePath="${sys:JM.LOG.PATH}/nacos" maxDepth="1" testMode="${sys:JM.LOG.RETAIN.DURATION.OFF:-true}">
<DefaultRolloverStrategy max="${nacosClientProperty:JM.LOG.RETAIN.COUNT:-7}">
<Delete basePath="${nacosClientProperty:JM.LOG.PATH}/nacos" maxDepth="1" testMode="${nacosClientProperty:JM.LOG.RETAIN.DURATION.OFF:-true}">
<IfFileName glob="config.log.*.*" />
<IfLastModified age="${sys:JM.LOG.RETAIN.DURATION:-P180D}" />
<IfLastModified age="${nacosClientProperty:JM.LOG.RETAIN.DURATION:-P180D}" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>

<RollingFile name="REMOTE_LOG_FILE" fileName="${sys:JM.LOG.PATH}/nacos/remote.log"
filePattern="${sys:JM.LOG.PATH}/nacos/remote.log.%d{yyyy-MM-dd}.%i">
<RollingFile name="REMOTE_LOG_FILE" fileName="${nacosClientProperty:JM.LOG.PATH}/nacos/remote.log"
filePattern="${nacosClientProperty:JM.LOG.PATH}/nacos/remote.log.%d{yyyy-MM-dd}.%i">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %p [%-5t:%c{2}] %m%n</Pattern>
</PatternLayout>

<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="${sys:JM.LOG.FILE.SIZE:-10MB}"/>
<SizeBasedTriggeringPolicy size="${nacosClientProperty:JM.LOG.FILE.SIZE:-10MB}"/>
</Policies>

<DefaultRolloverStrategy max="${sys:JM.LOG.RETAIN.COUNT:-7}">
<Delete basePath="${sys:JM.LOG.PATH}/nacos" maxDepth="1" testMode="${sys:JM.LOG.RETAIN.DURATION.OFF:-true}">
<DefaultRolloverStrategy max="${nacosClientProperty:JM.LOG.RETAIN.COUNT:-7}">
<Delete basePath="${nacosClientProperty:JM.LOG.PATH}/nacos" maxDepth="1" testMode="${nacosClientProperty:JM.LOG.RETAIN.DURATION.OFF:-true}">
<IfFileName glob="remote.log.*.*" />
<IfLastModified age="${sys:JM.LOG.RETAIN.DURATION:-P180D}" />
<IfLastModified age="${nacosClientProperty:JM.LOG.RETAIN.DURATION:-P180D}" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>

<RollingFile name="NAMING_LOG_FILE" fileName="${sys:JM.LOG.PATH}/nacos/naming.log"
filePattern="${sys:JM.LOG.PATH}/nacos/naming.log.%d{yyyy-MM-dd}.%i">
<RollingFile name="NAMING_LOG_FILE" fileName="${nacosClientProperty:JM.LOG.PATH}/nacos/naming.log"
filePattern="${nacosClientProperty:JM.LOG.PATH}/nacos/naming.log.%d{yyyy-MM-dd}.%i">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %p [%-5t:%c{2}] %m%n</Pattern>
</PatternLayout>

<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="${sys:JM.LOG.FILE.SIZE:-10MB}"/>
<SizeBasedTriggeringPolicy size="${nacosClientProperty:JM.LOG.FILE.SIZE:-10MB}"/>
</Policies>

<DefaultRolloverStrategy max="${sys:JM.LOG.RETAIN.COUNT:-7}">
<Delete basePath="${sys:JM.LOG.PATH}/nacos" maxDepth="1" testMode="${sys:JM.LOG.RETAIN.DURATION.OFF:-true}">
<DefaultRolloverStrategy max="${nacosClientProperty:JM.LOG.RETAIN.COUNT:-7}">
<Delete basePath="${nacosClientProperty:JM.LOG.PATH}/nacos" maxDepth="1" testMode="${nacosClientProperty:JM.LOG.RETAIN.DURATION.OFF:-true}">
<IfFileName glob="naming.log.*.*" />
<IfLastModified age="${sys:JM.LOG.RETAIN.DURATION:-P180D}" />
<IfLastModified age="${nacosClientProperty:JM.LOG.RETAIN.DURATION:-P180D}" />
</Delete>
</DefaultRolloverStrategy>

</RollingFile>
</Appenders>

<Loggers>
<Logger name="com.alibaba.nacos.client" level="${sys:com.alibaba.nacos.config.log.level:-info}"
<Logger name="com.alibaba.nacos.client" level="${nacosClientProperty:com.alibaba.nacos.config.log.level:-info}"
additivity="false">
<AppenderRef ref="CONFIG_LOG_FILE"/>
</Logger>

<Logger name="com.alibaba.nacos.common.remote.client" level="${sys:com.alibaba.nacos.config.log.level:-info}"
<Logger name="com.alibaba.nacos.common.remote.client" level="${nacosClientProperty:com.alibaba.nacos.config.log.level:-info}"
additivity="false">
<AppenderRef ref="REMOTE_LOG_FILE"/>
</Logger>

<Logger name="com.alibaba.nacos.client.config" level="${sys:com.alibaba.nacos.config.log.level:-info}"
<Logger name="com.alibaba.nacos.client.config" level="${nacosClientProperty:com.alibaba.nacos.config.log.level:-info}"
additivity="false">
<AppenderRef ref="CONFIG_LOG_FILE"/>
</Logger>

<Logger name="com.alibaba.nacos.client.naming" level="${sys:com.alibaba.nacos.naming.log.level:-info}"
<Logger name="com.alibaba.nacos.client.naming" level="${nacosClientProperty:com.alibaba.nacos.naming.log.level:-info}"
additivity="false">
<AppenderRef ref="NAMING_LOG_FILE"/>
</Logger>
Expand Down
Loading

0 comments on commit 6332bf4

Please sign in to comment.