import org.apache.log4j.Logger; //导入方法依赖的package包/类
/**
* Constructor - when created try to find if a DB appender has been configured on the local system
*/
@SuppressWarnings( "unchecked")
public RemoteLoggingConfigurator( String atsAgent ) {
/*
* This code is run on:
* - Test Executor side prior to calling an agent for first time in this testcase
* - Agent side prior to calling another chained agent for first time in this testcase
*
* we use this code to remember the logging configuration which we will pass to some agent
*/
this.atsAgent = atsAgent;
// look for the DB appender
Category log = Logger.getLogger("com.axway.ats");
boolean dbAppenderIsProcessed = false;
while (log != null && !dbAppenderIsProcessed) {
Enumeration appenders = log.getAllAppenders();
while (appenders.hasMoreElements()) {
Appender appender = appenders.nextElement();
if (appender.getClass() == ActiveDbAppender.class // running on Test Executor side
|| appender.getClass() == PassiveDbAppender.class // running on Agent side
) {
//we found the appender, read all properties
appenderConfiguration = ((AbstractDbAppender) appender).getAppenderConfig();
appenderLogger = log.getName();
int atsDbLogLevel = DEFAULT_LOG_LEVEL;
if (atsDbLoggerLevelsFromTests.containsKey(this.atsAgent)) {
// user specified in the test the log level for this agent
atsDbLogLevel = atsDbLoggerLevelsFromTests.get(this.atsAgent).toInt();
} else if (log.getLevel() != null) {
// user specified the log level in log4j configuration file
atsDbLogLevel = log.getLevel().toInt();
}
//set the effective logging level for threshold if new one is set
if (appenderConfiguration.getLoggingThreshold() == null
|| appenderConfiguration.getLoggingThreshold().toInt() != atsDbLogLevel) {
/*
* Log4j is deprecating the Priority class used by setLoggingThreshold,
* but we cannot make an instance of this class as its constructor is not public.
*
* So here we first change the log level on the Test Executor,
* then get the Priority object, then restore back the value on the Test Executor
*/
final Level currentLevelBackup = log.getLevel();
log.setLevel(Level.toLevel(atsDbLogLevel));
appenderConfiguration.setLoggingThreshold(log.getEffectiveLevel());
log.setLevel(currentLevelBackup);
}
//exit the loop
dbAppenderIsProcessed = true;
break;
}
}
log = log.getParent();
}
// look for any user loggers
Enumeration allLoggers = Logger.getRootLogger().getLoggerRepository().getCurrentLoggers();
while (allLoggers.hasMoreElements()) {
Logger logger = allLoggers.nextElement();
Level level = logger.getLevel();
if (level != null) {
// user explicitly specified a level for this logger
otherLoggerLevels.put(logger.getName(), level.toInt());
}
}
}