Java Code Examples for org.apache.log4j.RollingFileAppender

The following examples show how to use org.apache.log4j.RollingFileAppender. These examples are extracted from open source projects. 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 Project: Bats   Source File: StdOutErrLog.java    License: 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 2
Source Project: hadoop   Source File: TestAuditLogs.java    License: 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 Project: big-c   Source File: TestAuditLogs.java    License: 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 4
Source Project: attic-apex-core   Source File: StdOutErrLog.java    License: 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
@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 6
Source Project: astor   Source File: BaseEvolutionaryTest.java    License: 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 7
Source Project: hadoop   Source File: TestFsck.java    License: 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 8
/**
 * 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 9
Source Project: big-c   Source File: TestFsck.java    License: 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 10
Source Project: development   Source File: LoggerFactory.java    License: 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 11
Source Project: development   Source File: LoggerFactory.java    License: 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 12
Source Project: attic-apex-core   Source File: LogFileInformationTest.java    License: 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 13
Source Project: attic-apex-core   Source File: LogFileInformationTest.java    License: 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 14
Source Project: attic-apex-core   Source File: LogFileInformationTest.java    License: 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 15
Source Project: attic-apex-core   Source File: LogFileInformationTest.java    License: 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
@Override
protected void apply() {
    RollingFileAppender appender = appenderCache.get(key);
    if (appender != null) {
        appender.append(event);
    }
}
 
Example 17
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 18
@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 19
Source Project: astor   Source File: BaseEvolutionaryTest.java    License: 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 20
Source Project: jqm   Source File: EngineCallback.java    License: 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 21
Source Project: jqm   Source File: CommonService.java    License: 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 22
Source Project: olca-app   Source File: HtmlLogFile.java    License: 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 23
Source Project: sailfish-core   Source File: AbstractScriptRunner.java    License: 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());
}