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

com.fastchar.slf4j.core.FastLog4j2XmlBuilder Maven / Gradle / Ivy

The newest version!
package com.fastchar.slf4j.core;

import com.fastchar.slf4j.FastLog4j2Config;
import com.fastchar.utils.FastStringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class FastLog4j2XmlBuilder {

    private final FastLog4j2Config config;

    public FastLog4j2XmlBuilder(FastLog4j2Config config) {
        this.config = config;
    }


    public String createConfigXml() {
        return "" +
                "" +
                "    " +
                "        " + config.getLogDirectory() + "" +
                "    " +
                "    " +
                buildAppenderXml() +
                "    " +
                "    " +
                buildLoggersXml() +
                "    " +
                "";
    }

    private String buildAppenderXml() {
        List appenderList = new ArrayList<>();
        Map loggerMap = config.getLoggers();

        for (Map.Entry entry : loggerMap.entrySet()) {
            FastLog4jLogger log4jLogger = entry.getValue();
            Map rollingFiles = log4jLogger.getRollingFiles();
            for (Map.Entry fileEntry : rollingFiles.entrySet()) {

                String configThresholdFilter = "  ";

                FastLog4jLevelEnum nextLevel = FastLog4jLevelEnum.getNextLevel(fileEntry.getKey());
                if (nextLevel != null) {
                    configThresholdFilter += "";
                }

                FastLog4jRollingFile rollingFile = fileEntry.getValue();
                String rollinFileXml = "" +
                        "    " +
                        configThresholdFilter +
                        "    " +
                        "    " +
                        "    " +
                        "        " +
                        "        " +
                        "    " +
                        "";
                appenderList.add(rollinFileXml);

                if (rollingFile.isConsole() && rollingFile.getConsole() != null) {
                    FastLog4jConsole console = rollingFile.getConsole();
                    String consoleAppender = "" +
                            "    " +
                            configThresholdFilter +
                            "    " +
                            "   " +
                            "";
                    appenderList.add(consoleAppender);
                }
            }


        }
        return FastStringUtils.join(appenderList, "");
    }




    private String buildLoggersXml() {
        List loggers = new ArrayList<>();

        Map loggerMap = config.getLoggers();
        for (Map.Entry entry : loggerMap.entrySet()) {
            FastLog4jLogger log4jLogger = entry.getValue();

            List appenderRefs = new ArrayList<>();
            for (Map.Entry rollingFileEntry : log4jLogger.getRollingFiles().entrySet()) {
                FastLog4jRollingFile rollingFile = rollingFileEntry.getValue();
                if (rollingFile.getConsole() != null) {
                    appenderRefs.add("");
                }
                appenderRefs.add("");
            }

            loggers.add("" + FastStringUtils.join(appenderRefs, "") + "");
        }

        return FastStringUtils.join(loggers, "");
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy