setup log4j programmatically

GitOrigin-RevId: e8fd62789921268975e9be6a7a8fd708f2bc6c6a
This commit is contained in:
Sergey Ignatov
2019-05-29 16:08:06 +03:00
committed by intellij-monorepo-bot
parent f22b792b2d
commit b8fdc6b81e
3 changed files with 53 additions and 18 deletions
@@ -1,13 +1,15 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.idea;
import com.intellij.diagnostic.DialogAppender;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.JDOMUtil;
import com.intellij.openapi.util.io.FileUtilRt;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.text.CharSequenceReader;
import org.apache.log4j.LogManager;
import org.apache.log4j.*;
import org.apache.log4j.varia.LevelRangeFilter;
import org.apache.log4j.xml.DOMConfigurator;
import org.jdom.Document;
import org.jdom.JDOMException;
@@ -44,10 +46,20 @@ public class LoggerFactory implements Logger.Factory {
return new IdeaLogger(org.apache.log4j.Logger.getLogger(name));
}
private static void init() throws IOException, JDOMException {
private static void init() throws Exception {
System.setProperty("log4j.defaultInitOverride", "true");
String text = FileUtilRt.loadFile(PathManager.getLogFile());
File xmlFile = PathManager.getLogFile();
if (xmlFile != null) {
loadFromXmlFile(xmlFile);
}
else {
configureProgrammatically();
}
}
private static void loadFromXmlFile(File xmlFile) throws Exception {
String text = FileUtilRt.loadFile(xmlFile);
text = StringUtil.replace(text, SYSTEM_MACRO, StringUtil.replace(PathManager.getSystemPath(), "\\", "\\\\"));
text = StringUtil.replace(text, APPLICATION_MACRO, StringUtil.replace(PathManager.getHomePath(), "\\", "\\\\"));
text = StringUtil.replace(text, LOG_DIR_MACRO, StringUtil.replace(PathManager.getLogPath(), "\\", "\\\\"));
@@ -80,4 +92,31 @@ public class LoggerFactory implements Logger.Factory {
}, null, null).output(document).getDocumentElement();
new DOMConfigurator().doConfigure(element, LogManager.getLoggerRepository());
}
private static void configureProgrammatically() throws IOException {
org.apache.log4j.Logger root = LogManager.getRootLogger();
root.removeAllAppenders();
root.setLevel(Level.INFO);
PatternLayout layout = new PatternLayout("%d [%7r] %6p - %30.30c - %m \n");
RollingFileAppender ideaLog = new RollingFileAppender(layout, PathManager.getLogPath() + "/" + "idea.log", true);
ideaLog.setEncoding("UTF-8");
ideaLog.setMaxBackupIndex(12);
ideaLog.setMaximumFileSize(10_000_000);
root.addAppender(ideaLog);
ConsoleAppender consoleWarn = new ConsoleAppender(layout, ConsoleAppender.SYSTEM_ERR);
LevelRangeFilter warnFilter = new LevelRangeFilter();
warnFilter.setLevelMin(Level.WARN);
consoleWarn.addFilter(warnFilter);
root.addAppender(consoleWarn);
DialogAppender appender = new DialogAppender();
LevelRangeFilter filter = new LevelRangeFilter();
filter.setLevelMin(Level.INFO);
appender.addFilter(filter);
root.addAppender(appender);
}
}
@@ -56,9 +56,7 @@ public class TestLoggerFactory implements Logger.Factory {
public static boolean reconfigure() {
try {
File logXmlFile = new File(PathManager.getHomePath(), "test-log.xml");
if (!logXmlFile.exists()) {
logXmlFile = new File(PathManager.getBinPath(), "log.xml");
}
if (!logXmlFile.exists()) {
return false;
}
@@ -38,7 +38,7 @@ public class PathManager {
public static final String PROPERTY_SCRATCH_PATH = "idea.scratch.path";
public static final String PROPERTY_PLUGINS_PATH = "idea.plugins.path";
public static final String PROPERTY_LOG_PATH = "idea.log.path";
public static final String PROPERTY_GUI_TEST_LOG_FILE = "idea.gui.tests.log.file";
public static final String PROPERTY_LOG_CONFIG_FILE = "idea.log.config.file";
public static final String PROPERTY_PATHS_SELECTOR = "idea.paths.selector";
public static final String OPTIONS_DIRECTORY = "options";
@@ -616,16 +616,14 @@ public class PathManager {
}
}
@NotNull
public static File getLogFile() throws FileNotFoundException {
String logXmlPath = System.getProperty(PROPERTY_GUI_TEST_LOG_FILE);
if (logXmlPath != null) {
File logXmlFile = new File(logXmlPath);
if (logXmlFile.exists()) {
return logXmlFile;
}
throw new FileNotFoundException(String.format("'%s' not found.", logXmlPath));
}
return findBinFileWithException("log.xml");
@Nullable
public static File getLogFile() {
String logXmlPath = System.getProperty(PROPERTY_LOG_CONFIG_FILE);
if (logXmlPath == null) return null;
File logXmlFile = new File(logXmlPath);
if (logXmlFile.exists()) return logXmlFile;
return null;
}
}