Java Code Examples for org.apache.log4j.Logger#getAppender()

The following examples show how to use org.apache.log4j.Logger#getAppender() . 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: LogUtil.java    From datacollector with Apache License 2.0 6 votes vote down vote up
public static boolean registerLogger(String pipelineName, String rev, String suffix, String filename,
  Configuration configuration) {
  String loggerName = getLoggerName(pipelineName, rev, suffix);
  boolean registered;
  Logger logger = Logger.getLogger(loggerName);
  if (logger.getAppender(loggerName) != null) {
    LOG.debug("Logger '{}' already exists", loggerName);
    registered = false;
  }
  else {
    synchronized (logger) {
      if (logger.getAppender(loggerName) == null) {
        logger.setAdditivity(false);
        logger.addAppender(createRollingFileAppender(loggerName, filename, configuration));
        registered = true;
      } else {
        LOG.debug("Logger '{}' already exists", loggerName);
        registered = false;
      }
    }
  }
  return registered;
}
 
Example 2
Source File: DavGatewayTray.java    From davmail with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Open logging window.
 */
public static void showLogs() {
    Logger rootLogger = Logger.getRootLogger();
    LF5Appender lf5Appender = (LF5Appender) rootLogger.getAppender("LF5Appender");
    if (lf5Appender == null) {
        LogBrokerMonitor logBrokerMonitor = new LogBrokerMonitor(LogLevel.getLog4JLevels()) {
            @Override
            protected void closeAfterConfirm() {
                hide();
            }
        };
        lf5Appender = new LF5Appender(logBrokerMonitor);
        lf5Appender.setName("LF5Appender");
        rootLogger.addAppender(lf5Appender);
    }
    lf5Appender.getLogBrokerMonitor().show();
}
 
