use of com.sun.common.util.logging.SortedLoggingProperties in project Payara by payara.
the class PayaraMicroImpl method resetLogging.
/**
* Reset the logging properties from the given file.
* @param loggingProperty the location of the file to read from.
*/
private void resetLogging(String loggingProperty) {
if (loggingProperty != null) {
// we need to copy into the unpacked domain the specified logging.properties file
File file = new File(loggingProperty);
if (file.canRead()) {
try {
runtimeDir.setLoggingProperties(file);
} catch (IOException ex) {
LOGGER.log(Level.SEVERE, "Could not copy over logging properties file", ex);
}
}
if (logToFile) {
LOGGER.log(Level.WARNING, "logToFile command line option ignored as a logging.properties file has been provided");
}
System.setProperty("java.util.logging.config.file", runtimeDir.getLoggingProperties().getAbsolutePath());
try (InputStream is = new FileInputStream(runtimeDir.getLoggingProperties())) {
LogManager.getLogManager().readConfiguration(is);
// go through all root handlers and set formatters based on properties
Logger rootLogger = LogManager.getLogManager().getLogger("");
for (Handler handler : rootLogger.getHandlers()) {
String formatter = LogManager.getLogManager().getProperty(handler.getClass().getCanonicalName() + ".formatter");
if (formatter != null) {
handler.setFormatter((Formatter) Class.forName(formatter).newInstance());
}
}
} catch (SecurityException | IOException | ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
LOGGER.log(Level.SEVERE, "Unable to reset the log manager", ex);
}
} else {
// we are likely using our default properties file so see if we need to rewrite it
if (logToFile) {
// we need to reset our logging properties to use the file handler as well
// read the default properties and then add the file handler properties
Properties currentProps = new Properties();
try (InputStream is = new FileInputStream(runtimeDir.getLoggingProperties())) {
currentProps.load(is);
// add file handler properties
currentProps.setProperty("java.util.logging.FileHandler.pattern", userLogFile);
currentProps.setProperty("handlers", "java.util.logging.FileHandler, java.util.logging.ConsoleHandler");
currentProps.setProperty("java.util.logging.FileHandler.limit", "1024000");
currentProps.setProperty("java.util.logging.FileHandler.count", "10");
currentProps.setProperty("java.util.logging.FileHandler.level", "INFO");
currentProps.setProperty("java.util.logging.FileHandler.formatter", "java.util.logging.SimpleFormatter");
currentProps.setProperty("java.util.logging.FileHandler.append", "true");
} catch (IOException ex) {
LOGGER.log(Level.SEVERE, "Unable to load the logging properties from the runtime directory", ex);
}
// now write them back
try (OutputStream os = new FileOutputStream(runtimeDir.getLoggingProperties())) {
new SortedLoggingProperties(currentProps).store(os, "Generated Logging properties file from Payara Micro log to file option");
} catch (IOException ex) {
LOGGER.log(Level.SEVERE, "Unable to load the logging properties from the runtime directory", ex);
}
}
System.setProperty("java.util.logging.config.file", runtimeDir.getLoggingProperties().getAbsolutePath());
try (InputStream is = new FileInputStream(runtimeDir.getLoggingProperties())) {
LogManager.getLogManager().readConfiguration(is);
// reset the formatters on the two handlers
// Logger rootLogger = Logger.getLogger("");
String formatter = LogManager.getLogManager().getProperty("java.util.logging.ConsoleHandler.formatter");
Formatter formatterClass = new ODLLogFormatter(null);
try {
formatterClass = (Formatter) Class.forName(formatter).newInstance();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
LOGGER.log(Level.SEVERE, "Specified Formatter class could not be loaded " + formatter, ex);
}
Logger rootLogger = Logger.getLogger("");
for (Handler handler : rootLogger.getHandlers()) {
handler.setFormatter(formatterClass);
}
} catch (SecurityException | IOException ex) {
LOGGER.log(Level.SEVERE, "Unable to reset the log manager", ex);
}
}
}
Aggregations