use of org.apache.log4j.spi.RootLogger in project logging-log4j2 by apache.
the class PropertyConfiguratorTest method testLocalVsGlobal.
@Test
public void testLocalVsGlobal() {
LoggerRepository repos1, repos2;
final Logger catA = Logger.getLogger(CAT_A_NAME);
final Logger catB = Logger.getLogger(CAT_B_NAME);
final Logger catC = Logger.getLogger(CAT_C_NAME);
final Properties globalSettings = new Properties();
globalSettings.put("log4j.logger." + CAT_A_NAME, Level.WARN.toString());
globalSettings.put("log4j.logger." + CAT_B_NAME, Level.WARN.toString());
globalSettings.put("log4j.logger." + CAT_C_NAME, Level.DEBUG.toString());
PropertyConfigurator.configure(globalSettings);
assertEquals(Level.WARN, catA.getLevel());
assertEquals(Level.WARN, catB.getLevel());
assertEquals(Level.DEBUG, catC.getLevel());
assertEquals(Level.WARN, catA.getLoggerRepository().getLogger(CAT_A_NAME).getLevel());
assertEquals(Level.WARN, catB.getLoggerRepository().getLogger(CAT_B_NAME).getLevel());
assertEquals(Level.DEBUG, catC.getLoggerRepository().getLogger(CAT_C_NAME).getLevel());
final Properties repos1Settings = new Properties();
repos1Settings.put("log4j.logger." + CAT_A_NAME, Level.DEBUG.toString());
repos1Settings.put("log4j.logger." + CAT_B_NAME, Level.INFO.toString());
repos1 = new Hierarchy(new RootLogger(Level.OFF));
new PropertyConfigurator().doConfigure(repos1Settings, repos1);
assertEquals(Level.DEBUG, repos1.getLogger(CAT_A_NAME).getLevel());
assertEquals(Level.INFO, repos1.getLogger(CAT_B_NAME).getLevel());
final Properties repos2Settings = new Properties();
repos2Settings.put("log4j.logger." + CAT_A_NAME, Level.INFO.toString());
repos2Settings.put("log4j.logger." + CAT_B_NAME, Level.DEBUG.toString());
repos2 = new Hierarchy(new RootLogger(Level.OFF));
new PropertyConfigurator().doConfigure(repos2Settings, repos2);
assertEquals(Level.INFO, repos2.getLogger(CAT_A_NAME).getLevel());
assertEquals(Level.DEBUG, repos2.getLogger(CAT_B_NAME).getLevel());
}
use of org.apache.log4j.spi.RootLogger in project commons by twitter.
the class JULBridgeLogManagerTest method checkAssimilateTakesOver.
@Test
public void checkAssimilateTakesOver() {
// Create a test log4j environment
final List<LoggingEvent> events = new LinkedList<LoggingEvent>();
org.apache.log4j.Logger log4jRoot = new RootLogger(org.apache.log4j.Level.ALL);
LoggerRepository loggerRepository = new Hierarchy(log4jRoot);
loggerRepository.setThreshold(org.apache.log4j.Level.INFO);
log4jRoot.addAppender(new AppenderSkeleton() {
@Override
public boolean requiresLayout() {
return false;
}
@Override
public void close() {
}
@Override
protected void append(LoggingEvent event) {
events.add(event);
}
});
JULBridgeLogManager.assimilate(loggerRepository);
Logger.getLogger("test.1").log(Level.INFO, "test message 1");
Logger.getLogger("test.2").log(Level.FINE, "test message 2");
Logger.getLogger("test.3").log(Level.WARNING, "test message 3");
assertThat(events.size(), is(2));
assertThat(events.get(0).getLoggerName(), is("test.1"));
assertThat(events.get(0).getMessage(), is((Object) "test message 1"));
assertThat(events.get(1).getLoggerName(), is("test.3"));
assertThat(events.get(1).getMessage(), is((Object) "test message 3"));
}
use of org.apache.log4j.spi.RootLogger in project ACS by ACS-Community.
the class Log4jFactory method enableAcsLogging.
/**
* This method must be called once in order to enable ACS logging behind the scenes of log4j logging.
* <p>
* The log4j framework is quite resistant against being substituted with a different logging framework.
* Even though it is possible to configure a custom logger factory using <code>log4j.loggerFactory</code>,
* that factory will not be used when 3rd party code calls the usual <code>Logger.getLogger(name)</code>.
* It seems to make sense only for cases where the custom logger is used as in <code>MyLogger.getLogger(name)</code>.
* log4j-over-slf4j (http://www.slf4j.org/legacy.html) simply re-implements the relevant log4j classes,
* which is too much trouble here for us because only basic log4j features are being used.
* <p>
* We make use of the RepositorySelector mechanism, which log4j foresees for a different purpose,
* to separate logging contexts in an application server that does not have classloader separation.
* (See also http://articles.qos.ch/sc.html.)
* It is not possible to configure this externally, so that an application must call this method.
* See also http://mail-archives.apache.org/mod_mbox/logging-log4j-user/200904.mbox/%3Ca44e15a30904020424g4b7d7fcx63ca32152c81f80d@mail.gmail.com%3E
* <p>
* @TODO: In the future we could let ClientLogManager call this method,
* but currently we are afraid of side effects with frameworks other than the laser alarm system
* that also use log4j (see http://jira.alma.cl/browse/COMP-8423).
*/
public static void enableAcsLogging() {
System.setProperty("log4j.defaultInitOverride", "true");
// System.setProperty("log4j.debug", "true");
Hierarchy h = new MyLog4jHierarchy();
RepositorySelector repositorySelector = new DefaultRepositorySelector(h);
LogManager.setRepositorySelector(repositorySelector, null);
Logger rootLogger = Logger.getRootLogger();
rootLogger.removeAllAppenders();
// to avoid "log4j:WARN No appenders could be found for logger (root)."
rootLogger.addAppender(new NullAppender());
rootLogger.setLevel(Level.ALL);
}
use of org.apache.log4j.spi.RootLogger in project com.revolsys.open by revolsys.
the class ContextClassLoaderRepositorySelector method add.
/**
* Add a new hierarchy for the specified class loader if one does not exist or
* return the previous hierarchy.
*
* @param classLoader The classloader to create the hierarchy for.
* @return The created heirarchy.
*/
public static synchronized Hierarchy add(final ClassLoader classLoader) {
Hierarchy hierarchy = (Hierarchy) repositories.get(classLoader);
if (hierarchy == null) {
hierarchy = new Hierarchy(new RootLogger(Level.DEBUG));
repositories.put(classLoader, hierarchy);
}
return hierarchy;
}
use of org.apache.log4j.spi.RootLogger in project alluxio by Alluxio.
the class AlluxioLog4jSocketNode method configureHierarchy.
/**
* Configure a {@link Hierarchy} instance used to retrive logger by name and maintain the logger
* hierarchy. {@link AlluxioLog4jSocketNode} instance can retrieve the logger to log incoming
* {@link org.apache.log4j.spi.LoggingEvent}s.
*
* @param processType type of the process sending this {@link LoggingEvent}
* @return a {@link Hierarchy} instance to retrieve logger
* @throws IOException if fails to create an {@link FileInputStream} to read log4j.properties
*/
private LoggerRepository configureHierarchy(String processType) throws IOException {
String inetAddressStr = mSocket.getInetAddress().getHostAddress();
Properties properties = new Properties();
File configFile;
try {
configFile = new File(new URI(System.getProperty("log4j.configuration")));
} catch (URISyntaxException e) {
// properties file is global, we should throw an exception.
throw new RuntimeException("Cannot derive a valid URI to log4j.properties file.", e);
}
try (FileInputStream inputStream = new FileInputStream(configFile)) {
properties.load(inputStream);
}
// Assign Level.TRACE to level so that log server prints whatever log messages received from
// log clients. If the log server receives a LoggingEvent, it assumes that the remote
// log client has the intention of writing this LoggingEvent to logs. It does not make
// much sense for the log client to send the message over the network and wants the
// messsage to be discarded by the log server.
// With this assumption, since TRACE is the lowest logging level, we do not have to compare
// the level of the event and the level of the logger when the log server receives
// LoggingEvents.
Level level = Level.TRACE;
Hierarchy clientHierarchy = new Hierarchy(new RootLogger(level));
// Startup script should guarantee that mBaseLogsDir already exists.
String logDirectoryPath = PathUtils.concatPath(mBaseLogsDir, processType.toLowerCase());
File logDirectory = new File(logDirectoryPath);
if (!logDirectory.exists()) {
logDirectory.mkdir();
}
String logFilePath = PathUtils.concatPath(logDirectoryPath, inetAddressStr + ".log");
properties.setProperty(ROOT_LOGGER_PROPERTY_KEY, level.toString() + "," + AlluxioLogServerProcess.LOGSERVER_CLIENT_LOGGER_APPENDER_NAME);
properties.setProperty(ROOT_LOGGER_APPENDER_FILE_PROPERTY_KEY, logFilePath);
new PropertyConfigurator().doConfigure(properties, clientHierarchy);
return clientHierarchy;
}
Aggregations