Java Code Examples for org.apache.log4j.Appender#close()

The following examples show how to use org.apache.log4j.Appender#close() . 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: GraphingStatisticsAppender.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
public void close() {
   AppenderAttachableImpl var1 = this.downstreamAppenders;
   synchronized(this.downstreamAppenders) {
      this.flush();
      Enumeration enumer = this.downstreamAppenders.getAllAppenders();

      while(enumer != null && enumer.hasMoreElements()) {
         Appender appender = (Appender)enumer.nextElement();
         appender.close();
      }

   }
}
 
Example 2
Source File: TestAuditLogs.java    From hadoop with Apache License 2.0 5 votes vote down vote up
private void verifyAuditLogsRepeat(boolean expectSuccess, int ndupe)
    throws IOException {
  // Turn off the logs
  Logger logger = ((Log4JLogger) FSNamesystem.auditLog).getLogger();
  logger.setLevel(Level.OFF);

  // Close the appenders and force all logs to be flushed
  Enumeration<?> appenders = logger.getAllAppenders();
  while (appenders.hasMoreElements()) {
    Appender appender = (Appender)appenders.nextElement();
    appender.close();
  }

  BufferedReader reader = new BufferedReader(new FileReader(auditLogFile));
  String line = null;
  boolean ret = true;

  try {
    for (int i = 0; i < ndupe; i++) {
      line = reader.readLine();
      assertNotNull(line);
      assertTrue("Expected audit event not found in audit log",
          auditPattern.matcher(line).matches());
      ret &= successPattern.matcher(line).matches();
    }
    assertNull("Unexpected event in audit log", reader.readLine());
    assertTrue("Expected success=" + expectSuccess, ret == expectSuccess);
  } finally {
    reader.close();
  }
}
 
Example 3
Source File: TestAuditLogs.java    From hadoop with Apache License 2.0 5 votes vote down vote up
private void verifyAuditLogsCheckPattern(boolean expectSuccess, int ndupe, Pattern pattern)
    throws IOException {
  // Turn off the logs
  Logger logger = ((Log4JLogger) FSNamesystem.auditLog).getLogger();
  logger.setLevel(Level.OFF);

  // Close the appenders and force all logs to be flushed
  Enumeration<?> appenders = logger.getAllAppenders();
  while (appenders.hasMoreElements()) {
    Appender appender = (Appender)appenders.nextElement();
    appender.close();
  }

  BufferedReader reader = new BufferedReader(new FileReader(auditLogFile));
  String line = null;
  boolean ret = true;
  boolean patternMatches = false;

  try {
      for (int i = 0; i < ndupe; i++) {
        line = reader.readLine();
        assertNotNull(line);
        patternMatches |= pattern.matcher(line).matches();
        ret &= successPattern.matcher(line).matches();
      }
      assertNull("Unexpected event in audit log", reader.readLine());
      assertTrue("Expected audit event not found in audit log", patternMatches);
      assertTrue("Expected success=" + expectSuccess, ret == expectSuccess);
    } finally {
      reader.close();
    }
}
 
Example 4
Source File: TestAuditLogs.java    From big-c with Apache License 2.0 5 votes vote down vote up
private void verifyAuditLogsRepeat(boolean expectSuccess, int ndupe)
    throws IOException {
  // Turn off the logs
  Logger logger = ((Log4JLogger) FSNamesystem.auditLog).getLogger();
  logger.setLevel(Level.OFF);

  // Close the appenders and force all logs to be flushed
  Enumeration<?> appenders = logger.getAllAppenders();
  while (appenders.hasMoreElements()) {
    Appender appender = (Appender)appenders.nextElement();
    appender.close();
  }

  BufferedReader reader = new BufferedReader(new FileReader(auditLogFile));
  String line = null;
  boolean ret = true;

  try {
    for (int i = 0; i < ndupe; i++) {
      line = reader.readLine();
      assertNotNull(line);
      assertTrue("Expected audit event not found in audit log",
          auditPattern.matcher(line).matches());
      ret &= successPattern.matcher(line).matches();
    }
    assertNull("Unexpected event in audit log", reader.readLine());
    assertTrue("Expected success=" + expectSuccess, ret == expectSuccess);
  } finally {
    reader.close();
  }
}
 
Example 5
Source File: TestAuditLogs.java    From big-c with Apache License 2.0 5 votes vote down vote up
private void verifyAuditLogsCheckPattern(boolean expectSuccess, int ndupe, Pattern pattern)
    throws IOException {
  // Turn off the logs
  Logger logger = ((Log4JLogger) FSNamesystem.auditLog).getLogger();
  logger.setLevel(Level.OFF);

  // Close the appenders and force all logs to be flushed
  Enumeration<?> appenders = logger.getAllAppenders();
  while (appenders.hasMoreElements()) {
    Appender appender = (Appender)appenders.nextElement();
    appender.close();
  }

  BufferedReader reader = new BufferedReader(new FileReader(auditLogFile));
  String line = null;
  boolean ret = true;
  boolean patternMatches = false;

  try {
      for (int i = 0; i < ndupe; i++) {
        line = reader.readLine();
        assertNotNull(line);
        patternMatches |= pattern.matcher(line).matches();
        ret &= successPattern.matcher(line).matches();
      }
      assertNull("Unexpected event in audit log", reader.readLine());
      assertTrue("Expected audit event not found in audit log", patternMatches);
      assertTrue("Expected success=" + expectSuccess, ret == expectSuccess);
    } finally {
      reader.close();
    }
}
 
