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

The following examples show how to use org.apache.log4j.ConsoleAppender#setLayout() . 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 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 2
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 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: 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 5
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 6
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 7
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 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: 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 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: 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 12
Source File: MainComingTest.java    From coming with MIT License 5 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);

	java.util.logging.Logger.getLogger("fr.labri.gumtree.matchers").setLevel(java.util.logging.Level.OFF);
	Matcher.LOGGER.setLevel(java.util.logging.Level.OFF);
}
 
Example 13
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 14
Source File: LoggingConfigurator.java    From selenium-grid-extensions with Apache License 2.0 5 votes vote down vote up
private void installConsoleAppender() {
    ConsoleAppender console = new ConsoleAppender();
    console.setName("Console");
    console.setThreshold(logLevel);
    console.setLayout(new PatternLayout(PATTERN));
    console.setFollow(true);
    console.activateOptions();

    Logger.getRootLogger().addAppender(console);
}
 
Example 15
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 16
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);
		}
	}
 
Example 17
Source File: Configuration.java    From GDH with MIT License 5 votes vote down vote up
public Configuration() {
    SecureRandom random = new SecureRandom();
    this.log4jLogger = Logger.getLogger("Logger"+ random.nextInt());
    
    ConsoleAppender appender = new ConsoleAppender();
    appender.setWriter(new PrintWriter(new OutputStreamWriter(System.out, StandardCharsets.UTF_8)));
    appender.setLayout(new PatternLayout(PatternLayout.DEFAULT_CONVERSION_PATTERN));
    this.log4jLogger.addAppender(appender);
    
    this.log4jLogger.setLevel(Level.OFF);
}
 
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: TestTaskExecutor.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void init() {
	ConsoleAppender ca = new ConsoleAppender();
	ca.setWriter(new OutputStreamWriter(System.out));
	ca.setLayout(new PatternLayout("%-5p [%t]: %m%n"));
	BasicConfigurator.configure(ca);
}
 
Example 20
Source File: QueryBenchmark.java    From rya with Apache License 2.0 4 votes vote down vote up
@Setup
public void setup() throws Exception {
    // Setup logging.
    final ConsoleAppender console = new ConsoleAppender();
    console.setLayout(new PatternLayout("%d [%p|%c|%C{1}] %m%n"));
    console.setThreshold(Level.INFO);
    console.activateOptions();
    Logger.getRootLogger().addAppender(console);

    // Load the benchmark's configuration file.
    final InputStream queriesStream = Files.newInputStream(QUERY_BENCHMARK_CONFIGURATION_FILE);
    final QueriesBenchmarkConf benchmarkConf = new QueriesBenchmarkConfReader().load(queriesStream);

    // Create the Rya Configuration object using the benchmark's configuration.
    final AccumuloRdfConfiguration ryaConf = new AccumuloRdfConfiguration();

    final Rya rya = benchmarkConf.getRya();
    ryaConf.setTablePrefix(rya.getRyaInstanceName());

    final Accumulo accumulo = rya.getAccumulo();
    ryaConf.set(ConfigUtils.CLOUDBASE_USER, accumulo.getUsername());
    ryaConf.set(ConfigUtils.CLOUDBASE_PASSWORD, accumulo.getPassword());
    ryaConf.set(ConfigUtils.CLOUDBASE_ZOOKEEPERS, accumulo.getZookeepers());
    ryaConf.set(ConfigUtils.CLOUDBASE_INSTANCE, accumulo.getInstanceName());

    // Print the query plan so that you can visually inspect how PCJs are being applied for each benchmark.
    ryaConf.set(ConfigUtils.DISPLAY_QUERY_PLAN, "true");

    // Turn on PCJs if we are configured to use them.
    final SecondaryIndexing secondaryIndexing = rya.getSecondaryIndexing();
    if(secondaryIndexing.isUsePCJ()) {
        ryaConf.set(ConfigUtils.USE_PCJ, "true");
        ryaConf.set(ConfigUtils.PCJ_STORAGE_TYPE, PrecomputedJoinStorageType.ACCUMULO.toString());
        ryaConf.set(ConfigUtils.PCJ_UPDATER_TYPE, PrecomputedJoinUpdaterType.NO_UPDATE.toString());
    } else {
        ryaConf.set(ConfigUtils.USE_PCJ, "false");
    }

    // Create the connections used to execute the benchmark.
    sail = RyaSailFactory.getInstance( ryaConf );
    sailConn = sail.getConnection();
}