org.apache.log4j.RollingFileAppender Java Examples

The following examples show how to use org.apache.log4j.RollingFileAppender. 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: TestAuditLogs.java    From hadoop with Apache License 2.0 6 votes vote down vote up
private void configureAuditLogs() throws IOException {
  // Shutdown the LogManager to release all logger open file handles.
  // Unfortunately, Apache commons logging library does not provide
  // means to release underlying loggers. For additional info look up
  // commons library FAQ.
  LogManager.shutdown();

  File file = new File(auditLogFile);
  if (file.exists()) {
    assertTrue(file.delete());
  }
  Logger logger = ((Log4JLogger) FSNamesystem.auditLog).getLogger();
  // disable logging while the cluster startup preps files
  logger.setLevel(Level.OFF);
  PatternLayout layout = new PatternLayout("%m%n");
  RollingFileAppender appender = new RollingFileAppender(layout, auditLogFile);
  logger.addAppender(appender);
}
 
Example #2
Source File: TestAuditLogs.java    From big-c with Apache License 2.0 6 votes vote down vote up
private void configureAuditLogs() throws IOException {
  // Shutdown the LogManager to release all logger open file handles.
  // Unfortunately, Apache commons logging library does not provide
  // means to release underlying loggers. For additional info look up
  // commons library FAQ.
  LogManager.shutdown();

  File file = new File(auditLogFile);
  if (file.exists()) {
    assertTrue(file.delete());
  }
  Logger logger = ((Log4JLogger) FSNamesystem.auditLog).getLogger();
  // disable logging while the cluster startup preps files
  logger.setLevel(Level.OFF);
  PatternLayout layout = new PatternLayout("%m%n");
  RollingFileAppender appender = new RollingFileAppender(layout, auditLogFile);
  logger.addAppender(appender);
}
 
Example #3
Source File: BaseEvolutionaryTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public Logger createCustomFileLogger(String file) throws IOException {
	// ----
	ConsoleAppender console = new ConsoleAppender();
	String PATTERN = "%m%n-%c: ";
	console.setLayout(new PatternLayout(PATTERN));
	console.setThreshold(Level.INFO);
	console.activateOptions();
	Logger log = Logger.getLogger(Thread.currentThread().getName());

	log.getLoggerRepository().resetConfiguration();
	log.removeAllAppenders();
	log.addAppender(console);
	// ----
	org.apache.log4j.RollingFileAppender rfa = new RollingFileAppender(new org.apache.log4j.PatternLayout(PATTERN),
			file);
	log.addAppender(rfa);
	rfa.setImmediateFlush(true);

	return log;

}
 
Example #4
Source File: StdOutErrLog.java    From attic-apex-core with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("UseOfSystemOutOrSystemErr")
public static void tieSystemOutAndErrToLog()
{

  org.apache.log4j.Logger rootLogger = org.apache.log4j.Logger.getRootLogger();
  Appender appender = rootLogger.getAppender(DT_LOG_APPENDER);
  if (appender instanceof RollingFileAppender) {
    RollingFileAppender rfa = (RollingFileAppender)appender;
    if (rfa.getFile() == null || rfa.getFile().isEmpty()) {
      rfa.setFile(System.getProperty(DT_LOGDIR));
      rfa.activateOptions();
    }
  } else if (appender != null) {
    logger.warn("found appender {} instead of RollingFileAppender", appender);
  }

  LoggerUtil.addAppenders();
  System.setOut(createLoggingProxy(System.out));
  System.setErr(createLoggingProxy(System.err));
}
 
Example #5
Source File: StdOutErrLog.java    From Bats with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("UseOfSystemOutOrSystemErr")
public static void tieSystemOutAndErrToLog()
{

  org.apache.log4j.Logger rootLogger = org.apache.log4j.Logger.getRootLogger();
  Appender appender = rootLogger.getAppender(DT_LOG_APPENDER);
  if (appender instanceof RollingFileAppender) {
    RollingFileAppender rfa = (RollingFileAppender)appender;
    if (rfa.getFile() == null || rfa.getFile().isEmpty()) {
      rfa.setFile(System.getProperty(DT_LOGDIR));
      rfa.activateOptions();
    }
  } else if (appender != null) {
    logger.warn("found appender {} instead of RollingFileAppender", appender);
  }

  LoggerUtil.addAppenders();
  System.setOut(createLoggingProxy(System.out));
  System.setErr(createLoggingProxy(System.err));
}
 
