Java Code Examples for org.apache.log4j.ConsoleAppender#activateOptions()

The following examples show how to use org.apache.log4j.ConsoleAppender#activateOptions() . 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 yb-sample-apps with Apache License 2.0 6 votes vote down vote up
public static void configureLogLevel(boolean verbose) {
  // First remove all appenders.
  Logger.getLogger("com.yugabyte.sample").removeAppender("YBConsoleLogger");
  Logger.getRootLogger().removeAppender("YBConsoleLogger");;

  // Create the console appender.
  ConsoleAppender console = new ConsoleAppender();
  console.setName("YBConsoleLogger");
  String PATTERN = "%d [%p|%c|%C{1}] %m%n";
  console.setLayout(new PatternLayout(PATTERN));
  console.setThreshold(verbose ? Level.DEBUG : Level.INFO);
  console.activateOptions();

  // Set the desired logging level.
  if (verbose) {
    // If verbose, make everything DEBUG log level and output to console.
    Logger.getRootLogger().addAppender(console);
    Logger.getRootLogger().setLevel(Level.DEBUG);
  } else {
    // If not verbose, allow YB sample app and driver INFO logs go to console.
    Logger.getLogger("com.yugabyte.sample").addAppender(console);
    Logger.getLogger("com.yugabyte.driver").addAppender(console);
    Logger.getLogger("com.datastax.driver").addAppender(console);
    Logger.getRootLogger().setLevel(Level.WARN);
  }
}
 
Example 2
Source File: LogUtil.java    From lumongo with Apache License 2.0 6 votes vote down vote up
public static void loadLogConfig() throws Exception {
	synchronized (lock) {
		
		if (!loaded) {
			ConsoleAppender console = new ConsoleAppender(); //create appender
			//configure the appender
			String PATTERN = "%d [%t] <%p> %c{2}: %m%n";
			console.setLayout(new PatternLayout(PATTERN));
			console.setThreshold(Level.INFO);
			console.activateOptions();
			//add appender to any Logger (here is root)
			Logger.getRootLogger().addAppender(console);
			
			//String propPath = "/etc/loglevel.properties";
			//URL url = LogUtil.class.getResource(propPath);
			//if (url == null) {
			//	throw new Exception("Cannot find log properties file: " + propPath);
			//}
			//PropertyConfigurator.configure(url);
			loaded = true;
		}
	}
	
}
 
Example 3
Source File: PatternMatchingTest.java    From coming with MIT License 6 votes vote down vote up
@Before
public void setUp() throws Exception {

	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().addAppender(console);

	File s = getFile("patterns_examples/case1/1205753_EmbedPooledConnection_0_s.java");
	File t = getFile("patterns_examples/case1/1205753_EmbedPooledConnection_0_t.java");
	FineGrainDifftAnalyzer r = new FineGrainDifftAnalyzer();
	diffUpdate = r.getDiff(s, t);
	System.out.println("Output: " + diffUpdate);
	Assert.assertEquals(1, diffUpdate.getRootOperations().size());

	s = getFile("patterns_examples/case2/1205753_EmbedPooledConnection_0_s.java");
	t = getFile("patterns_examples/case2/1205753_EmbedPooledConnection_0_t.java");
	diffInsert = r.getDiff(s, t);
	System.out.println("Output: " + diffInsert);
	Assert.assertEquals(1, diffInsert.getRootOperations().size());

}
 
Example 4
Source File: BaseTest.java    From AndroidMvc with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void beforeClass() {
    ConsoleAppender console = new ConsoleAppender(); //create appender
    //configure the appender
    String PATTERN = "%d [%p] %C{1}: %m%n";
    console.setLayout(new PatternLayout(PATTERN));
    console.setThreshold(Level.DEBUG);
    console.activateOptions();
    //add appender to any Logger (here is root)
    Logger.getRootLogger().addAppender(console);
}
 
Example 5
Source File: DGALoggingUtil.java    From distributed-graph-analytics with Apache License 2.0 5 votes vote down vote up
public static void setDGALogLevel(String logLevel) {
    //System.out.println("Setting DGA Log level to: " + logLevel);
    Level level = Level.toLevel(logLevel, Level.INFO);
    ConsoleAppender console = new ConsoleAppender();
    //configure the appender
    String pattern = "%d [%p|%c] %m%n";
    console.setLayout(new PatternLayout(pattern));
    console.setThreshold(Level.DEBUG);
    console.activateOptions();
    //add appender to any Logger
    Logger.getLogger("com.soteradefense").addAppender(console);
    Logger.getLogger("com.soteradefense").setLevel(level);
}
 
Example 6
Source File: CoasterPersistentService.java    From swift-k with Apache License 2.0 5 votes vote down vote up
private static void disableConsoleLogging() {
	ConsoleAppender ca = (ConsoleAppender) getAppender(ConsoleAppender.class);
    if (ca == null) {
        logger.warn("Failed to configure console log level");
    }
    else {
        ca.setThreshold(Level.WARN);
        ca.activateOptions();
    }
}
 
Example 7
Source File: SketchCore.java    From arduino-remote-uploader with GNU General Public License v2.0 5 votes vote down vote up
protected static void initLog4j() {
	  ConsoleAppender console = new ConsoleAppender();
	  String PATTERN = "%d [%p|%c|%C{1}] %m%n";
	  console.setLayout(new PatternLayout(PATTERN)); 
	  console.activateOptions();
	  // only log this package
	  Logger.getLogger(SketchCore.class.getPackage().getName()).addAppender(console);
	  Logger.getLogger(SketchCore.class.getPackage().getName()).setLevel(Level.WARN);
	  Logger.getRootLogger().addAppender(console);
	  // quiet logger
	  Logger.getRootLogger().setLevel(Level.ERROR);
}
 
