Search in sources :

Example 1 with ODLLogFormatter

use of com.sun.enterprise.server.logging.ODLLogFormatter in project Payara by payara.

the class GFLauncherLogger method addLogFileHandler.

/**
 * IMPORTANT! The server's logfile is added to the *local* logger. But it is
 * never removed. The files are kept open by the logger. One really bad
 * result is that Windows will not be able to delete that server after
 * stopping it. Solution: remove the file handler when done.
 *
 * @param logFile The logfile
 * @throws GFLauncherException if the info object has not been setup
 */
static synchronized void addLogFileHandler(String logFile, GFLauncherInfo info) throws GFLauncherException {
    try {
        if (logFile == null || logfileHandler != null) {
            return;
        }
        logfileHandler = new FileHandler(logFile, true);
        logfileHandler.setFormatter(new ODLLogFormatter());
        logfileHandler.setLevel(Level.INFO);
        logger.addHandler(logfileHandler);
    } catch (IOException e) {
        // should be seen in verbose and watchdog modes for debugging
        e.printStackTrace();
    }
}
Also used : ODLLogFormatter(com.sun.enterprise.server.logging.ODLLogFormatter) IOException(java.io.IOException)

Example 2 with ODLLogFormatter

use of com.sun.enterprise.server.logging.ODLLogFormatter in project Payara by payara.

the class PayaraMicroImpl method resetLogging.

private void resetLogging() {
    String loggingProperty = System.getProperty("java.util.logging.config.file");
    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())) {
                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();
            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) IOException(java.io.IOException) Logger(java.util.logging.Logger) 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

ODLLogFormatter (com.sun.enterprise.server.logging.ODLLogFormatter)2 IOException (java.io.IOException)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)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