Example #6
Source File: AsynchChachedFileAppender.java    From scheduling with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void close() {
    try {
        isClosing.lock();
        super.flush();
        Optional<String> opKey = extractKey();
        if (opKey.isPresent()) {
            RollingFileAppender appender = appenderCache.remove(opKey.get());
            if (appender != null) {
                appender.close();
            }
        }
    } finally {
        isClosing.unlock();
    }
}
 
Example #7
Source File: LogFileInformationTest.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
@Test
public void testImmediateFlushOff()
{
  RollingFileAppender rfa = (RollingFileAppender)logger.getAppender(APPENDER_NAME);
  assertTrue(rfa.getImmediateFlush());
  rfa.setImmediateFlush(false);
  assertNull(LoggerUtil.getLogFileInformation());
  rfa.setImmediateFlush(true);
}
 
Example #8
Source File: HtmlLogFile.java    From olca-app with Mozilla Public License 2.0 5 votes vote down vote up
private static WriterAppender createAppender(File logFile)
		throws IOException {
	HTMLLayout layout = new HTMLLayout();
	RollingFileAppender app = new RollingFileAppender(layout,
			logFile.getAbsolutePath(), true);
	app.setMaxFileSize("3MB");
	app.setMaxBackupIndex(3);
	return app;
}
 
Example #9
Source File: CommonService.java    From jqm with Apache License 2.0 5 votes vote down vote up
static void setLogFileName(String name)
{
    Appender a = Logger.getRootLogger().getAppender("rollingfile");
    if (a == null)
    {
        return;
    }
    RollingFileAppender r = (RollingFileAppender) a;
    r.setFile("./logs/jqm-" + name + ".log");
    r.activateOptions();
}
 
Example #10
Source File: EngineCallback.java    From jqm with Apache License 2.0 5 votes vote down vote up
@Override
public void onNodeConfigurationRead(Node node)
{
    DbConn cnx = Helpers.getNewDbSession();

    // Main log levels comes from configuration
    CommonService.setLogLevel(node.getRootLogLevel());
    this.logLevel = node.getRootLogLevel();

    // Log multicasting (& log4j stdout redirect)
    String gp1 = GlobalParameter.getParameter(cnx, "logFilePerLaunch", "true");
    if ("true".equals(gp1) || "both".equals(gp1))
    {
        oneLogPerLaunch = true;
        RollingFileAppender a = (RollingFileAppender) Logger.getRootLogger().getAppender("rollingfile");
        MultiplexPrintStream s = new MultiplexPrintStream(System.out, FilenameUtils.getFullPath(a.getFile()), "both".equals(gp1));
        System.setOut(s);
        ((ConsoleAppender) Logger.getRootLogger().getAppender("consoleAppender")).setWriter(new OutputStreamWriter(s));
        s = new MultiplexPrintStream(System.err, FilenameUtils.getFullPath(a.getFile()), "both".equals(gp1));
        System.setErr(s);
    }

    // Jetty
    this.server = new JettyServer();
    this.server.start(node, cnx);

    // Deployment scanner
    String gp2 = GlobalParameter.getParameter(cnx, "directoryScannerRoot", "");
    if (!gp2.isEmpty())
    {
        scanner = new DirectoryScanner(gp2, node);
        (new Thread(scanner)).start();
    }

    cnx.close();
}
 
Example #11
Source File: BaseEvolutionaryTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void createFileLogger(String file) throws IOException {
	// ----
	ConsoleAppender console = new ConsoleAppender();
	String PATTERN = "%m%n";
	console.setLayout(new PatternLayout(PATTERN));
	console.setThreshold(Level.INFO);
	console.activateOptions();
	Logger.getRootLogger().getLoggerRepository().resetConfiguration();
	Logger.getRootLogger().removeAllAppenders();
	Logger.getRootLogger().addAppender(console);
	// ----
	org.apache.log4j.RollingFileAppender rfa = new RollingFileAppender(new org.apache.log4j.PatternLayout(), file);
	Logger.getRootLogger().addAppender(rfa);

}
 