Example 8
Source File: UnitTestHelper.java    From metron with Apache License 2.0 5 votes vote down vote up
public static void verboseLogging(String pattern, Level level) {
  ConsoleAppender console = new ConsoleAppender(); //create appender
  //configure the appender
  console.setLayout(new PatternLayout(pattern));
  console.setThreshold(level);
  console.activateOptions();
  //add appender to any Logger (here is root)
  Logger.getRootLogger().addAppender(console);
}
 
Example 9
Source File: UnitTestHelper.java    From metron with Apache License 2.0 5 votes vote down vote up
public static void verboseLogging(String pattern, Level level) {
  ConsoleAppender console = new ConsoleAppender(); //create appender
  //configure the appender
  console.setLayout(new PatternLayout(pattern));
  console.setThreshold(level);
  console.activateOptions();
  //add appender to any Logger (here is root)
  Logger.getRootLogger().addAppender(console);
}
 
Example 10
Source File: BaseTest.java    From AndroidMvc with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void beforeClass() {
    ConsoleAppender console = new ConsoleAppender(); //create appender
    //configure the appender
    String PATTERN = "%d [%p] %C{1}: %m%n";
    console.setLayout(new PatternLayout(PATTERN));
    console.setThreshold(Level.DEBUG);
    console.activateOptions();
    //add appender to any Logger (here is root)
    Logger.getRootLogger().addAppender(console);
}
 
Example 11
Source File: LoggerConfig.java    From olca-app with Mozilla Public License 2.0 5 votes vote down vote up
private static void addConsoleOutput(Logger logger) {
	BasicConfigurator.configure();
	ConsoleAppender appender = new ConsoleAppender(new PatternLayout());
	logger.addAppender(appender);
	appender.setTarget(ConsoleAppender.SYSTEM_OUT);
	appender.activateOptions();
}
 
Example 12
Source File: GadgetInspector.java    From gadgetinspector with MIT License 5 votes vote down vote up
private static void configureLogging() {
    ConsoleAppender console = new ConsoleAppender();
    String PATTERN = "%d %c [%p] %m%n";
    console.setLayout(new PatternLayout(PATTERN));
    console.setThreshold(Level.DEBUG);
    console.activateOptions();
    org.apache.log4j.Logger.getRootLogger().addAppender(console);
}
 
Example 13
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 14
Source File: ExampleHelpers.java    From Wikidata-Toolkit-Examples with Apache License 2.0 5 votes vote down vote up
/**
 * Defines how messages should be logged. This method can be modified to
 * restrict the logging messages that are shown on the console or to change
 * their formatting. See the documentation of Log4J for details on how to do
 * this.
 */
public static void configureLogging() {
	// Create the appender that will write log messages to the console.
	ConsoleAppender consoleAppender = new ConsoleAppender();
	// Define the pattern of log messages.
	// Insert the string "%c{1}:%L" to also show class name and line.
	String pattern = "%d{yyyy-MM-dd HH:mm:ss} %-5p - %m%n";
	consoleAppender.setLayout(new PatternLayout(pattern));
	// Change to Level.ERROR for fewer messages:
	consoleAppender.setThreshold(Level.INFO);

	consoleAppender.activateOptions();
	Logger.getRootLogger().addAppender(consoleAppender);
}
 
Example 15
Source File: ExampleHelpers.java    From Wikidata-Toolkit with Apache License 2.0 5 votes vote down vote up
/**
 * Defines how messages should be logged. This method can be modified to
 * restrict the logging messages that are shown on the console or to change
 * their formatting. See the documentation of Log4J for details on how to do
 * this.
 */
public static void configureLogging() {
	// Create the appender that will write log messages to the console.
	ConsoleAppender consoleAppender = new ConsoleAppender();
	// Define the pattern of log messages.
	// Insert the string "%c{1}:%L" to also show class name and line.
	String pattern = "%d{yyyy-MM-dd HH:mm:ss} %-5p - %m%n";
	consoleAppender.setLayout(new PatternLayout(pattern));
	// Change to Level.ERROR for fewer messages:
	consoleAppender.setThreshold(Level.INFO);

	consoleAppender.activateOptions();
	Logger.getRootLogger().addAppender(consoleAppender);
}
 
Example 16
Source File: ZeppelinApplicationDevServer.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
void setLogger() {
  ConsoleAppender console = new ConsoleAppender(); //create appender
  //configure the appender
  String PATTERN = "%d [%p|%c|%C{1}] %m%n";
  console.setLayout(new PatternLayout(PATTERN));
  console.setThreshold(Level.DEBUG);
  console.activateOptions();
  //add appender to any Logger (here is root)
  org.apache.log4j.Logger.getRootLogger().addAppender(console);
}
 
Example 17
Source File: Main.java    From swingsane with Apache License 2.0 5 votes vote down vote up
private static void initLog4J() {
  ConsoleAppender console = new ConsoleAppender();
  console.setLayout(new EnhancedPatternLayout("%d %-5p [%t] %c.%M - %m%n %throwable{short}"));
  console.setThreshold(Level.DEBUG);
  console.activateOptions();
  Logger.getRootLogger().addAppender(console);
}
 
Example 18
Source File: Main.java    From jpeek with MIT License 5 votes vote down vote up
/**
 * Prepare {@link ConsoleAppender} based on configuration.
 * @return Configured {@link ConsoleAppender}.
 */
private ConsoleAppender buildConsoleAppender() {
    final ConsoleAppender console = new ConsoleAppender();
    if (!this.quiet) {
        console.setLayout(new PatternLayout("%m%n"));
        console.activateOptions();
        Logger.getRootLogger().addAppender(console);
    }
    return console;
}
 
Example 19
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 20
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());
}