diff --git a/platform/platform-impl/src/com/intellij/idea/LoggerFactory.java b/platform/platform-impl/src/com/intellij/idea/LoggerFactory.java index 2e83f2b757e1..3b195720213e 100644 --- a/platform/platform-impl/src/com/intellij/idea/LoggerFactory.java +++ b/platform/platform-impl/src/com/intellij/idea/LoggerFactory.java @@ -1,11 +1,10 @@ -// 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. +// Copyright 2000-2020 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.*; @@ -18,24 +17,23 @@ import org.jdom.output.DOMOutputter; import org.jetbrains.annotations.NotNull; import org.w3c.dom.Element; -import java.io.File; import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; -@SuppressWarnings({"CallToPrintStackTrace", "UseOfSystemOutOrSystemErr"}) public class LoggerFactory implements Logger.Factory { private static final String SYSTEM_MACRO = "$SYSTEM_DIR$"; private static final String APPLICATION_MACRO = "$APPLICATION_DIR$"; private static final String LOG_DIR_MACRO = "$LOG_DIR$"; - private static final String DOCUMENT_BUILDER_FACTORY_KEY = "javax.xml.parsers.DocumentBuilderFactory"; - @SuppressWarnings("SpellCheckingInspection") - private static final String DOCUMENT_BUILDER_FACTORY_IMPL = "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl"; - LoggerFactory() { try { init(); } catch (Exception e) { + //noinspection CallToPrintStackTrace e.printStackTrace(); } } @@ -49,43 +47,43 @@ public class LoggerFactory implements Logger.Factory { private static void init() throws Exception { System.setProperty("log4j.defaultInitOverride", "true"); - File xmlFile = PathManager.getLogFile(); - if (xmlFile != null) { - loadFromXmlFile(xmlFile); - } - else { - configureProgrammatically(); + String configPath = System.getProperty(PathManager.PROPERTY_LOG_CONFIG_FILE); + if (configPath != null) { + Path configFile = Paths.get(configPath); + if (!configFile.isAbsolute()) { + configFile = Paths.get(PathManager.getBinPath()).resolve(configPath); // look from the 'bin/' directory where log.xml was used to be + } + if (Files.exists(configFile)) { + configureFromXmlFile(configFile); + return; + } } + + configureProgrammatically(); } - private static void loadFromXmlFile(File xmlFile) throws Exception { - String text = FileUtilRt.loadFile(xmlFile); + private static void configureFromXmlFile(Path xmlFile) throws Exception { + String text = new String(Files.readAllBytes(xmlFile), StandardCharsets.UTF_8); 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(), "\\", "\\\\")); - File file = new File(PathManager.getLogPath()); - if (!file.mkdirs() && !file.exists()) { - System.err.println("Cannot create log directory: " + file); - } - - // Jdom is used instead of XML DOM because of https://youtrack.jetbrains.com/issue/IDEA-173468 - // DOMConfigurator really wants Document - @SuppressWarnings("deprecation") - Document document = JDOMUtil.loadDocument(new CharSequenceReader(text)); + // JDOM is used instead of XML DOM because of IDEA-173468 (`DOMConfigurator` really wants `Document`) + @SuppressWarnings("deprecation") Document document = JDOMUtil.loadDocument(new CharSequenceReader(text)); Element element = new DOMOutputter(new JAXPDOMAdapter() { @Override public org.w3c.dom.Document createDocument() throws JDOMException { - String property = System.setProperty(DOCUMENT_BUILDER_FACTORY_KEY, DOCUMENT_BUILDER_FACTORY_IMPL); + String key = "javax.xml.parsers.DocumentBuilderFactory"; + @SuppressWarnings("SpellCheckingInspection") String property = System.setProperty(key, "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl"); try { return super.createDocument(); } finally { if (property == null) { - System.clearProperty(DOCUMENT_BUILDER_FACTORY_KEY); + System.clearProperty(key); } else { - System.setProperty(DOCUMENT_BUILDER_FACTORY_KEY, property); + System.setProperty(key, property); } } } @@ -100,8 +98,8 @@ public class LoggerFactory implements Logger.Factory { 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"); + RollingFileAppender ideaLog = new RollingFileAppender(layout, PathManager.getLogPath() + "/idea.log", true); + ideaLog.setEncoding(StandardCharsets.UTF_8.name()); ideaLog.setMaxBackupIndex(12); ideaLog.setMaximumFileSize(10_000_000); root.addAppender(ideaLog); @@ -119,4 +117,4 @@ public class LoggerFactory implements Logger.Factory { root.addAppender(appender); } -} +} \ No newline at end of file diff --git a/platform/util/src/com/intellij/openapi/application/PathManager.java b/platform/util/src/com/intellij/openapi/application/PathManager.java index 56e7bc3da009..e4f4464e0033 100644 --- a/platform/util/src/com/intellij/openapi/application/PathManager.java +++ b/platform/util/src/com/intellij/openapi/application/PathManager.java @@ -641,24 +641,4 @@ public class PathManager { return path; } } - - @Nullable - public static File getLogFile() { - String logXmlPath = System.getProperty(PROPERTY_LOG_CONFIG_FILE); - if (logXmlPath == null) return null; - - // If the property value specifies a file name rather than a path, look for the - // specified file in the bin directory (where log.xml was previously stored) - File logXmlFile; - if (logXmlPath.indexOf('/') < 0 && logXmlPath.indexOf('\\') < 0) { - logXmlFile = new File(getBinPath(), logXmlPath); - } - else { - logXmlFile = new File(logXmlPath); - } - - if (logXmlFile.exists()) return logXmlFile; - - return null; - } } \ No newline at end of file