All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.sdk.log.env.LogEnvUtil Maven / Gradle / Ivy

package org.zodiac.sdk.log.env;

//import static org.zodiac.sdk.log.constants.LogConstants.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;

import org.zodiac.sdk.log.CommonLoggingConfigurations;
import org.zodiac.sdk.log.constants.LogConstants;
import org.zodiac.sdk.log.util.ReportUtil;
import org.zodiac.sdk.toolkit.util.ClassUtil;
import org.zodiac.sdk.toolkit.util.JvmToolUtil;
import org.zodiac.sdk.toolkit.util.lang.StrUtil;

public final class LogEnvUtil {

    private static volatile Map globalSystemProperties;

    private static volatile boolean useDefaultSystemProperties;

    private LogEnvUtil() {}

    public static boolean isLogbackUsable(ClassLoader spaceClassloader) {
        Objects.requireNonNull(spaceClassloader);
        try {
            return spaceClassloader.loadClass("ch.qos.logback.classic.LoggerContext") != null;
        } catch (ClassNotFoundException e) {
            return false;
        }
    }

    public static boolean isLog4j2Usable(ClassLoader spaceClassloader) {
        Objects.requireNonNull(spaceClassloader);
        try {
            return (spaceClassloader.loadClass("org.apache.logging.slf4j.Log4jLoggerFactory") != null);
        } catch (ClassNotFoundException e) {
            return false;
        }
    }

    public static boolean isLog4jUsable(ClassLoader spaceClassloader) {
        Objects.requireNonNull(spaceClassloader);
        try {
            return (spaceClassloader.loadClass("org.slf4j.impl.Log4jLoggerFactory") != null);
        } catch (ClassNotFoundException e) {
            // logger.debug("log4j dependency is not existed.");
            return false;
        }
    }

    public static boolean isCommonsLoggingUsable(ClassLoader spaceClassloader) {
        Objects.requireNonNull(spaceClassloader);
        try {
            return (spaceClassloader.loadClass("org.slf4j.impl.JCLLoggerAdapter") != null
                && (spaceClassloader.loadClass("org.apache.commons.logging.impl.Log4JLogger") != null)
                && (spaceClassloader.loadClass("org.apache.log4j.Logger") != null)
                && (spaceClassloader.loadClass("org.apache.commons.logging.Log") != null));
        } catch (ClassNotFoundException e) {
            // logger.debug("log4j dependency is not existed.");
            return false;
        }
    }

