Java Code Examples for java.util.logging.LogManager#readConfiguration()

The following examples show how to use java.util.logging.LogManager#readConfiguration() . 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: LoggingConfig.java    From launchpad-missioncontrol with Apache License 2.0 7 votes vote down vote up
public LoggingConfig() {
    try {
        // Load a properties file from class path java.util.logging.config.file
        final LogManager logManager = LogManager.getLogManager();
        URL configURL = getClass().getResource("/logging.properties");
        if (configURL != null) {
            try (InputStream is = configURL.openStream()) {
                logManager.readConfiguration(is);
            }
        } else {
            // Programmatic configuration
            System.setProperty("java.util.logging.SimpleFormatter.format",
                               "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS.%1$tL %4$-7s [%3$s] %5$s %6$s%n");

            final ConsoleHandler consoleHandler = new ConsoleHandler();
            consoleHandler.setLevel(Level.FINEST);
            consoleHandler.setFormatter(new SimpleFormatter());

            final Logger app = Logger.getLogger("app");
            app.setLevel(Level.FINEST);
            app.addHandler(consoleHandler);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 2
Source File: MZmineLoggingConfiguration.java    From mzmine3 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Configures the logging properties according to the logging.properties file found in the jar
 * resources
 */
public MZmineLoggingConfiguration() {

  try {
    ClassLoader cl = MZmineLoggingConfiguration.class.getClassLoader();
    InputStream loggingProperties = cl.getResourceAsStream("logging.properties");
    LogManager logMan = LogManager.getLogManager();
    logMan.readConfiguration(loggingProperties);
    loggingProperties.close();
  } catch (Exception e) {
    e.printStackTrace();
  }

}
 
Example 3
Source File: MZmineLogging.java    From old-mzmine3 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Configures the logging properties according to the logging.properties file found in the jar
 * resources
 */
public static void configureLogging() {

  try {
    ClassLoader cl = MZmineCore.class.getClassLoader();
    InputStream loggingProperties = cl.getResourceAsStream("logging.properties");
    LogManager logMan = LogManager.getLogManager();
    logMan.readConfiguration(loggingProperties);
    loggingProperties.close();
  } catch (Exception e) {
    e.printStackTrace();
  }

}
 
Example 4
Source File: VmRuntimeLogHandler.java    From appengine-java-vm-runtime with Apache License 2.0 5 votes vote down vote up
/**
 * Reloads logging to pick up changes to the java.util.logging.config.file system property.
 */
private static void reloadLoggingProperties(LogManager logManager) {
  if (System.getProperty(JAVA_UTIL_LOGGING_CONFIG_PROPERTY) == null) {
    return;
  }
  try {
    logManager.readConfiguration();
  } catch (SecurityException | IOException e) {
    System.err.println("Warning: caught exception when reading logging properties.");
    System.err.println(e.getClass().getName() + ": " + e.getMessage());
  }
}
 
Example 5
Source File: JMetalLogger.java    From jMetal with MIT License 5 votes vote down vote up
/**
 * This method provides a single-call method to configure the {@link Logger}
 * instances. A default configuration is considered, enriched with a custom
 * property file for more convenient logging. The custom file is considered
 * after the default configuration, so it can override it if necessary. The
 * custom file might be provided as an argument of this method, otherwise we
 * look for a file named "jMetal.log.ini". If no custom file is provided,
 * then only the default configuration is considered.
 * 
 * @param propertyFile
 *            the property file to use for custom configuration,
 *            <code>null</code> to use only the default configuration
 * @throws IOException
 */
public static void configureLoggers(File propertyFile) throws IOException {
	// Prepare default configuration
	ByteArrayOutputStream stream = new ByteArrayOutputStream();
	PrintStream printer = new PrintStream(stream);
	printer.println(".level = INFO");
	printer.println("handlers = java.util.logging.FileHandler, java.util.logging.ConsoleHandler");
	printer.println("formatters = java.util.logging.SimpleFormatter");
	printer.println("java.util.logging.SimpleFormatter.format = %1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS.%1$tL %4$s: %5$s [%2$s]%6$s%n");

	printer.println("java.util.logging.FileHandler.pattern = jMetal.log");
	printer.println("java.util.logging.FileHandler.level = ALL");

	printer.println("java.util.logging.ConsoleHandler.level = ALL");

	// Retrieve custom configuration
	File defaultFile = new File("jMetal.log.ini");
	if (propertyFile != null) {
		printer.println(FileUtils.readFileToString(propertyFile));
	} else if (defaultFile.exists()) {
		printer.println(FileUtils.readFileToString(defaultFile));
	} else {
		// use only default configuration
	}
	printer.close();

	// Apply configuration
	LogManager manager = LogManager.getLogManager();
	manager.readConfiguration(IOUtils.toInputStream(new String(stream
			.toByteArray(), Charset.forName("UTF-8"))));
	logger.info("Loggers configured with " + propertyFile);
}
 
Example 6
Source File: TestConfigurationListeners.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void accept(LogManager t) throws IOException {
    t.readConfiguration();
}
 
Example 7
Source File: JSqsh.java    From jsqsh with Apache License 2.0 4 votes vote down vote up
static private void configureLogging(List<String> loggers) {
    
    InputStream in = 
        JSqsh.class.getClassLoader().getResourceAsStream(LOGGING_CONFIG);
    if (in == null) {
        
        System.err.println("WARNING: Cannot find resource " 
            + LOGGING_CONFIG);
        return;
    }
    
    try {
        
        LogManager logMan = LogManager.getLogManager();
        logMan.readConfiguration(in);
        in.close();
    }
    catch (IOException e) {
        
        System.err.println("WARNING: Unable to read logging "
            + "properties " + LOGGING_CONFIG + ": " + e.getMessage());
    }

    /*
     * Turn on debugging if requested.
     */
    for (String logger : loggers) {
        
        Logger log = Logger.getLogger(logger);
        if (log != null) {
            
            log.setLevel(Level.FINE);
            System.out.println("Debugging level for '"
                + log.getName() + "' is now '"
                + log.getLevel().getName() + "'");
        }
        else {
            
            System.err.println("--debug: Unable to find logger '"
                + logger + "'");
            System.exit(1);
        }
    }
}
 
Example 8
Source File: LoggerUnitTest.java    From candybean with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * A system test for a logger configured using the candybean logger
 * configuration.
 * 
 * @throws Exception
 */
@Test
public void cbConfiguredLogger() throws Exception {
	String name1 = this.getClass().getSimpleName() + "1";
	String name2 = this.getClass().getSimpleName() + "2";
	String config1Path = Candybean.ROOT_DIR + File.separator + name1 + ".config";
	String config2Path = Candybean.ROOT_DIR + File.separator + name2 + ".config";
	String log1Path = Candybean.ROOT_DIR + File.separator + "log" + File.separator + name1 + ".log";
	String log2Path = Candybean.ROOT_DIR + File.separator + "log" + File.separator + name2 + ".log";
	
	// Load the initial properties from the candybean config file
       Properties initialProperties = candybean.config.getPropertiesCopy();
	
	// Change the FileHandler Formatter to XMLFormatter
	initialProperties.setProperty("java.util.logging.FileHandler.formatter", "java.util.logging.XMLFormatter");
	initialProperties.setProperty("java.util.logging.ConsoleHandler.formatter", "java.util.logging.XMLFormatter");
	
	// Create a new config file and write props to that file
	File config1File = new File(config1Path);
	config1File.createNewFile();
	initialProperties.store(new FileOutputStream(config1File), null);

	// Update the system property that specifies where to load the logging configuration from.
	System.setProperty("java.util.logging.config.file", config1Path);
	LogManager.getLogManager().readConfiguration();
	logger = Logger.getLogger(this.getClass().getSimpleName());
	
	// Log to file and verify text
	File log1File = new File(log1Path);
	FileHandler firstFileHandler = new FileHandler(log1Path);
	logger.addHandler(firstFileHandler);
	logger.info("First logged message configured using candybean configuration file");
	assertTrue(log1File.exists());
	assertEquals(getLinesInLogFile(log1File), 14);
	
	// Change the FileHandler Formatter to SimpleFormatter
	initialProperties.setProperty("java.util.logging.FileHandler.formatter", "java.util.logging.SimpleFormatter");
	initialProperties.setProperty("java.util.logging.ConsoleHandler.formatter", "java.util.logging.SimpleFormatter");
	
	// Create a second config file and write props to that file
	File config2File = new File(config2Path);
	config2File.createNewFile();
	initialProperties.store(new FileOutputStream(config2File), null);

	// Update the system property that specifies where to load the logging configuration from.
	System.setProperty("java.util.logging.config.file", config2Path);
	LogManager.getLogManager().readConfiguration();
	logger = Logger.getLogger(this.getClass().getSimpleName());
	
	// Log to file and verify text
	File log2File = new File(log2Path);
	FileHandler secondFileHandler = new FileHandler(log2Path);
	logger.addHandler(secondFileHandler);
	logger.info("Second logged message configured using different candybean configuration file");
	assertTrue(log2File.exists());
	assertTrue(getLinesInLogFile(log2File) < 13);
	
	// Reset the logging config file path to the default and re-read the configuration
	System.setProperty("java.util.logging.config.file", candybean.config.configFile.getCanonicalPath());
	LogManager logManager = LogManager.getLogManager();
	logManager.readConfiguration();
	
	// Delete all created configuration and log files
	config1File.delete();
	log1File.delete();
	config2File.delete();
	log2File.delete();
}