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; 020 021import org.apache.log4j.helpers.NullEnumeration; 022import org.apache.log4j.legacy.core.ContextUtil; 023import org.apache.log4j.spi.HierarchyEventListener; 024import org.apache.log4j.spi.LoggerFactory; 025import org.apache.log4j.spi.LoggerRepository; 026import org.apache.log4j.spi.RepositorySelector; 027import org.apache.logging.log4j.spi.LoggerContext; 028import org.apache.logging.log4j.util.Strings; 029 030/** 031 * 032 */ 033public final class LogManager { 034 035 /** 036 * @deprecated This variable is for internal use only. It will 037 * become package protected in future versions. 038 * */ 039 @Deprecated 040 public static final String DEFAULT_CONFIGURATION_FILE = "log4j.properties"; 041 042 /** 043 * @deprecated This variable is for internal use only. It will 044 * become private in future versions. 045 * */ 046 @Deprecated 047 public static final String DEFAULT_CONFIGURATION_KEY = "log4j.configuration"; 048 049 /** 050 * @deprecated This variable is for internal use only. It will 051 * become private in future versions. 052 * */ 053 @Deprecated 054 public static final String CONFIGURATOR_CLASS_KEY = "log4j.configuratorClass"; 055 056 /** 057 * @deprecated This variable is for internal use only. It will 058 * become private in future versions. 059 */ 060 @Deprecated 061 public static final String DEFAULT_INIT_OVERRIDE_KEY = "log4j.defaultInitOverride"; 062 063 static final String DEFAULT_XML_CONFIGURATION_FILE = "log4j.xml"; 064 065 private static final LoggerRepository REPOSITORY = new Repository(); 066 067 private static final boolean isLog4jCore; 068 069 static { 070 boolean core = false; 071 try { 072 if (Class.forName("org.apache.logging.log4j.core.LoggerContext") != null) { 073 core = true; 074 } 075 } catch (Exception ex) { 076 // Ignore the exception; 077 } 078 isLog4jCore = core; 079 } 080 081 private LogManager() { 082 } 083 084 public static Logger getRootLogger() { 085 return Category.getInstance(PrivateManager.getContext(), Strings.EMPTY); 086 } 087 088 public static Logger getLogger(final String name) { 089 return Category.getInstance(PrivateManager.getContext(), name); 090 } 091 092 public static Logger getLogger(final Class<?> clazz) { 093 return Category.getInstance(PrivateManager.getContext(), clazz.getName()); 094 } 095 096 public static Logger getLogger(final String name, final LoggerFactory factory) { 097 return Category.getInstance(PrivateManager.getContext(), name); 098 } 099 100 public static Logger exists(final String name) { 101 final LoggerContext ctx = PrivateManager.getContext(); 102 if (!ctx.hasLogger(name)) { 103 return null; 104 } 105 return Logger.getLogger(name); 106 } 107 108 @SuppressWarnings("rawtypes") 109 public static Enumeration getCurrentLoggers() { 110 return NullEnumeration.getInstance(); 111 } 112 113 static void reconfigure() { 114 if (isLog4jCore) { 115 final LoggerContext ctx = PrivateManager.getContext(); 116 ContextUtil.reconfigure(ctx); 117 } 118 } 119 120 /** 121 * No-op implementation. 122 */ 123 public static void shutdown() { 124 } 125 126 /** 127 * No-op implementation. 128 */ 129 public static void resetConfiguration() { 130 } 131 132 /** 133 * No-op implementation. 134 * @param selector The RepositorySelector. 135 * @param guard prevents calls at the incorrect time. 136 * @throws IllegalArgumentException if a parameter is invalid. 137 */ 138 public static void setRepositorySelector(final RepositorySelector selector, final Object guard) 139 throws IllegalArgumentException { 140 } 141 142 public static LoggerRepository getLoggerRepository() { 143 return REPOSITORY; 144 } 145 146 /** 147 * The Repository. 148 */ 149 private static class Repository implements LoggerRepository { 150 @Override 151 public void addHierarchyEventListener(final HierarchyEventListener listener) { 152 153 } 154 155 @Override 156 public boolean isDisabled(final int level) { 157 return false; 158 } 159 160 @Override 161 public void setThreshold(final Level level) { 162 163 } 164 165 @Override 166 public void setThreshold(final String val) { 167 168 } 169 170 @Override 171 public void emitNoAppenderWarning(final Category cat) { 172 173 } 174 175 @Override 176 public Level getThreshold() { 177 return Level.OFF; 178 } 179 180 @Override 181 public Logger getLogger(final String name) { 182 return Category.getInstance(PrivateManager.getContext(), name); 183 } 184 185 @Override 186 public Logger getLogger(final String name, final LoggerFactory factory) { 187 return Category.getInstance(PrivateManager.getContext(), name); 188 } 189 190 @Override 191 public Logger getRootLogger() { 192 return Category.getRoot(PrivateManager.getContext()); 193 } 194 195 @Override 196 public Logger exists(final String name) { 197 return LogManager.exists(name); 198 } 199 200 @Override 201 public void shutdown() { 202 } 203 204 @Override 205 @SuppressWarnings("rawtypes") 206 public Enumeration getCurrentLoggers() { 207 return NullEnumeration.getInstance(); 208 } 209 210 @Override 211 @SuppressWarnings("rawtypes") 212 public Enumeration getCurrentCategories() { 213 return NullEnumeration.getInstance(); 214 } 215 216 @Override 217 public void fireAddAppenderEvent(final Category logger, final Appender appender) { 218 } 219 220 @Override 221 public void resetConfiguration() { 222 } 223 } 224 225 /** 226 * Internal LogManager. 227 */ 228 private static class PrivateManager extends org.apache.logging.log4j.LogManager { 229 private static final String FQCN = LogManager.class.getName(); 230 231 public static LoggerContext getContext() { 232 return getContext(FQCN, false); 233 } 234 235 public static org.apache.logging.log4j.Logger getLogger(final String name) { 236 return getLogger(FQCN, name); 237 } 238 } 239}