Example 6
Source File: AppenderAttachableImpl.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
  * Remove and close all previously attached appenders.
  * */
 public
 void removeAllAppenders() {
   if(appenderList != null) {
     int len = appenderList.size();      
     for(int i = 0; i < len; i++) {
Appender a = (Appender) appenderList.elementAt(i);
a.close();
     }
     appenderList.removeAllElements();
     appenderList = null;      
   }
 }
 
Example 7
Source File: JobLogger.java    From scheduling with GNU Affero General Public License v3.0 5 votes vote down vote up
public void close(JobId id) {
    updateMdcWithTaskLogFilename(id);
    logger.debug(PREFIX + id + " closing logger");
    for (Appender appender : (List<Appender>) Collections.list(logger.getAllAppenders())) {
        if (appender != null && appender instanceof FileAppender) {
            appender.close();
        }
    }
    MDC.remove(FileAppender.FILE_NAME);
}
 
Example 8
Source File: TaskLogger.java    From scheduling with GNU Affero General Public License v3.0 5 votes vote down vote up
public void close(TaskId id) {
    updateMdcWithTaskLogFilename(id);
    logger.debug(format(id, "closing logger"));
    for (Appender appender : (List<Appender>) Collections.list(logger.getAllAppenders())) {
        if (appender != null && appender instanceof FileAppender) {
            appender.close();
        }
    }
    MDC.remove(FileAppender.FILE_NAME);
}
 
Example 9
Source File: LogUtil.java    From datacollector with Apache License 2.0 5 votes vote down vote up
public static void resetRollingFileAppender(String pipeline, String rev, String suffix) {
  String loggerName = getLoggerName(pipeline, rev, suffix);
  Logger logger =Logger.getLogger(loggerName);
  Appender logAppender = logger.getAppender(loggerName);
  if (logAppender != null) {
    logAppender.close();
  }
  logger.removeAppender(loggerName);
}
 
Example 10
Source File: Log4jAppenderHandler.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void close() throws SecurityException {
    checkAccess(this);
    final Appender appender = appenderUpdater.getAndSet(this, null);
    if (appender != null) {
        appender.close();
    }
}
 
Example 11
Source File: RemoteLoggingConfigurator.java    From ats-framework with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings( "unchecked")
public void revert() {

    /*
     * This code is run on remote agent's side
     */

    /*
     * The outer code will call this method when the "needs applying" method return true.
     * 
     * Currently we could come here because of 2 reasons:
     *  - the DB appenders must be reconfigured
     *  - the user loggers must be reconfigured
     */

    if (appenderLogger != null && needsToConfigureDbAppender) {
        // there is a DB appender and it is out-of-date

        //remove the filter which will deny logging of system events
        Logger rootLogger = Logger.getRootLogger();
        Enumeration<Appender> appenders = rootLogger.getAllAppenders();
        while (appenders.hasMoreElements()) {
            Appender appender = appenders.nextElement();

            //remove the filter
            //FIXME:This is very risky, as someone may have added other filters
            //the current implementation of the filter chain in log4j will not allow
            //us to easily remove a single filter
            appender.clearFilters();
        }

        Category log;
        if ("root".equals(appenderLogger)) {
            log = Logger.getRootLogger();
        } else {
            log = Logger.getLogger(appenderLogger);
        }

        Appender dbAppender = PassiveDbAppender.getCurrentInstance(ThreadsPerCaller.getCaller());
        if (dbAppender != null) {
            //close the appender
            dbAppender.close();

            //remove it
            log.removeAppender(dbAppender);
        }
    }

    // in case we must reconfigure the custom loggers, here we should remove them from log4j, 
    // but log4j does not provide a way to do it - so we do nothing here 
}
 
Example 12
Source File: Util.java    From spork with Apache License 2.0 4 votes vote down vote up
public static void removeLogAppender(Class clazz, String appenderName) {
    Logger logger = Logger.getLogger(clazz);
    Appender appender = logger.getAppender(appenderName);
    appender.close();
    logger.removeAppender(appenderName);
}
 
Example 13
Source File: AppenderAttachableImpl.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
public void close() {
    for (Appender appender : appenders.values()) {
        appender.close();
    }
}
 
Example 14
Source File: DataStreamerImplSelfTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * Cluster topology mismatch shall result in DataStreamer retrying cache update with the latest topology and
 * no error logged to the console.
 *
 * @throws Exception if failed
 */
@Test
public void testRetryWhenTopologyMismatch() throws Exception {
    final int KEY = 1;
    final String VAL = "1";

    cnt = 0;

    StringWriter logWriter = new StringWriter();
    Appender logAppender = new WriterAppender(new SimpleLayout(), logWriter);

    Logger.getRootLogger().addAppender(logAppender);

    startGrids(MAX_CACHE_COUNT - 1); // cache-enabled nodes

    try (Ignite ignite = startGrid(MAX_CACHE_COUNT);
         IgniteDataStreamer<Integer, String> streamer = ignite.dataStreamer(DEFAULT_CACHE_NAME)) {

        needStaleTop = true; // simulate stale topology for the next action

        streamer.addData(KEY, VAL);
    } finally {
        needStaleTop = false;

        logWriter.flush();

        Logger.getRootLogger().removeAppender(logAppender);

        logAppender.close();
    }

    assertFalse(logWriter.toString().contains("DataStreamer will retry data transfer at stable topology"));
}