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