Search in sources :

Example 1 with SortedLoggingProperties

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);
        }
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Formatter(java.util.logging.Formatter) ODLLogFormatter(com.sun.enterprise.server.logging.ODLLogFormatter) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Handler(java.util.logging.Handler) SortedLoggingProperties(com.sun.common.util.logging.SortedLoggingProperties) IOException(java.io.IOException) Logger(java.util.logging.Logger) SortedLoggingProperties(com.sun.common.util.logging.SortedLoggingProperties) BootstrapProperties(org.glassfish.embeddable.BootstrapProperties) GlassFishProperties(org.glassfish.embeddable.GlassFishProperties) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream) ODLLogFormatter(com.sun.enterprise.server.logging.ODLLogFormatter) JarFile(java.util.jar.JarFile) File(java.io.File)

Aggregations

SortedLoggingProperties (com.sun.common.util.logging.SortedLoggingProperties)1 ODLLogFormatter (com.sun.enterprise.server.logging.ODLLogFormatter)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Properties (java.util.Properties)1 JarFile (java.util.jar.JarFile)1 Formatter (java.util.logging.Formatter)1 Handler (java.util.logging.Handler)1 Logger (java.util.logging.Logger)1 BootstrapProperties (org.glassfish.embeddable.BootstrapProperties)1 GlassFishProperties (org.glassfish.embeddable.GlassFishProperties)1