Skip to content

Commit

Permalink
[Enhancement](log) Improve Safety and Robustness of Log4j Configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
zy-kkk committed Oct 9, 2023
1 parent f8e4cef commit 3bdaf8c
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions fe/fe-core/src/main/java/org/apache/doris/common/Log4jConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Map;

//
Expand Down Expand Up @@ -228,12 +229,20 @@ private static void reconfig() throws IOException {
SpringLog4j2Config.writeSpringLogConf(customConfDir);

// new SimpleLog4jConfiguration with xmlConfTemplate
ByteArrayInputStream bis = new ByteArrayInputStream(newXmlConfTemplate.getBytes("UTF-8"));
ConfigurationSource source = new ConfigurationSource(bis);
Log4jConfig config = new Log4jConfig(source);
if (newXmlConfTemplate == null || newXmlConfTemplate.isEmpty()) {
throw new IOException("The configuration template is empty!");
}

Log4jConfig config;
try (ByteArrayInputStream bis = new ByteArrayInputStream(newXmlConfTemplate.getBytes(StandardCharsets.UTF_8))) {
ConfigurationSource source = new ConfigurationSource(bis);
config = new Log4jConfig(source);

LoggerContext context = (LoggerContext) LogManager.getContext(false);
context.start(config);
LoggerContext context = (LoggerContext) LogManager.getContext(LogManager.class.getClassLoader(), false);
context.start(config);
} catch (Exception e) {
throw new IOException("Error occurred while configuring Log4j", e);
}
}

public static String getLogXmlConfTemplate() {
Expand Down

0 comments on commit 3bdaf8c

Please sign in to comment.