Search in sources :

Example 1 with Task

use of org.glassfish.api.logging.Task in project Payara by payara.

the class GFFileHandler method rotationOnDateChange.

private void rotationOnDateChange() {
    dayBasedFileRotation = true;
    rotationTimeLimitValue = 0L;
    int millisecondsInDay = 1000 * 60 * 60 * 24;
    Date date = new Date();
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yy");
    long systemTime = System.currentTimeMillis();
    String nextDate = dateFormat.format(date.getTime() + millisecondsInDay);
    Date nextDay;
    try {
        nextDay = dateFormat.parse(nextDate);
    } catch (ParseException e) {
        nextDay = new Date();
        logRecord = new LogRecord(Level.WARNING, LogFacade.DATE_PARSING_FAILED);
        logRecord.setParameters(new Object[] { nextDate });
        logRecord.setResourceBundle(ResourceBundle.getBundle(LogFacade.LOGGING_RB_NAME));
        logRecord.setThreadID((int) Thread.currentThread().getId());
        logRecord.setLoggerName(LogFacade.LOGGING_LOGGER_NAME);
        EarlyLogHandler.earlyMessages.add(logRecord);
    }
    long nextDaySystemTime = nextDay.getTime();
    rotationTimeLimitValue = nextDaySystemTime - systemTime;
    Task rotationTask = () -> {
        rotate();
        return null;
    };
    if (className.equals(GF_FILE_HANDLER)) {
        LogRotationTimer.getInstance().startTimer(new LogRotationTimerTask(rotationTask, rotationTimeLimitValue / 60000));
    } else {
        PayaraNotificationLogRotationTimer.getInstance().startTimer(new LogRotationTimerTask(rotationTask, rotationTimeLimitValue / 60000));
    }
}
Also used : Task(org.glassfish.api.logging.Task) GFLogRecord(com.sun.common.util.logging.GFLogRecord) LogRecord(java.util.logging.LogRecord) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with Task

use of org.glassfish.api.logging.Task in project Payara by payara.

the class GFFileHandler method postConstruct.