Example 3
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 4
Source File: PropertiesHelper.java    From wings with Apache License 2.0 5 votes vote down vote up
private static void addFileAppender(Logger logger, String id) {
	if (logger.getAppender(id) != null)
		return;

	String filename = getProposedLogFileName(id);
	try {
		FileAppender fileappender = new FileAppender(new SimpleLayout(), filename);
		fileappender.setAppend(false);
		fileappender.setName(id);
		logger.addAppender(fileappender);
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example 5
Source File: Log4JConfigurationTests.java    From dynamodb-cross-region-library with Apache License 2.0 5 votes vote down vote up
@Test
public void testShardSyncerMessage() throws ClassNotFoundException {
    Logger log = Logger.getRootLogger();
    Appender a = log.getAppender("CONSOLE");
    Filter f = a.getFilter();
    assertEquals(null, f.getNext());
    assertEquals(StringMatchFilter.class, f.getClass());
    assertEquals(Filter.NEUTRAL, a.getFilter().decide(new LoggingEvent(Logger.class.toString(), log, Level.ERROR, "TestMessage", null)));
    assertEquals(Filter.DENY, a.getFilter().decide(new LoggingEvent(Logger.class.toString(), log, Level.ERROR, SHARD_SYNC_MESSAGE, null)));
    assertEquals(Filter.DENY,
        a.getFilter().decide(new LoggingEvent(Logger.class.toString(), log, Level.ERROR, "Shard shard-1234-5432-1234 " + SHARD_SYNC_MESSAGE, null)));
}
 
Example 6
Source File: MongoRyaDirectExample.java    From rya with Apache License 2.0 5 votes vote down vote up
public static void setupLogging() {
    final Logger rootLogger = LogManager.getRootLogger();
    rootLogger.setLevel(Level.OFF);
    final ConsoleAppender ca = (ConsoleAppender) rootLogger.getAppender("stdout");
    ca.setLayout(new PatternLayout("%d{MMM dd yyyy HH:mm:ss} %5p [%t] (%F:%L) - %m%n"));
    rootLogger.setLevel(Level.INFO);
    // Filter out noisy messages from the following classes.
    Logger.getLogger(ClientCnxn.class).setLevel(Level.OFF);
    Logger.getLogger(EmbeddedMongoFactory.class).setLevel(Level.OFF);
}
 
Example 7
Source File: ProspectorExample.java    From rya with Apache License 2.0 5 votes vote down vote up
private static void setupLogging() {
    // Turn off all the loggers and customize how they write to the console.
    final Logger rootLogger = LogManager.getRootLogger();
    rootLogger.setLevel(Level.OFF);
    final ConsoleAppender ca = (ConsoleAppender) rootLogger.getAppender("stdout");
    ca.setLayout(new PatternLayout("%-5p - %m%n"));

    // Turn the logger used by the demo back on.
    log.setLevel(Level.INFO);
}
 
Example 8
Source File: RyaClientExample.java    From rya with Apache License 2.0 5 votes vote down vote up
private static void setupLogging() {
    // Turn off all the loggers and customize how they write to the console.
    final Logger rootLogger = LogManager.getRootLogger();
    rootLogger.setLevel(Level.OFF);
    final ConsoleAppender ca = (ConsoleAppender) rootLogger.getAppender("stdout");
    ca.setLayout(new PatternLayout("%-5p - %m%n"));


    // Turn the logger used by the demo back on.
    log.setLevel(Level.INFO);
    Logger.getLogger(ClientCnxn.class).setLevel(Level.ERROR);
}
 
Example 9
Source File: InferenceExamples.java    From rya with Apache License 2.0 5 votes vote down vote up
public static void setupLogging() {
    final Logger rootLogger = LogManager.getRootLogger();
    final ConsoleAppender ca = (ConsoleAppender) rootLogger.getAppender("stdout");
    ca.setLayout(new PatternLayout("%d{MMM dd yyyy HH:mm:ss} %5p [%t] (%F:%L) - %m%n"));
    rootLogger.setLevel(Level.INFO);
    // Filter out noisy messages from the following classes.
    Logger.getLogger(ClientCnxn.class).setLevel(Level.OFF);
    Logger.getLogger(EmbeddedMongoFactory.class).setLevel(Level.OFF);
}
 
Example 10
Source File: DemoUtilities.java    From rya with Apache License 2.0 5 votes vote down vote up
/**
 * Sets up log4j logging to fit the demo's needs.
 * @param loggingDetail the {@link LoggingDetail} to use.
 */
public static void setupLogging(final LoggingDetail loggingDetail) {
    // Turn off all the loggers and customize how they write to the console.
    final Logger rootLogger = LogManager.getRootLogger();
    rootLogger.setLevel(Level.OFF);
    final ConsoleAppender ca = (ConsoleAppender) rootLogger.getAppender("stdout");
    ca.setLayout(loggingDetail.getPatternLayout());

    // Turn the loggers used by the demo back on.
    //log.setLevel(Level.INFO);
    rootLogger.setLevel(Level.INFO);
}
 
Example 11
Source File: DemoDriver.java    From rya with Apache License 2.0 5 votes vote down vote up
private static void setupLogging() {
    // Turn off all the loggers and customize how they write to the console.
    final Logger rootLogger = LogManager.getRootLogger();
    rootLogger.setLevel(Level.OFF);
    final ConsoleAppender ca = (ConsoleAppender) rootLogger.getAppender("stdout");
    ca.setLayout(new PatternLayout("%-5p - %m%n"));


    // Turn the loggers used by the demo back on.
    log.setLevel(Level.INFO);
}
 
Example 12
Source File: LoggerUtilTest.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
private static void testAndRemoveAppender(Logger logger, String name)
{
  Appender appender = logger.getAppender(name);
  assertNotNull(appender);
  assertTrue(LoggerUtil.getAppendersNames(logger).contains(name));
  LoggerUtil.removeAppender(logger, name);
  assertNull(logger.getAppender(name));
}
 
Example 13
Source File: LoggerFactory.java    From development with Apache License 2.0 5 votes vote down vote up
private static void changeFileAppenderIfNew(Logger logger,
        String fileAppenderName, RollingFileAppender newFileAppender) {
    Appender existingFileAppender = logger.getAppender(fileAppenderName);
    if (existingFileAppender == null) {
        logger.removeAppender(consoleAppender);
        logger.addAppender(newFileAppender);
    } else if (existingFileAppender != newFileAppender) {
        logger.removeAppender(existingFileAppender);
        logger.addAppender(newFileAppender);
    }
    return;
}
 
Example 14
Source File: AbstractRipper.java    From ripme with MIT License 5 votes vote down vote up
/**
 * Notifies observers and updates state if all files have been ripped.
 */
void checkIfComplete() {
    if (observer == null) {
        LOGGER.debug("observer is null");
        return;
    }

    if (!completed) {
        completed = true;
        LOGGER.info("   Rip completed!");

        RipStatusComplete rsc = new RipStatusComplete(workingDir, getCount());
        RipStatusMessage msg = new RipStatusMessage(STATUS.RIP_COMPLETE, rsc);
        observer.update(this, msg);

        Logger rootLogger = Logger.getRootLogger();
        FileAppender fa = (FileAppender) rootLogger.getAppender("FILE");
        if (fa != null) {
            LOGGER.debug("Changing log file back to 'ripme.log'");
            fa.setFile("ripme.log");
            fa.activateOptions();
        }
        if (Utils.getConfigBoolean("urls_only.save", false)) {
            String urlFile = this.workingDir + File.separator + "urls.txt";
            try {
                Desktop.getDesktop().open(new File(urlFile));
            } catch (IOException e) {
                LOGGER.warn("Error while opening " + urlFile, e);
            }
        }
    }
}
 
Example 15
Source File: AbstractRipper.java    From ripme with MIT License 5 votes vote down vote up
/**
 * Sets ripper's:
 *      Working directory
 *      Logger (for debugging)
 *      FileAppender
 *      Threadpool
 * @throws IOException 
 *      Always be prepared.
 */
public void setup() throws IOException {
    setWorkingDir(this.url);
    Logger rootLogger = Logger.getRootLogger();
    FileAppender fa = (FileAppender) rootLogger.getAppender("FILE");
    if (fa != null) {
        fa.setFile(this.workingDir + File.separator + "log.txt");
        fa.activateOptions();
    }

    this.threadPool = new DownloadThreadPool();
}
 
Example 16
Source File: N4jsUiLoggingInitializer.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Initializes the log4j logging with an additional appender which routes the logging to the Eclipse ErrorView.
 */
public static void init() {
	Logger rootLogger = Logger.getRootLogger();

	// # This appender will write to the Eclipse error log. It will ONLY log ERROR and FATAL messages.
	// log4j.appender.eclipse=org.eclipse.xtext.logging.EclipseLogAppender
	EclipseLogAppender eclipseAppender = new EclipseLogAppender();
	eclipseAppender.setName("eclipse");
	eclipseAppender.setThreshold(Level.ERROR);
	rootLogger.addAppender(eclipseAppender);

	// log4j.appender.eclipse.layout=org.apache.log4j.PatternLayout
	// log4j.appender.eclipse.layout.ConversionPattern=%c %x - %m%n
	eclipseAppender.setLayout(new PatternLayout("%c %x - %m%n"));

	// # EclipseLogAppender must not log to the 'eclipse' appender as that would cause a stack overflow!
	// log4j.additivity.org.eclipse.xtext.logging.EclipseLogAppender=false
	// log4j.logger.org.eclipse.xtext.logging.EclipseLogAppender=WARN, default
	Logger eclipseAppenderLogger = Logger.getLogger(EclipseLogAppender.class);
	eclipseAppenderLogger.setAdditivity(false);
	eclipseAppenderLogger.setLevel(Level.WARN);
	Appender defaultAppender = eclipseAppenderLogger.getAppender("default");
	if (eclipseAppenderLogger.getAllAppenders().hasMoreElements()) {
		eclipseAppenderLogger.removeAllAppenders();
	}
	if (defaultAppender != null) {
		eclipseAppenderLogger.addAppender(defaultAppender);
	}
}
 
Example 17
Source File: HttpRequestLog.java    From big-c with Apache License 2.0 4 votes vote down vote up
public static RequestLog getRequestLog(String name) {

    String lookup = serverToComponent.get(name);
    if (lookup != null) {
      name = lookup;
    }
    String loggerName = "http.requests." + name;
    String appenderName = name + "requestlog";
    Log logger = LogFactory.getLog(loggerName);

    boolean isLog4JLogger;;
    try {
      isLog4JLogger = logger instanceof Log4JLogger;
    } catch (NoClassDefFoundError err) {
      // In some dependent projects, log4j may not even be on the classpath at
      // runtime, in which case the above instanceof check will throw
      // NoClassDefFoundError.
      LOG.debug("Could not load Log4JLogger class", err);
      isLog4JLogger = false;
    }
    if (isLog4JLogger) {
      Log4JLogger httpLog4JLog = (Log4JLogger)logger;
      Logger httpLogger = httpLog4JLog.getLogger();
      Appender appender = null;

      try {
        appender = httpLogger.getAppender(appenderName);
      } catch (LogConfigurationException e) {
        LOG.warn("Http request log for " + loggerName
            + " could not be created");
        throw e;
      }

      if (appender == null) {
        LOG.info("Http request log for " + loggerName
            + " is not defined");
        return null;
      }

      if (appender instanceof HttpRequestLogAppender) {
        HttpRequestLogAppender requestLogAppender
          = (HttpRequestLogAppender)appender;
        NCSARequestLog requestLog = new NCSARequestLog();
        requestLog.setFilename(requestLogAppender.getFilename());
        requestLog.setRetainDays(requestLogAppender.getRetainDays());
        return requestLog;
      }
      else {
        LOG.warn("Jetty request log for " + loggerName
            + " was of the wrong class");
        return null;
      }
    }
    else {
      LOG.warn("Jetty request log can only be enabled using Log4j");
      return null;
    }
  }
 
Example 18
Source File: HttpRequestLog.java    From hadoop with Apache License 2.0 4 votes vote down vote up
public static RequestLog getRequestLog(String name) {

    String lookup = serverToComponent.get(name);
    if (lookup != null) {
      name = lookup;
    }
    String loggerName = "http.requests." + name;
    String appenderName = name + "requestlog";
    Log logger = LogFactory.getLog(loggerName);

    boolean isLog4JLogger;;
    try {
      isLog4JLogger = logger instanceof Log4JLogger;
    } catch (NoClassDefFoundError err) {
      // In some dependent projects, log4j may not even be on the classpath at
      // runtime, in which case the above instanceof check will throw
      // NoClassDefFoundError.
      LOG.debug("Could not load Log4JLogger class", err);
      isLog4JLogger = false;
    }
    if (isLog4JLogger) {
      Log4JLogger httpLog4JLog = (Log4JLogger)logger;
      Logger httpLogger = httpLog4JLog.getLogger();
      Appender appender = null;

      try {
        appender = httpLogger.getAppender(appenderName);
      } catch (LogConfigurationException e) {
        LOG.warn("Http request log for " + loggerName
            + " could not be created");
        throw e;
      }

      if (appender == null) {
        LOG.info("Http request log for " + loggerName
            + " is not defined");
        return null;
      }

      if (appender instanceof HttpRequestLogAppender) {
        HttpRequestLogAppender requestLogAppender
          = (HttpRequestLogAppender)appender;
        NCSARequestLog requestLog = new NCSARequestLog();
        requestLog.setFilename(requestLogAppender.getFilename());
        requestLog.setRetainDays(requestLogAppender.getRetainDays());
        return requestLog;
      }
      else {
        LOG.warn("Jetty request log for " + loggerName
            + " was of the wrong class");
        return null;
      }
    }
    else {
      LOG.warn("Jetty request log can only be enabled using Log4j");
      return null;
    }
  }
 
Example 19
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);
}