org.apache.log4j.AsyncAppender Java Examples

The following examples show how to use org.apache.log4j.AsyncAppender. 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: ConfigManager.java    From serve with Apache License 2.0 7 votes vote down vote up
private void enableAsyncLogging(Logger logger) {
    AsyncAppender asyncAppender = new AsyncAppender();

    @SuppressWarnings("unchecked")
    Enumeration<Appender> en = logger.getAllAppenders();
    while (en.hasMoreElements()) {
        Appender appender = en.nextElement();
        if (appender instanceof AsyncAppender) {
            // already async
            return;
        }

        logger.removeAppender(appender);
        asyncAppender.addAppender(appender);
    }
    logger.addAppender(asyncAppender);
}
 
Example #2
Source File: ConfigManager.java    From multi-model-server with Apache License 2.0 6 votes vote down vote up
private void enableAsyncLogging(Logger logger) {
    AsyncAppender asyncAppender = new AsyncAppender();

    @SuppressWarnings("unchecked")
    Enumeration<Appender> en = logger.getAllAppenders();
    while (en.hasMoreElements()) {
        Appender appender = en.nextElement();
        if (appender instanceof AsyncAppender) {
            // already async
            return;
        }

        logger.removeAppender(appender);
        asyncAppender.addAppender(appender);
    }
    logger.addAppender(asyncAppender);
}
 
Example #3
Source File: Log4jHookProxy.java    From uavstack with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private LinkedList<LogProfileInfo> figureoutLogConfiguration(Logger Logger, String appid) {
    
    LinkedList<LogProfileInfo> logProfiles = new LinkedList<LogProfileInfo>();

    Enumeration<Appender> appenders = Logger.getAllAppenders();
    while (appenders != null && appenders.hasMoreElements()) {

        Appender appender = appenders.nextElement();
        if (appender instanceof FileAppender) {
            logProfiles.add(getAppenderInfo((FileAppender)appender, appid));
        }
        else if(appender instanceof AsyncAppender) {
            Enumeration<Appender> ads = ((AsyncAppender) appender).getAllAppenders();
            while(ads != null && ads.hasMoreElements()) {
                Appender ap = ads.nextElement();
                if(ap instanceof FileAppender) {
                    logProfiles.add(getAppenderInfo((FileAppender)ap, appid));
                }
            }
        }
    }
    
    return logProfiles;
}
 
Example #4
Source File: CoasterService.java    From swift-k with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("rawtypes")
protected static Appender getFileAppender() {
    Logger root = Logger.getRootLogger();
    Enumeration e = root.getAllAppenders();
    while (e.hasMoreElements()) {
        Appender a = (Appender) e.nextElement();
        if (a instanceof FileAppender) {
            return a;
        }
        if (a instanceof AsyncAppender) {
            // likely this is running in a JVM in which
            // the file appender has been replaced with
            // an async appender, so don't mess with things
            return null;
        }
    }
    logger.warn("Could not find a file appender to configure");
    return null;
}
 
Example #5
Source File: Log4jHelper.java    From arthas with Apache License 2.0 5 votes vote down vote up
private static List<Map<String, Object>> doGetLoggerAppenders(Enumeration<Appender> appenders) {
    List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();

    if (appenders == null) {
        return result;
    }

    while (appenders.hasMoreElements()) {
        Map<String, Object> info = new HashMap<String, Object>();
        Appender appender = appenders.nextElement();

        info.put(LoggerHelper.name, appender.getName());
        info.put(LoggerHelper.clazz, appender.getClass());

        result.add(info);
        if (appender instanceof FileAppender) {
            info.put(LoggerHelper.file, ((FileAppender) appender).getFile());
        } else if (appender instanceof ConsoleAppender) {
            info.put(LoggerHelper.target, ((ConsoleAppender) appender).getTarget());
        } else if (appender instanceof AsyncAppender) {
            @SuppressWarnings("unchecked")
            Enumeration<Appender> appendersOfAsync = ((AsyncAppender) appender).getAllAppenders();
            if (appendersOfAsync != null) {
                List<Map<String, Object>> asyncs = doGetLoggerAppenders(appendersOfAsync);
                // 标明异步appender
                List<String> appenderRef = new ArrayList<String>();
                for (Map<String, Object> a : asyncs) {
                    appenderRef.add((String) a.get(LoggerHelper.name));
                    result.add(a);
                }
                info.put(LoggerHelper.blocking, ((AsyncAppender) appender).getBlocking());
                info.put(LoggerHelper.appenderRef, appenderRef);
            }
        }
    }

    return result;
}
 
