001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017package org.apache.log4j; 018 019import java.util.Enumeration; 020import java.util.HashMap; 021import java.util.Map; 022 023import org.apache.log4j.helpers.NullEnumeration; 024import org.apache.log4j.legacy.core.ContextUtil; 025import org.apache.log4j.or.ObjectRenderer; 026import org.apache.log4j.or.RendererSupport; 027import org.apache.log4j.spi.HierarchyEventListener; 028import org.apache.log4j.spi.LoggerFactory; 029import org.apache.log4j.spi.LoggerRepository; 030import org.apache.log4j.spi.RepositorySelector; 031import org.apache.logging.log4j.spi.LoggerContext; 032import org.apache.logging.log4j.util.Strings; 033 034/** 035 * 036 */ 037public final class LogManager { 038 039 /** 040 * @deprecated This variable is for internal use only. It will 041 * become package protected in future versions. 042 * */ 043 @Deprecated 044 public static final String DEFAULT_CONFIGURATION_FILE = "log4j.properties"; 045 046 /** 047 * @deprecated This variable is for internal use only. It will 048 * become private in future versions. 049 * */ 050 @Deprecated 051 public static final String DEFAULT_CONFIGURATION_KEY = "log4j.configuration"; 052 053 /** 054 * @deprecated This variable is for internal use only. It will 055 * become private in future versions. 056 * */ 057 @Deprecated 058 public static final String CONFIGURATOR_CLASS_KEY = "log4j.configuratorClass"; 059 060 /** 061 * @deprecated This variable is for internal use only. It will 062 * become private in future versions. 063 */ 064 @Deprecated 065 public static final String DEFAULT_INIT_OVERRIDE_KEY = "log4j.defaultInitOverride"; 066 067 static final String DEFAULT_XML_CONFIGURATION_FILE = "log4j.xml"; 068 069 private static final LoggerRepository REPOSITORY = new Repository(); 070 071 private static final boolean isLog4jCore; 072 073 static { 074 boolean core = false; 075 try { 076 if (Class.forName("org.apache.logging.log4j.core.LoggerContext") != null) { 077 core = true; 078 } 079 } catch (Exception ex) { 080 // Ignore the exception; 081 } 082 isLog4jCore = core; 083 } 084 085 private LogManager() { 086 } 087 088 public static Logger getRootLogger() { 089 return Category.getInstance(PrivateManager.getContext(), Strings.EMPTY); 090 } 091 092 public static Logger getLogger(final String name) { 093 return Category.getInstance(PrivateManager.getContext(), name); 094 } 095 096 public static Logger getLogger(final Class<?> clazz) { 097 return Category.getInstance(PrivateManager.getContext(), clazz.getName()); 098 } 099 100 public static Logger getLogger(final String name, final LoggerFactory factory) { 101 return Category.getInstance(PrivateManager.getContext(), name); 102 } 103 104 public static Logger exists(final String name) { 105 final LoggerContext ctx = PrivateManager.getContext(); 106 if (!ctx.hasLogger(name)) { 107 return null; 108 } 109 return Logger.getLogger(name); 110 } 111 112 @SuppressWarnings("rawtypes") 113 public static Enumeration getCurrentLoggers() { 114 return NullEnumeration.getInstance(); 115 } 116 117 static void reconfigure() { 118 if (isLog4jCore) { 119 final LoggerContext ctx = PrivateManager.getContext(); 120 ContextUtil.reconfigure(ctx); 121 } 122 } 123 124 /** 125 * No-op implementation. 126 */ 127 public static void shutdown() { 128 } 129 130 /** 131 * No-op implementation. 132 */ 133 public static void resetConfiguration() { 134 } 135 136 /** 137 * No-op implementation. 138 * @param selector The RepositorySelector. 139 * @param guard prevents calls at the incorrect time. 140 * @throws IllegalArgumentException if a parameter is invalid. 141 */ 142 public static void setRepositorySelector(final RepositorySelector selector, final Object guard) 143 throws IllegalArgumentException { 144 } 145 146 public static LoggerRepository getLoggerRepository() { 147 return REPOSITORY; 148 } 149 150 /** 151 * The Repository. 152 */ 153 private static class Repository implements LoggerRepository, RendererSupport { 154 155 private final Map<Class<?>, ObjectRenderer> rendererMap = new HashMap<>(); 156 157 @Override 158 public Map<Class<?>, ObjectRenderer> getRendererMap() { 159 return rendererMap; 160 } 161 162 @Override 163 public void addHierarchyEventListener(final HierarchyEventListener listener) { 164 165 } 166 167 @Override 168 public boolean isDisabled(final int level) { 169 return false; 170 } 171 172 @Override 173 public void setThreshold(final Level level) { 174 175 } 176 177 @Override 178 public void setThreshold(final String val) { 179 180 } 181 182 @Override 183 public void emitNoAppenderWarning(final Category cat) { 184 185 } 186 187 @Override 188 public Level getThreshold() { 189 return Level.OFF; 190 } 191 192 @Override 193 public Logger getLogger(final String name) { 194 return Category.getInstance(PrivateManager.getContext(), name); 195 } 196 197 @Override 198 public Logger getLogger(final String name, final LoggerFactory factory) { 199 return Category.getInstance(PrivateManager.getContext(), name); 200 } 201 202 @Override 203 public Logger getRootLogger() { 204 return Category.getRoot(PrivateManager.getContext()); 205 } 206 207 @Override 208 public Logger exists(final String name) { 209 return LogManager.exists(name); 210 } 211 212 @Override 213 public void shutdown() { 214 } 215 216 @Override 217 @SuppressWarnings("rawtypes") 218 public Enumeration getCurrentLoggers() { 219 return NullEnumeration.getInstance(); 220 } 221 222 @Override 223 @SuppressWarnings("rawtypes") 224 public Enumeration getCurrentCategories() { 225 return NullEnumeration.getInstance(); 226 } 227 228 @Override 229 public void fireAddAppenderEvent(final Category logger, final Appender appender) { 230 } 231 232 @Override 233 public void resetConfiguration() { 234 } 235 } 236 237 /** 238 * Internal LogManager. 239 */ 240 private static class PrivateManager extends org.apache.logging.log4j.LogManager { 241 private static final String FQCN = LogManager.class.getName(); 242 243 public static LoggerContext getContext() { 244 return getContext(FQCN, false); 245 } 246 247 public static org.apache.logging.log4j.Logger getLogger(final String name) { 248 return getLogger(FQCN, name); 249 } 250 } 251}