Example #12
Source File: LoggingConfiguratorTest.java    From selenium-grid-extensions with Apache License 2.0 5 votes vote down vote up
@Test
public void configure_shouldInstallFileAppenderFromArguments() throws Exception {

    String[] arguments = loggingConfigurator.configure(new String[]{"-log", "target/" + LOG_FILENAME});
    assertEquals(0, arguments.length);
    Appender appender = Logger.getRootLogger().getAppender("FileAppender");
    assertNotNull(appender);
    assertThat(appender, instanceOf(RollingFileAppender.class));
    assertThat(((RollingFileAppender) appender).getFile(), endsWith(LOG_FILENAME));
    assertThat(arguments.length, is(0));
}
 
Example #13
Source File: LoggingConfigurator.java    From selenium-grid-extensions with Apache License 2.0 5 votes vote down vote up
private void installFileAppender(String logFilename) {
    RollingFileAppender file = new RollingFileAppender();
    file.setName("FileAppender");
    file.setThreshold(logLevel);
    file.setFile(new File(logFilename).getAbsolutePath());
    file.setMaxFileSize(MAX_FILE_SIZE);
    file.setMaxBackupIndex(MAX_BACKUPS);
    file.setLayout(new PatternLayout(PATTERN));
    file.setAppend(true);
    file.activateOptions();

    Logger.getRootLogger().addAppender(file);
}
 
Example #14
Source File: AsynchChachedFileAppender.java    From scheduling with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected void apply() {
    RollingFileAppender appender = appenderCache.get(key);
    if (appender != null) {
        appender.append(event);
    }
}
 
Example #15
Source File: LogFileInformationTest.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoFileAppender()
{
  RollingFileAppender rfa = (RollingFileAppender)logger.getAppender(APPENDER_NAME);
  logger.removeAppender(APPENDER_NAME);
  assertNull(LoggerUtil.getLogFileInformation());
  logger.addAppender(rfa);
}
 
Example #16
Source File: LogFileInformationTest.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
@Test
public void testErrorLevelOff()
{
  RollingFileAppender rfa = (RollingFileAppender)logger.getAppender(APPENDER_NAME);
  assertNull(rfa.getThreshold());
  rfa.setThreshold(Level.FATAL);
  assertNull(LoggerUtil.getLogFileInformation(logger));
  rfa.setThreshold(null);
}
 
Example #17
Source File: LogFileInformationTest.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void beforeClass() throws IOException
{
  String logFileDir = new File("target" + File.separator + "logDir").getAbsolutePath();
  logFileName = logFileDir + File.separator + "appTest.log";
  RollingFileAppender rfa = new RollingFileAppender(new PatternLayout("%d{ISO8601} [%t] %-5p %c{2} %M - %m%n"),
      logFileName);
  rfa.setName(APPENDER_NAME);
  logger.addAppender(rfa);
}
 
Example #18
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 #19
Source File: LoggerFactory.java    From development with Apache License 2.0 5 votes vote down vote up
private static void initAppenders() throws IOException {
    systemLogAppender = new RollingFileAppender(getLayout(), logFilePath
            + File.separatorChar + "system.log");
    accessLogAppender = new RollingFileAppender(getLayout(), logFilePath
            + File.separatorChar + "access.log");
    auditLogAppender = new RollingFileAppender(getLayout(), logFilePath
            + File.separatorChar + "audit.log");
    reverseProxyLogAppender = new RollingFileAppender(getLayout(),
            logFilePath + File.separatorChar + "reverseproxy.log");

    // setting the max backup index and file size
    systemLogAppender.setMaxBackupIndex(MAX_BACKUP_INDEX);
    systemLogAppender.setMaxFileSize(MAX_FILE_SIZE);
    systemLogAppender.setName(systemLogAppenderName);

    accessLogAppender.setMaxBackupIndex(MAX_BACKUP_INDEX);
    accessLogAppender.setMaxFileSize(MAX_FILE_SIZE);
    accessLogAppender.setName(accessLogAppenderName);

    auditLogAppender.setMaxBackupIndex(MAX_BACKUP_INDEX);
    auditLogAppender.setMaxFileSize(MAX_FILE_SIZE);
    auditLogAppender.setName(auditLogAppenderName);

    reverseProxyLogAppender.setMaxBackupIndex(MAX_BACKUP_INDEX);
    reverseProxyLogAppender.setMaxFileSize(MAX_FILE_SIZE);
    reverseProxyLogAppender.setName(reverseProxyLogAppenderName);
}
 