Example #6
Source File: TestAuditLogs.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Before
 public void setupCluster() throws Exception {
   // must configure prior to instantiating the namesystem because it
   // will reconfigure the logger if async is enabled
   configureAuditLogs();
   conf = new HdfsConfiguration();
   final long precision = 1L;
   conf.setLong(DFSConfigKeys.DFS_NAMENODE_ACCESSTIME_PRECISION_KEY, precision);
   conf.setLong(DFSConfigKeys.DFS_BLOCKREPORT_INTERVAL_MSEC_KEY, 10000L);
   conf.setBoolean(DFSConfigKeys.DFS_WEBHDFS_ENABLED_KEY, true);
   conf.setBoolean(DFSConfigKeys.DFS_NAMENODE_AUDIT_LOG_ASYNC_KEY, useAsyncLog);
   util = new DFSTestUtil.Builder().setName("TestAuditAllowed").
       setNumFiles(20).build();
   cluster = new MiniDFSCluster.Builder(conf).numDataNodes(4).build();
   fs = cluster.getFileSystem();
   util.createFiles(fs, fileName);

   // make sure the appender is what it's supposed to be
   Logger logger = ((Log4JLogger) FSNamesystem.auditLog).getLogger();
   @SuppressWarnings("unchecked")
   List<Appender> appenders = Collections.list(logger.getAllAppenders());
   assertEquals(1, appenders.size());
   assertEquals(useAsyncLog, appenders.get(0) instanceof AsyncAppender);
   
   fnames = util.getFileNames(fileName);
   util.waitReplication(fs, fileName, (short)3);
   userGroupInfo = UserGroupInformation.createUserForTesting(username, groups);
}
 
Example #7
Source File: TestAuditLogs.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Before
 public void setupCluster() throws Exception {
   // must configure prior to instantiating the namesystem because it
   // will reconfigure the logger if async is enabled
   configureAuditLogs();
   conf = new HdfsConfiguration();
   final long precision = 1L;
   conf.setLong(DFSConfigKeys.DFS_NAMENODE_ACCESSTIME_PRECISION_KEY, precision);
   conf.setLong(DFSConfigKeys.DFS_BLOCKREPORT_INTERVAL_MSEC_KEY, 10000L);
   conf.setBoolean(DFSConfigKeys.DFS_WEBHDFS_ENABLED_KEY, true);
   conf.setBoolean(DFSConfigKeys.DFS_NAMENODE_AUDIT_LOG_ASYNC_KEY, useAsyncLog);
   util = new DFSTestUtil.Builder().setName("TestAuditAllowed").
       setNumFiles(20).build();
   cluster = new MiniDFSCluster.Builder(conf).numDataNodes(4).build();
   fs = cluster.getFileSystem();
   util.createFiles(fs, fileName);

   // make sure the appender is what it's supposed to be
   Logger logger = ((Log4JLogger) FSNamesystem.auditLog).getLogger();
   @SuppressWarnings("unchecked")
   List<Appender> appenders = Collections.list(logger.getAllAppenders());
   assertEquals(1, appenders.size());
   assertEquals(useAsyncLog, appenders.get(0) instanceof AsyncAppender);
   
   fnames = util.getFileNames(fileName);
   util.waitReplication(fs, fileName, (short)3);
   userGroupInfo = UserGroupInformation.createUserForTesting(username, groups);
}
 
Example #8
Source File: SyslogAppenderTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Tests append method under normal conditions.
 */
public void testAppend() {
  SyslogAppender appender = new SyslogAppender();
  appender.setName("foo");
  appender.setThreshold(Level.INFO);
  appender.setSyslogHost("localhost");
  appender.setFacility("user");
  appender.setLayout(new PatternLayout("%m%n"));

  VectorErrorHandler errorHandler = new VectorErrorHandler();
  appender.setErrorHandler(errorHandler);
  appender.activateOptions();

  //
  //  wrap SyslogAppender with an Async since appender may
  //    hang if syslogd is not accepting network messages
  //
  AsyncAppender asyncAppender = new AsyncAppender();
  asyncAppender.addAppender(appender);
  asyncAppender.activateOptions();

  Logger logger = Logger.getRootLogger();
  logger.addAppender(asyncAppender);

  Exception e =
    new Exception("Expected exception from SyslogAppenderTest.testAppend");
  logger.info(
    "Expected message from log4j unit test SyslogAppenderTest.testAppend.", e);
  assertEquals(0, errorHandler.size());
}
 
Example #9
Source File: RMStarter.java    From scheduling with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Wrap existing appenders configured for the root logger in AsyncAppenders
 */
public static void overrideAppenders() {
    if (PAResourceManagerProperties.LOG4J_ASYNC_APPENDER_ENABLED.getValueAsBoolean()) {
        Logger rootLogger = Logger.getRootLogger();
        Enumeration<?> en = rootLogger.getAllAppenders();
        if (en != null) {
            List<AsyncAppender> newAppenders = new ArrayList<>();
            List<String> appendersToRemove = new ArrayList<>();
            int index = 0;
            while (en.hasMoreElements()) {
                Appender app = (Appender) en.nextElement();
                if (app != null && !(app instanceof AsyncAppender)) {
                    AsyncAppender asyncAppender = new AsyncAppender();
                    asyncAppender.setName("MainAsyncAppender_" + index);
                    asyncAppender.setBufferSize(PAResourceManagerProperties.LOG4J_ASYNC_APPENDER_BUFFER_SIZE.getValueAsInt());
                    asyncAppender.addAppender(app);
                    newAppenders.add(asyncAppender);
                    appendersToRemove.add(app.getName());
                    index++;
                }
            }
            for (String appenderName : appendersToRemove) {
                rootLogger.removeAppender(appenderName);
            }
            for (Appender newAppender : newAppenders) {
                rootLogger.addAppender(newAppender);
            }
        }
    }
}
 