    /**
     * Processing global configuration for "Platform-Log", priorities as follows:
     * 
*
    *
  1. JVM System Properties.
  2. *
  3. OS Env Properties.
  4. *
  5. External Configurations.
  6. *
*
* * Some configurations specific to this Platform: *
*
    *
  1. logging.path.
  2. *
  3. loggingRoot.
  4. *
  5. file.encoding, defaults to "UTF-8".
  6. *
  7. PID.
  8. *
*
* * @return Processed global configuration */ public static Map processGlobalSystemLogProperties() { if (globalSystemProperties != null) { return globalSystemProperties; } /*Firstly, Load from external configurations.*/ Map properties = new HashMap<>(CommonLoggingConfigurations.getExternalConfigurations()); /*Secondly, process configurations from OS env.*/ for (Map.Entry entry : System.getenv().entrySet()) { String lowerCaseKey = entry.getKey().toLowerCase(); if (isPlatformCommonLoggingPrefix(lowerCaseKey)) { properties.put(lowerCaseKey, entry.getValue()); } } /*Thirdly, process configurations from JVM System Properties.*/ final Properties systemProperties = System.getProperties(); final Set sysKeys = systemProperties.stringPropertyNames(); for (String key : sysKeys) { String value = systemProperties.getProperty(key); if (key == null || value == null) { continue; } String lowerCaseKey = key.toLowerCase(); if (isPlatformCommonLoggingPrefix(lowerCaseKey)) { properties.put(lowerCaseKey, value); } } properties.put(LogConstants.PROCESS_MARKER, JvmToolUtil.pid()); if (System.getProperties().contains(LogConstants.LOG_ENCODING_PROP_KEY)) { properties.put(LogConstants.LOG_ENCODING_PROP_KEY, System.getProperty(LogConstants.LOG_ENCODING_PROP_KEY)); } else { properties.putIfAbsent(LogConstants.LOG_ENCODING_PROP_KEY, LogConstants.UTF8_STR); } /*"logging.path" has priority over loggingRoot.*/ String loggingPath = System.getProperty(LogConstants.LOG_PATH); String loggingRoot = System.getProperty(LogConstants.OLD_LOG_PATH); if (StrUtil.isNotBlank(loggingPath)) { loggingRoot = loggingPath; } else if (StrUtil.isNotBlank(loggingRoot)) { loggingPath = loggingRoot; } if (StrUtil.isNotEmpty(loggingPath)) { properties.put(LogConstants.LOG_PATH, loggingPath); properties.put(LogConstants.OLD_LOG_PATH, loggingRoot); useDefaultSystemProperties = false; } else { /*Defaults to $HOME/logs.*/ properties.putIfAbsent(LogConstants.LOG_PATH, LogConstants.LOGGING_PATH_DEFAULT); properties.putIfAbsent(LogConstants.OLD_LOG_PATH, LogConstants.LOGGING_PATH_DEFAULT); useDefaultSystemProperties = true; } globalSystemProperties = properties; keepCompatible(globalSystemProperties, !isLogStarterExist()); return globalSystemProperties; } public static String getLogConfEnvSuffix(String spaceName) { String logEnvConfig = System.getProperty(LogConstants.LOG_ENV_SUFFIX, ""); String[] spaceNameToEnvSuffix = logEnvConfig.split("&"); String suffix = null; for (int i = 0; i < spaceNameToEnvSuffix.length && suffix == null; ++i) { String envConf = spaceNameToEnvSuffix[i]; String[] conf = envConf.split(":"); /*配置格式错误或者不是相同的 spaceName, 直接 skip。*/ if (conf.length != 2 || !conf[0].equals(spaceName)) { continue; } else if (!conf[1].isEmpty()) { /* .dev .test .product */ conf[1] = "." + conf[1]; } suffix = conf[1]; } suffix = (suffix == null) ? "" : suffix; if (!suffix.isEmpty()) { ReportUtil.reportDebug(spaceName + " log configuration: " + LogConstants.LOG_XML_CONFIG_FILE_NAME + suffix); } return suffix; } /** * Keep compatible with previous version. Set system properties of the following attributes. *
*
    *
  • logging.path
  • *
  • loggingRoot
  • *
  • file.encoding
  • *
*
* * @param context Properties * @param keep Keep compatible or don't */ public static void keepCompatible(Map context, boolean keep) { if (!keep) { return; } String loggingPath = System.getProperty(LogConstants.LOG_PATH, context.get(LogConstants.LOG_PATH)); String fileEncoding = System.getProperty(LogConstants.LOG_ENCODING_PROP_KEY, context.get(LogConstants.LOG_ENCODING_PROP_KEY)); System.setProperty(LogConstants.LOG_PATH, loggingPath); System.setProperty(LogConstants.OLD_LOG_PATH, System.getProperty(LogConstants.OLD_LOG_PATH, loggingPath)); System.setProperty(LogConstants.LOG_ENCODING_PROP_KEY, fileEncoding); } public static boolean isLogStarterExist() { return ClassUtil.isPresent1("org.zodiac.core.logging.Mark"); } private static boolean isPlatformCommonLoggingPrefix(String key) { return key.startsWith(LogConstants.PLATFORM_CONFIG_PREFIX) || key.startsWith(LogConstants.LOG_LEVEL_PREFIX) || key.startsWith(LogConstants.LOG_PATH_PREFIX) || key.startsWith(LogConstants.LOG_CONFIG_PREFIX); } public static boolean isPlatformCommonLoggingConfig(String key) { return isPlatformCommonLoggingPrefix(key) || key.equals(LogConstants.LOG_PATH) || key.equals(LogConstants.OLD_LOG_PATH) || key.equals(LogConstants.LOG_ENCODING_PROP_KEY); } public static void clearGlobalSystemProperties() { globalSystemProperties = null; } public static boolean isUseDefaultSystemProperties() { return useDefaultSystemProperties; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy