Java Code Examples for org.apache.log4j.FileAppender#setThreshold()

The following examples show how to use org.apache.log4j.FileAppender#setThreshold() . 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: RhnJavaJob.java    From uyuni with GNU General Public License v2.0 6 votes vote down vote up
void enableLogging(TaskoRun run) {
    PatternLayout pattern = new PatternLayout(DEFAULT_LOGGING_LAYOUT);
    try {
        getLogger().removeAllAppenders();
        FileAppender outLogAppender = new FileAppender(pattern,
                run.buildStdOutputLogPath());
        outLogAppender.setThreshold(Level.INFO);
        getLogger().addAppender(outLogAppender);
        FileAppender errLogAppender = new FileAppender(pattern,
                run.buildStdErrorLogPath());
        errLogAppender.setThreshold(Level.ERROR);
        getLogger().addAppender(errLogAppender);
    }
    catch (IOException e) {
        getLogger().warn("Logging to file disabled");
        e.printStackTrace();
    }
}
 
Example 2
Source File: Action.java    From charles-rest with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Setup the Log4J logger for this action thread.
 * @return String path to log file
 * @throws IOException If there's something wrong with the FileAppender.
 */
private void setupLog4jForAction() throws IOException {
    String loggerName = "Action_" + this.id;
    org.apache.log4j.Logger log4jLogger = org.apache.log4j.Logger.getLogger("Action_" + this.id);
    String logRoot = System.getProperty("LOG_ROOT");
    if(logRoot == null) {
        logRoot = ".";
    }
    String logFilePath = logRoot + "/charles-rest/ActionsLogs/" + this.id + ".log";
    
    File logFile = new File(logFilePath);
    logFile.getParentFile().mkdirs();
    logFile.createNewFile();//you have to create the file yourself since FileAppender acts funny under linux if the file doesn't already exist.

    FileAppender fa = new FileAppender(new PatternLayout("%d %p - %m%n"), logFilePath);
    fa.setName(this.id + "_appender");
    fa.setThreshold(Level.DEBUG);
    log4jLogger.addAppender(fa);
    log4jLogger.setLevel(Level.DEBUG);
    
    this.logger = LoggerFactory.getLogger(loggerName);
    
}
 
Example 3
Source File: Test.java    From cpsolver with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Setup log4j logging
 * 
 * @param logFile  log file
 * @param debug true if debug messages should be logged (use -Ddebug=true to enable debug message)
 */
public static void setupLogging(File logFile, boolean debug) {
    Logger root = Logger.getRootLogger();
    ConsoleAppender console = new ConsoleAppender(new PatternLayout("[%t] %m%n"));
    console.setThreshold(Level.INFO);
    root.addAppender(console);
    if (logFile != null) {
        try {
            FileAppender file = new FileAppender(new PatternLayout("%d{dd-MMM-yy HH:mm:ss.SSS} [%t] %-5p %c{2}> %m%n"), logFile.getPath(), false);
            file.setThreshold(Level.DEBUG);
            root.addAppender(file);
        } catch (IOException e) {
            sLogger.fatal("Unable to configure logging, reason: " + e.getMessage(), e);
        }
    }
    if (!debug)
        root.setLevel(Level.INFO);
}
 
Example 4
Source File: Test.java    From cpsolver with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Setup log4j logging
 * 
 * @param logFile
 *            log file
 * @param debug
 *            true if debug messages should be logged (use -Ddebug=true to
 *            enable debug message)
 */
public static void setupLogging(File logFile, boolean debug) {
    Logger root = Logger.getRootLogger();
    ConsoleAppender console = new ConsoleAppender(new PatternLayout("[%t] %m%n"));
    console.setThreshold(Level.INFO);
    root.addAppender(console);
    if (logFile != null) {
        try {
            FileAppender file = new FileAppender(new PatternLayout(
                    "%d{dd-MMM-yy HH:mm:ss.SSS} [%t] %-5p %c{2}> %m%n"), logFile.getPath(), false);
            file.setThreshold(Level.DEBUG);
            root.addAppender(file);
        } catch (IOException e) {
            sLog.fatal("Unable to configure logging, reason: " + e.getMessage(), e);
        }
    }
    if (!debug)
        root.setLevel(Level.INFO);
}
 
Example 5
Source File: Test.java    From cpsolver with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Setup log4j logging
 * 
 * @param logFile  log file
 */
public static void setupLogging(File logFile) {
    Logger root = Logger.getRootLogger();
    ConsoleAppender console = new ConsoleAppender(new PatternLayout("[%t] %m%n"));
    console.setThreshold(Level.INFO);
    root.addAppender(console);
    if (logFile != null) {
        try {
            FileAppender file = new FileAppender(new PatternLayout("%d{dd-MMM-yy HH:mm:ss.SSS} [%t] %-5p %c{2}> %m%n"), logFile.getPath(), false);
            file.setThreshold(Level.DEBUG);
            root.addAppender(file);
        } catch (IOException e) {
            sLog.fatal("Unable to configure logging, reason: " + e.getMessage(), e);
        }
    }
}
 
Example 6
Source File: Logging.java    From swift-t with Apache License 2.0 6 votes vote down vote up
private static void setupLoggingToFile(Logger stcLogger, String logfile,
    boolean trace) {
  Layout layout = new PatternLayout("%-5p %m%n");
  boolean append = false;
  try {
    FileAppender appender = new FileAppender(layout, logfile, append);
    Level threshold;
    if (trace) {
      threshold = Level.TRACE;
    } else {
      threshold = Level.DEBUG;
    }
    appender.setThreshold(threshold);
    stcLogger.addAppender(appender);
    stcLogger.setLevel(threshold);
  } catch (IOException e) {
    System.out.println(e.getMessage());
    System.exit(ExitCode.ERROR_IO.code());
  }
}
 