@Override
public void postConstruct() {
    String filename = evaluateFileName();
    File logFile = new File(filename);
    absoluteServerLogName = filename;
    if (!logFile.isAbsolute()) {
        logFile = new File(env.getDomainRoot(), filename);
        absoluteServerLogName = env.getDomainRoot() + File.separator + filename;
    }
    changeFileName(logFile);
    // Reading just few lines of log file to get the log fomatter used.
    BufferedReader br = null;
    String strLine = "";
    int odlFormatter = 0;
    int uniformLogFormatter = 0;
    int otherFormatter = 0;
    boolean mustRotate = false;
    String propValue = null;
    propValue = manager.getProperty(cname + ".logtoFile");
    boolean logToFile = false;
    if (propValue != null) {
        logToFile = Boolean.parseBoolean(propValue);
    }
    if (logToFile) {
        try {
            br = new BufferedReader(new FileReader(logFile));
            while ((strLine = br.readLine()) != null) {
                strLine = strLine.trim();
                if (!strLine.equals("")) {
                    if (LogFormatHelper.isUniformFormatLogHeader(strLine)) {
                        // for ufl formatter
                        uniformLogFormatter++;
                    } else if (LogFormatHelper.isODLFormatLogHeader(strLine)) {
                        // for ODL formatter
                        odlFormatter++;
                    } else {
                        // for other formatter
                        otherFormatter++;
                    }
                    // Rotate on startup for custom log files
                    if (otherFormatter > 0) {
                        mustRotate = true;
                    }
                    // Read only first log record line and break out of the loop
                    break;
                }
            }
        } catch (Exception e) {
            ErrorManager em = getErrorManager();
            if (em != null) {
                em.error(e.getMessage(), e, ErrorManager.GENERIC_FAILURE);
            }
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (Exception e) {
                }
            }
        }
        if (odlFormatter > 0) {
            currentgffileHandlerFormatter = "com.sun.enterprise.server.logging.ODLLogFormatter";
        } else if (uniformLogFormatter > 0) {
            currentgffileHandlerFormatter = "com.sun.enterprise.server.logging.UniformLogFormatter";
        }
        // start the Queue consumer thread.
        initializePump();
        LogRecord lr = new LogRecord(Level.INFO, LogFacade.GF_VERSION_INFO);
        lr.setParameters(new Object[] { Version.getFullVersion() });
        lr.setResourceBundle(ResourceBundle.getBundle(LogFacade.LOGGING_RB_NAME));
        lr.setThreadID((int) Thread.currentThread().getId());
        lr.setLoggerName(LogFacade.LOGGING_LOGGER_NAME);
        EarlyLogHandler.earlyMessages.add(lr);
        propValue = manager.getProperty(cname + ".rotationOnDateChange");
        boolean rotationOnDateChange = false;
        if (propValue != null) {
            rotationOnDateChange = Boolean.parseBoolean(propValue);
        }
        if (rotationOnDateChange) {
            dayBasedFileRotation = true;
            Long rotationTimeLimitValue = 0L;
            int MILLIS_IN_DAY = 1000 * 60 * 60 * 24;
            Date date = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yy");
            long systime = System.currentTimeMillis();
            String nextDate = dateFormat.format(date.getTime() + MILLIS_IN_DAY);
            Date nextDay = null;
            try {
                nextDay = dateFormat.parse(nextDate);
            } catch (ParseException e) {
                nextDay = new Date();
                lr = new LogRecord(Level.WARNING, LogFacade.DATE_PARSING_FAILED);
                lr.setParameters(new Object[] { nextDate });
                lr.setResourceBundle(ResourceBundle.getBundle(LogFacade.LOGGING_RB_NAME));
                lr.setThreadID((int) Thread.currentThread().getId());
                lr.setLoggerName(LogFacade.LOGGING_LOGGER_NAME);
                EarlyLogHandler.earlyMessages.add(lr);
            }
            long nextsystime = nextDay.getTime();
            rotationTimeLimitValue = nextsystime - systime;
            Task rotationTask = new Task() {

                @Override
                public Object run() {
                    rotate();
                    return null;
                }
            };
            if (cname.equals(GF_FILE_HANDER)) {
                LogRotationTimer.getInstance().startTimer(new LogRotationTimerTask(rotationTask, rotationTimeLimitValue / 60000));
            } else {
                PayaraNotificationLogRotationTimer.getInstance().startTimer(new LogRotationTimerTask(rotationTask, rotationTimeLimitValue / 60000));
            }
        } else {
            Long rotationTimeLimitValue = 0L;
            try {
                propValue = manager.getProperty(cname + ".rotationTimelimitInMinutes");
                if (propValue != null) {
                    rotationTimeLimitValue = Long.parseLong(propValue);
                }
            } catch (NumberFormatException e) {
                lr = new LogRecord(Level.WARNING, LogFacade.INVALID_ATTRIBUTE_VALUE);
                lr.setParameters(new Object[] { propValue, "rotationTimelimitInMinutes" });
                lr.setResourceBundle(ResourceBundle.getBundle(LogFacade.LOGGING_RB_NAME));
                lr.setThreadID((int) Thread.currentThread().getId());
                lr.setLoggerName(LogFacade.LOGGING_LOGGER_NAME);
                EarlyLogHandler.earlyMessages.add(lr);
            }
            if (rotationTimeLimitValue > 0) {
                Task rotationTask = new Task() {

                    public Object run() {
                        rotate();
                        return null;
                    }
                };
                if (cname.equals(GF_FILE_HANDER)) {
                    LogRotationTimer.getInstance().startTimer(new LogRotationTimerTask(rotationTask, rotationTimeLimitValue));
                } else {
                    PayaraNotificationLogRotationTimer.getInstance().startTimer(new LogRotationTimerTask(rotationTask, rotationTimeLimitValue));
                }
            }
        }
        // Also honor the size based rotation if configured.
        Integer rotationLimitAttrValue = DEFAULT_ROTATION_LIMIT_BYTES;
        try {
            propValue = manager.getProperty(cname + ".rotationLimitInBytes");
            if (propValue != null) {
                rotationLimitAttrValue = Integer.parseInt(propValue);
            }
        } catch (NumberFormatException e) {
            lr = new LogRecord(Level.WARNING, LogFacade.INVALID_ATTRIBUTE_VALUE);
            lr.setParameters(new Object[] { propValue, "rotationLimitInBytes" });
            lr.setResourceBundle(ResourceBundle.getBundle(LogFacade.LOGGING_RB_NAME));
            lr.setThreadID((int) Thread.currentThread().getId());
            lr.setLoggerName(LogFacade.LOGGING_LOGGER_NAME);
            EarlyLogHandler.earlyMessages.add(lr);
        }
        // it will be rotated.
        if (rotationLimitAttrValue >= MINIMUM_ROTATION_LIMIT_VALUE || rotationLimitAttrValue == DISABLE_LOG_FILE_ROTATION_VALUE) {
            setLimitForRotation(rotationLimitAttrValue);
        }
        // setLevel(Level.ALL);
        propValue = manager.getProperty(cname + ".flushFrequency");
        if (propValue != null) {
            try {
                flushFrequency = Integer.parseInt(propValue);
            } catch (NumberFormatException e) {
                lr = new LogRecord(Level.WARNING, LogFacade.INVALID_ATTRIBUTE_VALUE);
                lr.setParameters(new Object[] { propValue, "flushFrequency" });
                lr.setResourceBundle(ResourceBundle.getBundle(LogFacade.LOGGING_RB_NAME));
                lr.setThreadID((int) Thread.currentThread().getId());
                lr.setLoggerName(LogFacade.LOGGING_LOGGER_NAME);
                EarlyLogHandler.earlyMessages.add(lr);
            }
        }
        if (flushFrequency <= 0) {
            flushFrequency = 1;
        }
        propValue = manager.getProperty(cname + ".maxHistoryFiles");
        try {
            if (propValue != null) {
                maxHistoryFiles = Integer.parseInt(propValue);
            }
        } catch (NumberFormatException e) {
            lr = new LogRecord(Level.WARNING, LogFacade.INVALID_ATTRIBUTE_VALUE);
            lr.setParameters(new Object[] { propValue, "maxHistoryFiles" });
            lr.setResourceBundle(ResourceBundle.getBundle(LogFacade.LOGGING_RB_NAME));
            lr.setThreadID((int) Thread.currentThread().getId());
            lr.setLoggerName(LogFacade.LOGGING_LOGGER_NAME);
            EarlyLogHandler.earlyMessages.add(lr);
        }
        if (maxHistoryFiles < 0) {
            maxHistoryFiles = 10;
        }
        propValue = manager.getProperty(cname + ".compressOnRotation");
        boolean compressionOnRotation = false;
        if (propValue != null) {
            compressionOnRotation = Boolean.parseBoolean(propValue);
        }
        if (compressionOnRotation) {
            compressLogs = true;
        }
        String formatterName = manager.getProperty(cname + ".formatter");
        formatterName = (formatterName == null) ? DEFAULT_LOG_FILE_FORMATTER_CLASS_NAME : formatterName;
        // Below snapshot of the code is used to rotate server.log file on startup. It is used to avoid different format
        // log messages logged under same server.log file.
        gffileHandlerFormatter = formatterName;
        if (mustRotate) {
            rotate();
        } else if (gffileHandlerFormatter != null && !gffileHandlerFormatter.equals(currentgffileHandlerFormatter)) {
            rotate();
        }
        String excludeFields = manager.getProperty(LogManagerService.EXCLUDE_FIELDS_PROPERTY);
        boolean multiLineMode = Boolean.parseBoolean(manager.getProperty(LogManagerService.MULTI_LINE_MODE_PROPERTY));
        if (UniformLogFormatter.class.getName().equals(formatterName)) {
            configureUniformLogFormatter(excludeFields, multiLineMode);
        } else if (ODLLogFormatter.class.getName().equals(formatterName)) {
            configureODLFormatter(excludeFields, multiLineMode);
        } else if (JSONLogFormatter.class.getName().equals(formatterName)) {
            configureJSONFormatter(excludeFields, multiLineMode);
        } else {
            // Custom formatter is configured in logging.properties
            // Check if the user specified formatter is in play else
            // log an error message
            Formatter currentFormatter = this.getFormatter();
            if (currentFormatter == null || !currentFormatter.getClass().getName().equals(formatterName)) {
                Formatter formatter = findFormatterService(formatterName);
                if (formatter == null) {
                    lr = new LogRecord(Level.SEVERE, LogFacade.INVALID_FORMATTER_CLASS_NAME);
                    lr.setParameters(new Object[] { formatterName });
                    lr.setThreadID((int) Thread.currentThread().getId());
                    lr.setResourceBundle(ResourceBundle.getBundle(LogFacade.LOGGING_RB_NAME));
                    lr.setLoggerName(LogFacade.LOGGING_LOGGER_NAME);
                    EarlyLogHandler.earlyMessages.add(lr);
                    // Fall back to the GlassFish default
                    configureDefaultFormatter(excludeFields, multiLineMode);
                } else {
                    setFormatter(formatter);
                }
            }
        }
        formatterName = this.getFormatter().getClass().getName();
        lr = new LogRecord(Level.INFO, LogFacade.LOG_FORMATTER_INFO);
        lr.setParameters(new Object[] { formatterName });
        lr.setResourceBundle(ResourceBundle.getBundle(LogFacade.LOGGING_RB_NAME));
        lr.setThreadID((int) Thread.currentThread().getId());
        lr.setLoggerName(LogFacade.LOGGING_LOGGER_NAME);
        EarlyLogHandler.earlyMessages.add(lr);
    }
}
Also used : Task(org.glassfish.api.logging.Task) ErrorManager(java.util.logging.ErrorManager) JSONLogFormatter(fish.payara.enterprise.server.logging.JSONLogFormatter) Formatter(java.util.logging.Formatter) JSONLogFormatter(fish.payara.enterprise.server.logging.JSONLogFormatter) ParseException(java.text.ParseException) IOException(java.io.IOException) Date(java.util.Date) LogRecord(java.util.logging.LogRecord) GFLogRecord(com.sun.common.util.logging.GFLogRecord) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) ParseException(java.text.ParseException) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

GFLogRecord (com.sun.common.util.logging.GFLogRecord)2 ParseException (java.text.ParseException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 LogRecord (java.util.logging.LogRecord)2 Task (org.glassfish.api.logging.Task)2 JSONLogFormatter (fish.payara.enterprise.server.logging.JSONLogFormatter)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 Date (java.util.Date)1 ErrorManager (java.util.logging.ErrorManager)1 Formatter (java.util.logging.Formatter)1