001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.util;
029
030import java.util.ArrayList;
031import java.util.List;
032import java.util.Map;
033import java.util.TreeMap;
034
035import org.apache.logging.log4j.LogManager;
036import org.apache.logging.log4j.core.Logger;
037import org.apache.logging.log4j.core.LoggerContext;
038
039/**
040 * Utilities for dealing with log4j loggers.<p>
041 */
042public final class CmsLog4jUtil {
043
044    /**
045     * Hidden default constructor.<P>
046     */
047    private CmsLog4jUtil() {
048        // do nothing
049    }
050
051    /**
052     * Gets the list of all loggers.<p>
053     *
054     * @return the list of all loggers
055     */
056    @SuppressWarnings("resource")
057    public static List<Logger> getAllLoggers() {
058
059        LoggerContext context = (LoggerContext)(LogManager.getContext(false));
060        Map<String, Logger> loggersByName = new TreeMap<String, Logger>();
061        for (Logger logger : context.getLoggers()) {
062            String loggerName = logger.getName();
063            while (loggerName != null) {
064                if (!loggersByName.containsKey(loggerName)) {
065                    Logger currentLogger = (Logger)(LogManager.getLogger(loggerName));
066                    loggersByName.put(loggerName, currentLogger);
067                }
068                loggerName = getParentLoggerName(loggerName);
069
070            }
071        }
072        return new ArrayList<Logger>(loggersByName.values());
073
074    }
075
076    /**
077     * Gets the parent logger name for a given logger name, or null if there is no parent logger name.<p>
078     *
079     * @param loggerName the name of a logger
080     * @return the parent name of the logger, or null if the logger name has no parent
081     */
082    public static String getParentLoggerName(String loggerName) {
083
084        int dotIndex = loggerName.lastIndexOf(".");
085        if (dotIndex < 0) {
086            return null;
087        } else {
088            return loggerName.substring(0, dotIndex);
089        }
090
091    }
092
093}