Skip to content

Commit

Permalink
[ISSUE alibaba#8622] sub pr of 9356 - logging module (alibaba#9450)
Browse files Browse the repository at this point in the history
* [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
onewe authored Nov 4, 2022
1 parent e8d7135 commit 46e6318
Show file tree
Hide file tree
Showing 9 changed files with 309 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.alibaba.nacos.client.logging;

import com.alibaba.nacos.client.constant.Constants;
import com.alibaba.nacos.client.env.NacosClientProperties;
import com.alibaba.nacos.common.utils.ConvertUtils;
import com.alibaba.nacos.common.utils.StringUtils;

Expand All @@ -37,15 +38,15 @@ public abstract class AbstractNacosLogging {
private static final String NACOS_LOGGING_PATH_DIR = "logs";

static {
String loggingPath = System.getProperty(Constants.SysEnv.JM_LOG_PATH);
String loggingPath = NacosClientProperties.PROTOTYPE.getProperty(Constants.SysEnv.JM_LOG_PATH);
if (StringUtils.isBlank(loggingPath)) {
String userHome = System.getProperty(Constants.SysEnv.USER_HOME);
System.setProperty(Constants.SysEnv.JM_LOG_PATH, userHome + File.separator + NACOS_LOGGING_PATH_DIR);
String userHome = NacosClientProperties.PROTOTYPE.getProperty(Constants.SysEnv.USER_HOME);
NacosClientProperties.PROTOTYPE.setProperty(Constants.SysEnv.JM_LOG_PATH, userHome + File.separator + NACOS_LOGGING_PATH_DIR);
}
}

protected String getLocation(String defaultLocation) {
String location = System.getProperty(NACOS_LOGGING_CONFIG_PROPERTY);
String location = NacosClientProperties.PROTOTYPE.getProperty(NACOS_LOGGING_CONFIG_PROPERTY);
if (StringUtils.isBlank(location)) {
if (isDefaultConfigEnabled()) {
return defaultLocation;
Expand All @@ -56,7 +57,7 @@ protected String getLocation(String defaultLocation) {
}

private boolean isDefaultConfigEnabled() {
String property = System.getProperty(NACOS_LOGGING_DEFAULT_CONFIG_ENABLED_PROPERTY);
String property = NacosClientProperties.PROTOTYPE.getProperty(NACOS_LOGGING_DEFAULT_CONFIG_ENABLED_PROPERTY);
// The default value is true.
return property == null || ConvertUtils.toBoolean(property);
}
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
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
39 changes: 22 additions & 17 deletions client/src/main/resources/nacos-logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,22 @@
-->

<configuration debug="false" scan="false" packagingData="true">
<nacosClientProperty scope="context" name="logPath" source="JM.LOG.PATH"/>
<nacosClientProperty scope="context" name="logRetainCount" source="JM.LOG.RETAIN.COUNT" defaultValue="7"/>
<nacosClientProperty scope="context" name="logFileSize" source="JM.LOG.FILE.SIZE" defaultValue="10MB"/>
<nacosClientProperty scope="context" name="nacosConfigLogLevel" source="com.alibaba.nacos.config.log.level" defaultValue="info"/>


<appender name="CONFIG_LOG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${JM.LOG.PATH}/nacos/config.log</file>
<file>${logPath}/nacos/config.log</file>

<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>${JM.LOG.PATH}/nacos/config.log.%i</fileNamePattern>
<maxIndex>${JM.LOG.RETAIN.COUNT:-7}</maxIndex>
<fileNamePattern>${logPath}/nacos/config.log.%i</fileNamePattern>
<maxIndex>${logRetainCount}</maxIndex>
</rollingPolicy>

<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>${JM.LOG.FILE.SIZE:-10MB}</maxFileSize>
<maxFileSize>${logFileSize}</maxFileSize>
</triggeringPolicy>

<encoder>
Expand All @@ -35,15 +40,15 @@
</appender>

<appender name="NAMING_LOG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${JM.LOG.PATH}/nacos/naming.log</file>
<file>${logPath}/nacos/naming.log</file>

<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>${JM.LOG.PATH}/nacos/naming.log.%i</fileNamePattern>
<maxIndex>${JM.LOG.RETAIN.COUNT:-7}</maxIndex>
<fileNamePattern>${logPath}/nacos/naming.log.%i</fileNamePattern>
<maxIndex>${logRetainCount}</maxIndex>
</rollingPolicy>

<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>${JM.LOG.FILE.SIZE:-10MB}</maxFileSize>
<maxFileSize>${logFileSize}</maxFileSize>
</triggeringPolicy>

<encoder>
Expand All @@ -52,15 +57,15 @@
</appender>

<appender name="REMOTE_LOG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${JM.LOG.PATH}/nacos/remote.log</file>
<file>${logPath}/nacos/remote.log</file>

<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>${JM.LOG.PATH}/nacos/remote.log.%i</fileNamePattern>
<maxIndex>${JM.LOG.RETAIN.COUNT:-7}</maxIndex>
<fileNamePattern>${logPath}/nacos/remote.log.%i</fileNamePattern>
<maxIndex>${logRetainCount}</maxIndex>
</rollingPolicy>

<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>${JM.LOG.FILE.SIZE:-10MB}</maxFileSize>
<maxFileSize>${logFileSize}</maxFileSize>
</triggeringPolicy>

<encoder>
Expand All @@ -69,28 +74,28 @@
</appender>


<logger name="com.alibaba.nacos.client" level="${com.alibaba.nacos.config.log.level:-info}"
<logger name="com.alibaba.nacos.client" level="${nacosConfigLogLevel}"
additivity="false">
<appender-ref ref="CONFIG_LOG_FILE"/>
</logger>


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

<Logger name="com.alibaba.nacos.shaded.io.grpc" level="${com.alibaba.nacos.log.level:-info}"
<Logger name="com.alibaba.nacos.shaded.io.grpc" level="${nacosConfigLogLevel}"
additivity="false">
<appender-ref ref="REMOTE_LOG_FILE"/>
</Logger>

<logger name="com.alibaba.nacos.client.config" level="${com.alibaba.nacos.config.log.level:-info}"
<logger name="com.alibaba.nacos.client.config" level="${nacosConfigLogLevel}"
additivity="false">
<appender-ref ref="CONFIG_LOG_FILE"/>
</logger>

<logger name="com.alibaba.nacos.client.naming" level="${com.alibaba.nacos.naming.log.level:-info}"
<logger name="com.alibaba.nacos.client.naming" level="${nacosConfigLogLevel}"
additivity="false">
<appender-ref ref="NAMING_LOG_FILE"/>
</logger>
Expand Down
Loading

0 comments on commit 46e6318

Please sign in to comment.