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 049 // do nothing 050 } 051 052 /** 053 * Gets the list of all loggers.<p> 054 * 055 * @return the list of all loggers 056 */ 057 @SuppressWarnings("resource") 058 public static List<Logger> getAllLoggers() { 059 060 LoggerContext context = (LoggerContext)(LogManager.getContext(false)); 061 Map<String, Logger> loggersByName = new TreeMap<String, Logger>(); 062 for (Logger logger : context.getLoggers()) { 063 String loggerName = logger.getName(); 064 while (loggerName != null) { 065 if (!loggersByName.containsKey(loggerName)) { 066 Logger currentLogger = (Logger)(LogManager.getLogger(loggerName)); 067 loggersByName.put(loggerName, currentLogger); 068 } 069 loggerName = getParentLoggerName(loggerName); 070 071 } 072 } 073 return new ArrayList<Logger>(loggersByName.values()); 074 075 } 076 077 /** 078 * Gets the parent logger name for a given logger name, or null if there is no parent logger name.<p> 079 * 080 * @param loggerName the name of a logger 081 * @return the parent name of the logger, or null if the logger name has no parent 082 */ 083 public static String getParentLoggerName(String loggerName) { 084 085 int dotIndex = loggerName.lastIndexOf("."); 086 if (dotIndex < 0) { 087 return null; 088 } else { 089 return loggerName.substring(0, dotIndex); 090 } 091 092 } 093 094}