Example 7
Source File: RhnJavaJob.java    From spacewalk with GNU General Public License v2.0 6 votes vote down vote up
void enableLogging(TaskoRun run) {
    PatternLayout pattern = new PatternLayout(DEFAULT_LOGGING_LAYOUT);
    try {
        getLogger().removeAllAppenders();
        FileAppender outLogAppender = new FileAppender(pattern,
                run.buildStdOutputLogPath());
        outLogAppender.setThreshold(Level.INFO);
        getLogger().addAppender(outLogAppender);
        FileAppender errLogAppender = new FileAppender(pattern,
                run.buildStdErrorLogPath());
        errLogAppender.setThreshold(Level.ERROR);
        getLogger().addAppender(errLogAppender);
    }
    catch (IOException e) {
        getLogger().warn("Logging to file disabled");
        e.printStackTrace();
    }
}
 
Example 8
Source File: JackrabbitMain.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
private void prepareServerLog(final File log) throws IOException {
    final Layout layout = new PatternLayout("%d{dd.MM.yyyy HH:mm:ss} *%-5p* %c{1}: %m%n");

    final Logger jackrabbitLog = Logger.getRootLogger();
    jackrabbitAppender = new FileAppender(layout, new File(log, "jackrabbit.log").getPath());
    jackrabbitAppender.setThreshold(Level.ALL);
    jackrabbitLog.addAppender(jackrabbitAppender);

    final Logger jettyLog = Logger.getLogger("org.mortbay.log");
    jettyAppender = new FileAppender(layout, new File(log, "jetty.log").getPath());
    jettyAppender.setThreshold(Level.ALL);
    jettyLog.addAppender(jettyAppender);
    jettyLog.setAdditivity(false);

    System.setProperty("derby.stream.error.file", new File(log, "derby.log").getPath());
}
 
Example 9
Source File: MainWindow.java    From ripme with MIT License 5 votes vote down vote up
private void setLogLevel(String level) {
    Level newLevel = Level.ERROR;
    level = level.substring(level.lastIndexOf(' ') + 1);
    switch (level) {
    case "Debug":
        newLevel = Level.DEBUG;
        break;
    case "Info":
        newLevel = Level.INFO;
        break;
    case "Warn":
        newLevel = Level.WARN;
        break;
    case "Error":
        newLevel = Level.ERROR;
        break;
    }
    Logger.getRootLogger().setLevel(newLevel);
    LOGGER.setLevel(newLevel);
    ConsoleAppender ca = (ConsoleAppender) Logger.getRootLogger().getAppender("stdout");
    if (ca != null) {
        ca.setThreshold(newLevel);
    }
    FileAppender fa = (FileAppender) Logger.getRootLogger().getAppender("FILE");
    if (fa != null) {
        fa.setThreshold(newLevel);
    }
}
 
Example 10
Source File: MatrixRunner.java    From KITE with Apache License 2.0 5 votes vote down vote up
/**
 * Create a common test logger for all test cases of a given test
 *
 * @return the logger for tests
 */
private KiteLogger createTestLogger(String kiteRequestId, String testName) {
  KiteLogger testLogger = KiteLogger.getLogger(new SimpleDateFormat("yyyy-MM-dd-HHmmss").format(new Date()));
  String logFileName = ((kiteRequestId == null || kiteRequestId.equals("null")) ? 
    "" : (kiteRequestId + "_")) + testName + "/test_" + testLogger.getName() + ".log";
  try {
    FileAppender fileAppender = new FileAppender(
      new PatternLayout("%d %-5p - %m%n"), "logs/" + logFileName, false);    
    fileAppender.setThreshold(Level.INFO);
    testLogger.addAppender(fileAppender);
  } catch (IOException e) {
    logger.error(getStackTrace(e));
  }
  return testLogger;
}
 
Example 11
Source File: AstorMain.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void setupLogging() throws IOException {

		String patternLayout = "";
		if (ConfigurationProperties.getPropertyBool("disablelog")) {
			patternLayout = "%m%n";
		} else {
			patternLayout = ConfigurationProperties.getProperty("logpatternlayout");
		}

		Logger.getRootLogger().getLoggerRepository().resetConfiguration();
		ConsoleAppender console = new ConsoleAppender();
		console.setLayout(new PatternLayout(patternLayout));
		console.activateOptions();
		Logger.getRootLogger().addAppender(console);

		String loglevelSelected = ConfigurationProperties.properties.getProperty("loglevel");
		if (loglevelSelected != null)
			LogManager.getRootLogger().setLevel(Level.toLevel(loglevelSelected));

		if (ConfigurationProperties.hasProperty("logfilepath")) {
			FileAppender fa = new FileAppender();
			String filePath = ConfigurationProperties.getProperty("logfilepath");
			File fileLog = new File(filePath);
			if (!fileLog.exists()) {
				fileLog.getParentFile().mkdirs();
				fileLog.createNewFile();
			}

			fa.setName("FileLogger");
			fa.setFile(fileLog.getAbsolutePath());
			fa.setLayout(new PatternLayout(patternLayout));
			fa.setThreshold(LogManager.getRootLogger().getLevel());
			fa.setAppend(true);
			fa.activateOptions();
			Logger.getRootLogger().addAppender(fa);
			this.log.info("Log file at: " + filePath);
		}
	}