Java Code Examples for org.apache.log4j.Category#getLevel()

The following examples show how to use org.apache.log4j.Category#getLevel() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PropertyPrinter.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
protected
void printOptions(PrintWriter out, Category cat) {
  Enumeration appenders = cat.getAllAppenders();
  Level prio = cat.getLevel();
  String appenderString = (prio == null ? "" : prio.toString());
  
  while (appenders.hasMoreElements()) {
    Appender app = (Appender) appenders.nextElement();
    String name;
    
    if ((name = (String) appenderNames.get(app)) == null) {
    
      // first assign name to the appender
      if ((name = app.getName()) == null || isGenAppName(name)) {
          name = genAppName();
      }
      appenderNames.put(app, name);
      
      printOptions(out, app, "log4j.appender."+name);
      if (app.getLayout() != null) {
        printOptions(out, app.getLayout(), "log4j.appender."+name+".layout");
      }
    }
    appenderString += ", " + name;
  }
  String catKey = (cat == Logger.getRootLogger())
      ? "log4j.rootLogger"
      : "log4j.logger." + cat.getName();
  if (appenderString != "") {
    out.println(catKey + "=" + appenderString);
  }
  if (!cat.getAdditivity() && cat != Logger.getRootLogger()) {
  	out.println("log4j.additivity." + cat.getName() + "=false");    
  }
}
 
Example 2
Source File: LoggerUtilTest.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
@Test
public void testParentLevel()
{
  org.apache.log4j.Logger log4jLogger = LogManager.getLogger("com.datatorrent.stram.util.Unknown");
  assertNull(log4jLogger.getLevel());
  Category parent = log4jLogger.getParent();
  while (parent.getLevel() == null) {
    parent = parent.getParent();
  }
  assertSame(log4jLogger.getEffectiveLevel(), parent.getLevel());
}
 
Example 3
Source File: LoggerSerializer.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
private String getLoggerLevel(Category logger) {
  if (null == logger) {
    return StringUtils.EMPTY;
  }
  Level level = logger.getLevel();
  if (null != level) {
    return level.toString();
  } else {
    return getLoggerLevel(logger.getParent());
  }
}
 
Example 4
Source File: RemoteLoggingConfigurator.java    From ats-framework with Apache License 2.0 4 votes vote down vote up
/**
 * We will try to find if a DB appender has been configured on the local system
 * 
 * @param customLogLevel log level specified in the test ( or use AgentConfigurationLandscape.getInstance(atsAgent).getDbLogLevel() )
 * @param chunkSize chunk size (for batch db logging) specified in the test ( or use AgentConfigurationLandscape.getInstance(atsAgent).getChunkSize() )
 */
@SuppressWarnings( "unchecked")
public RemoteLoggingConfigurator( LogLevel customLogLevel, int chunkSize ) {

    /*
     * 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
     */

    // look for the DB appender
    Category log = Logger.getLogger("com.axway.ats");
    boolean dbAppenderIsProcessed = false;
    while (log != null && !dbAppenderIsProcessed) {

        Enumeration<Appender> 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();
                if (chunkSize > 0) {
                    // should a warning be logged here if the chunk size is not valid?
                    appenderConfiguration.setChunkSize(chunkSize + "");
                }
                appenderLogger = log.getName();

                int atsDbLogLevel = DEFAULT_LOG_LEVEL;
                if (customLogLevel != null) {
                    // user specified in the test the log level for this agent
                    atsDbLogLevel = customLogLevel.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<Logger> 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());
        }
    }
}