Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE#6999] Solve the problem that Nacos client does not support logback overload log configuration #8229

Merged
merged 11 commits into from
Apr 27, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,21 @@

package com.alibaba.nacos.client.logging.logback;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.util.ContextInitializer;
import ch.qos.logback.classic.spi.LoggerContextListener;
import ch.qos.logback.core.CoreConstants;
import com.alibaba.nacos.client.logging.AbstractNacosLogging;
import com.alibaba.nacos.common.utils.ResourceUtils;
import com.alibaba.nacos.common.utils.StringUtils;
import org.slf4j.impl.StaticLoggerBinder;

/**
* Support for Logback version 1.0.8 or higher
*
* @author <a href="mailto:[email protected]">hxy1991</a>
* @author <a href="mailto:[email protected]">hujun</a>
*
* @since 0.9.0
*/
public class LogbackNacosLogging extends AbstractNacosLogging {
Expand All @@ -35,17 +39,52 @@ public class LogbackNacosLogging extends AbstractNacosLogging {

@Override
public void loadConfiguration() {
String location = getLocation(NACOS_LOGBACK_LOCATION);
if (StringUtils.isBlank(location)) {
return;
LoggerContext loggerContext = loadConfigurationOnStart();
if (loggerContext.getObject(CoreConstants.RECONFIGURE_ON_CHANGE_TASK) != null) {
addListener(loggerContext);
}

}

private LoggerContext loadConfigurationOnStart() {
String location = getLocation(NACOS_LOGBACK_LOCATION);
try {
LoggerContext loggerContext = (LoggerContext) StaticLoggerBinder.getSingleton().getLoggerFactory();
new ContextInitializer(loggerContext).configureByResource(ResourceUtils.getResourceUrl(location));
NacosJoranConfigurator configurator = new NacosJoranConfigurator();
configurator.setContext(loggerContext);
configurator.doNacosConfigure(ResourceUtils.getResourceUrl(location));
return loggerContext;
} catch (Exception e) {
throw new IllegalStateException("Could not initialize Logback Nacos logging from " + location, e);
}
}

private void addListener(LoggerContext loggerContext) {
loggerContext.addListener(new LoggerContextListener() {
@Override
public boolean isResetResistant() {
return true;
}

@Override
public void onReset(LoggerContext context) {
loadConfigurationOnStart();
}

@Override
public void onStart(LoggerContext context) {

}

@Override
public void onStop(LoggerContext context) {

}

@Override
public void onLevelChange(Logger logger, Level level) {

}
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 1999-2018 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.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.event.SaxEvent;
import ch.qos.logback.core.joran.spi.JoranException;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

/**
* ensure that Nacos configuration does not affect user configuration savepoints and scanning url.
*
* @author <a href="mailto:[email protected]">hujun</a>
* @see <a href="https://github.com/alibaba/nacos/issues/6999">#6999</a>
*/
public class NacosJoranConfigurator extends JoranConfigurator {

/**
* ensure that Nacos configuration does not affect user configuration savepoints.
*
* @param eventList safe data
*/
@Override
public void registerSafeConfiguration(List<SaxEvent> eventList) {
}

/**
* ensure that Nacos configuration does not affect user configuration scanning url.
*
* @param url config url
* @throws JoranException e
*/
public void doNacosConfigure(URL url) throws JoranException {
InputStream in = null;
try {
URLConnection urlConnection = url.openConnection();
urlConnection.setUseCaches(false);
in = urlConnection.getInputStream();
doConfigure(in, url.toExternalForm());
} catch (IOException ioe) {
String errMsg = "Could not open URL [" + url + "].";
addError(errMsg, ioe);
throw new JoranException(errMsg, ioe);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ioe) {
String errMsg = "Could not close input stream";
addError(errMsg, ioe);
throw new JoranException(errMsg, ioe);
}
}
}
}

}
3 changes: 1 addition & 2 deletions client/src/main/resources/nacos-logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
~ limitations under the License.
-->

<configuration debug="false" scan="true" scanPeriod="30 seconds" packagingData="true">
<contextName>nacos</contextName>
<configuration debug="false" scan="false" packagingData="true">

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