Example #10
Source File: CoasterService.java    From swift-k with Apache License 2.0 5 votes vote down vote up
public static void configureLogName() {
    FileAppender fa = (FileAppender) getFileAppender();
    if (fa != null) {
        fa.setFile(Bootstrap.LOG_DIR + File.separator + makeLogFileName());
        fa.activateOptions();

        AsyncAppender aa = new AsyncAppender();
        aa.addAppender(fa);
        replaceAppender(fa, aa);
    }
}
 
Example #11
Source File: CoasterService.java    From swift-k with Apache License 2.0 4 votes vote down vote up
private static void replaceAppender(FileAppender fa, AsyncAppender aa) {
    Logger root = Logger.getRootLogger();
    root.removeAppender(fa);
    root.addAppender(aa);
}
 
Example #12
Source File: Loader.java    From swift-k with Apache License 2.0 4 votes vote down vote up
protected static String setupLogging(ArgumentParser ap, SwiftConfig config, String projectName,
        String runID) throws IOException {
    String logfile;
    if (ap.isPresent(ARG_LOGFILE)) {
        logfile = ap.getStringValue(ARG_LOGFILE);
    }
    else {
        logfile = projectName + "-" + runID + ".log";
    }
    
    config.setProperty("logfile", logfile);
    
    File f = new File(logfile);

    FileAppender fa = (FileAppender) getAppender(FileAppender.class);
    AsyncAppender aa = new AsyncAppender();
    aa.addAppender(fa);
    
    replaceAppender(fa, aa);
    if (fa == null) {
        logger.warn("Failed to configure log file name");
    }
    else {
        fa.setFile(f.getAbsolutePath());
        if (fa instanceof LazyFileAppender) {
            ((LazyFileAppender) fa).fileNameConfigured();
        }
        fa.activateOptions();
    }
    Level level = Level.WARN;
    if (ap.isPresent(ARG_VERBOSE)) {
        level = Level.INFO;
    }
    if (ap.isPresent(ARG_DEBUG)) {
        level = Level.DEBUG;
    }
    ConsoleAppender ca = (ConsoleAppender) getAppender(ConsoleAppender.class);
    if (ca == null) {
        logger.warn("Failed to configure console log level");
    }
    else {
        ca.setThreshold(level);
        ca.activateOptions();
    }
    Logger.getLogger(Log.class).setLevel(Level.INFO);
    if (ap.isPresent(ARG_UI) && !"summary".equals(ap.getStringValue(ARG_UI))) {
        String ui = ap.getStringValue(ARG_UI);
        if ("none".equals(ui)) {
            // config should be loaded now
            SwiftConfig.getDefault().set(SwiftConfig.Key.TICKER_ENABLED, false);
        }
        else {
            ma = new MonitorAppender(projectName, ui);
            Logger.getRootLogger().addAppender(ma);
            Logger.getLogger(Log.class).setLevel(Level.DEBUG);
            Logger.getLogger(AbstractGridNode.class).setLevel(Level.DEBUG);
            Logger.getLogger(Execute.class).setLevel(Level.DEBUG);
            Logger.getLogger(SwiftExecutor.class).setLevel(Level.INFO);
            Logger.getLogger(WeightedHostScoreScheduler.class).setLevel(
                Level.INFO);
        }
        if ("TUI".equals(ui) || "ANSI".equals(ui)) {
            ca.setThreshold(Level.FATAL);
        }
    }
    else if (ap.isPresent(ARG_MINIMAL_LOGGING)) {
        Logger.getLogger("swift").setLevel(Level.WARN);
        Logger.getRootLogger().setLevel(Level.WARN);
    }
    else if (ap.isPresent(ARG_REDUCED_LOGGING)) {
        Logger.getLogger(AbstractDataNode.class).setLevel(Level.WARN);
        Logger.getLogger(New.class).setLevel(Level.WARN);
        Logger.getLogger("org.globus.cog.karajan.workflow.service").setLevel(Level.WARN);
        Logger.getLogger("swift").setLevel(Level.INFO);
    }
    return logfile;
}
 
Example #13
Source File: Loader.java    From swift-k with Apache License 2.0 4 votes vote down vote up
private static void replaceAppender(FileAppender fa, AsyncAppender aa) {
    Logger root = Logger.getRootLogger();
    root.removeAppender(fa);
    root.addAppender(aa);
}