Example #20
Source File: TestFsck.java    From big-c with Apache License 2.0 5 votes vote down vote up
/** Sets up log4j logger for auditlogs */
private void setupAuditLogs() throws IOException {
  File file = new File(auditLogFile);
  if (file.exists()) {
    file.delete();
  }
  Logger logger = ((Log4JLogger) FSNamesystem.auditLog).getLogger();
  logger.setLevel(Level.INFO);
  PatternLayout layout = new PatternLayout("%m%n");
  RollingFileAppender appender = new RollingFileAppender(layout, auditLogFile);
  logger.addAppender(appender);
}
 
Example #21
Source File: DefaultEbicsLogger.java    From ebics-java-client with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Constructs a new ebics logger with a given file
 * @param logFile the log file
 */
public DefaultEbicsLogger(File logFile) {
  this.logFile = logFile;
  logger = Logger.getLogger(DefaultEbicsLogger.class);
  consoleAppender = new ConsoleAppender();
  fileAppender = new RollingFileAppender();
}
 
Example #22
Source File: TestFsck.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/** Sets up log4j logger for auditlogs */
private void setupAuditLogs() throws IOException {
  File file = new File(auditLogFile);
  if (file.exists()) {
    file.delete();
  }
  Logger logger = ((Log4JLogger) FSNamesystem.auditLog).getLogger();
  logger.setLevel(Level.INFO);
  PatternLayout layout = new PatternLayout("%m%n");
  RollingFileAppender appender = new RollingFileAppender(layout, auditLogFile);
  logger.addAppender(appender);
}
 
Example #23
Source File: AbstractScriptRunner.java    From sailfish-core with Apache License 2.0 3 votes vote down vote up
private Logger createScriptLogger(ScriptSettings scriptSettings, String reportFolder) throws IOException, WorkspaceStructureException {
    org.apache.log4j.Logger scriptLogger = org.apache.log4j.Logger.getLogger("TestScript_" + RandomStringUtils.randomAlphanumeric(10));
    scriptLogger.removeAllAppenders();

    PatternLayout layout = new PatternLayout(scriptSettings.getFileLayout());

    RollingFileAppender fileAppender = new RollingFileAppender(layout, workspaceDispatcher.createFile(FolderType.REPORT, true, reportFolder, "script.log").getPath());

    fileAppender.setName("TESTSCRIPTFILEAPPENDER");

    fileAppender.setThreshold(Level.toLevel(scriptSettings.getFileLoggerLevel()));

    fileAppender.activateOptions();

    HTMLLayout htmlLayout = new HTMLLayout();

    RollingFileAppender htmlFileAppender = new RollingFileAppender(htmlLayout, workspaceDispatcher.createFile(FolderType.REPORT, true, reportFolder, "scriptlog.html").getPath());

    htmlFileAppender.setName("HTMLTESTSCRIPTFILEAPPENDER");

    htmlFileAppender.setThreshold(Level.toLevel(scriptSettings.getFileLoggerLevel()));

    PatternLayout conLayout = new PatternLayout(scriptSettings.getConsoleLayout());

    ConsoleAppender conAppender = new ConsoleAppender(conLayout);
    conAppender.setThreshold(Level.toLevel(scriptSettings.getConsoleLoggerLevel()));
    conAppender.activateOptions();

    scriptLogger.addAppender(fileAppender);
    scriptLogger.addAppender(conAppender);
    scriptLogger.addAppender(htmlFileAppender);

    return LoggerFactory.getLogger(scriptLogger.getName());
}