Search in sources :

Example 1 with LogControlException

use of com.haulmont.cuba.core.sys.logging.LogControlException in project cuba by cuba-platform.

the class LogControlImpl method getTail.

@Override
public String getTail(String fileName) throws LogControlException {
    // security check, supported only valid file names
    fileName = FilenameUtils.getName(fileName);
    StringBuilder sb = new StringBuilder();
    RandomAccessFile randomAccessFile = null;
    try {
        File logFile = new File(logDir, fileName);
        if (!logFile.exists())
            throw new LogFileNotFoundException(fileName);
        randomAccessFile = new RandomAccessFile(logFile, "r");
        long lengthFile = randomAccessFile.length();
        if (lengthFile >= LOG_TAIL_AMOUNT_BYTES) {
            randomAccessFile.seek(lengthFile - LOG_TAIL_AMOUNT_BYTES);
            skipFirstLine(randomAccessFile);
        }
        while (randomAccessFile.read() != -1) {
            randomAccessFile.seek(randomAccessFile.getFilePointer() - 1);
            String line = readUtf8Line(randomAccessFile);
            if (line != null) {
                sb.append(line).append("\n");
            }
        }
    } catch (IOException e) {
        log.error("Error reading log file", e);
        throw new LogControlException("Error reading log file: " + fileName);
    } finally {
        if (randomAccessFile != null) {
            try {
                randomAccessFile.close();
            } catch (IOException ignored) {
            }
        }
    }
    return sb.toString();
}
Also used : LogFileNotFoundException(com.haulmont.cuba.core.sys.logging.LogFileNotFoundException) RandomAccessFile(java.io.RandomAccessFile) LogControlException(com.haulmont.cuba.core.sys.logging.LogControlException) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 2 with LogControlException

use of com.haulmont.cuba.core.sys.logging.LogControlException in project cuba by cuba-platform.

the class ServerLogWindow method setLoggerLevel.

public void setLoggerLevel() {
    if (StringUtils.isNotEmpty(loggerNameField.<String>getValue())) {
        if (loggerLevelField.getValue() != null) {
            String loggerName = loggerNameField.getValue();
            Level level = loggerLevelField.getValue();
            try {
                jmxRemoteLoggingAPI.setLoggerLevel(getSelectedConnection(), loggerName, level.toString());
            } catch (LogControlException | JmxControlException e) {
                log.error("Error setting logger level", e);
                showNotification(getMessage("exception.logControl"), NotificationType.ERROR);
                return;
            }
            showNotification(formatMessage("logger.setMessage", loggerName, level.toString()), NotificationType.HUMANIZED);
        } else
            showNotification(getMessage("logger.notSelectedLevel"), NotificationType.HUMANIZED);
    } else {
        loggerNameField.setValue(null);
        showNotification(getMessage("logger.notSelected"), NotificationType.HUMANIZED);
    }
}
Also used : Level(ch.qos.logback.classic.Level) LogControlException(com.haulmont.cuba.core.sys.logging.LogControlException) JmxControlException(com.haulmont.cuba.web.jmx.JmxControlException)

Example 3 with LogControlException

use of com.haulmont.cuba.core.sys.logging.LogControlException in project cuba by cuba-platform.

the class ServerLogWindow method openAddLoggerDialog.

protected void openAddLoggerDialog() {
    AdditionLoggerWindow additionLogger = (AdditionLoggerWindow) openWindow("serverLogAddLoggerDialog", OpenType.DIALOG);
    additionLogger.addCloseListener(actionId -> {
        if (COMMIT_ACTION_ID.equals(actionId)) {
            Level level = additionLogger.getSelectedLevel();
            String loggerName = additionLogger.getSelectedLoggerName();
            try {
                jmxRemoteLoggingAPI.setLoggerLevel(getSelectedConnection(), loggerName, level.toString());
            } catch (LogControlException | JmxControlException e) {
                log.error("Error setting logger level", e);
                showNotification(getMessage("exception.logControl"), NotificationType.ERROR);
            }
            showNotification(formatMessage("logger.setMessage", loggerName, level.toString()));
            refreshLoggers();
        }
    });
    loggerNameField.setValue(null);
    loggerLevelField.setValue(null);
}
Also used : Level(ch.qos.logback.classic.Level) LogControlException(com.haulmont.cuba.core.sys.logging.LogControlException) JmxControlException(com.haulmont.cuba.web.jmx.JmxControlException)

Example 4 with LogControlException

use of com.haulmont.cuba.core.sys.logging.LogControlException in project cuba by cuba-platform.

the class ServerLogWindow method openLoggerControlDialog.

public void openLoggerControlDialog() {
    Map<String, Level> loggersMap = new HashMap<>();
    Map<String, String> loggersLevels = jmxRemoteLoggingAPI.getLoggersLevels(getSelectedConnection());
    for (Map.Entry<String, String> log : loggersLevels.entrySet()) {
        loggersMap.put(log.getKey(), LoggingHelper.getLevelFromString(log.getValue()));
    }
    ControlLoggerWindow controlLogger = (ControlLoggerWindow) openWindow("serverLogLoggerControlDialog", OpenType.DIALOG, ParamsMap.of("loggersMap", loggersMap));
    controlLogger.addCloseListener(actionId -> {
        if (COMMIT_ACTION_ID.equals(actionId)) {
            Map<String, Level> levels = controlLogger.getLevels();
            try {
                Map<String, String> updates = new HashMap<>();
                for (Map.Entry<String, Level> levelEntry : levels.entrySet()) {
                    String loggerName = levelEntry.getKey();
                    Level newLogLevel = levelEntry.getValue();
                    Level prevLevel = loggersMap.get(loggerName);
                    String logLevel = prevLevel == null ? null : prevLevel.toString();
                    if (!Objects.equals(logLevel, newLogLevel.toString())) {
                        updates.put(loggerName, newLogLevel.toString());
                    }
                }
                if (!updates.isEmpty()) {
                    jmxRemoteLoggingAPI.setLoggersLevels(getSelectedConnection(), updates);
                }
            } catch (LogControlException | JmxControlException e) {
                log.error("Error setting logger level", e);
                showNotification(getMessage("exception.logControl"), NotificationType.ERROR);
            }
            showNotification(getMessage("logger.control.apply"), NotificationType.HUMANIZED);
            refreshLoggers();
        }
    });
}
Also used : Level(ch.qos.logback.classic.Level) LogControlException(com.haulmont.cuba.core.sys.logging.LogControlException) JmxControlException(com.haulmont.cuba.web.jmx.JmxControlException) ParamsMap(com.haulmont.bali.util.ParamsMap)

Example 5 with LogControlException

use of com.haulmont.cuba.core.sys.logging.LogControlException in project cuba by cuba-platform.

the class ServerLogWindow method setAppenderLevel.

public void setAppenderLevel() {
    if (StringUtils.isNotEmpty(appenderNameField.getValue())) {
        if (appenderLevelField.getValue() != null) {
            String appenderName = appenderNameField.getValue();
            Level threshold = appenderLevelField.getValue();
            try {
                jmxRemoteLoggingAPI.setAppenderThreshold(getSelectedConnection(), appenderName, threshold.toString());
            } catch (LogControlException | JmxControlException e) {
                log.error("Error setting appender level", e);
                Throwable rootCause = ExceptionUtils.getRootCause(e);
                showNotification(getMessage("exception.logControl"), rootCause.getMessage(), NotificationType.ERROR);
                return;
            }
            showNotification(formatMessage("appender.setMessage", appenderName, threshold.toString()), NotificationType.HUMANIZED);
        } else
            showNotification(getMessage("appender.notSelectedThreshold"), NotificationType.HUMANIZED);
    } else {
        appenderNameField.setValue(null);
        showNotification(getMessage("appender.notSelected"), NotificationType.HUMANIZED);
    }
}
Also used : Level(ch.qos.logback.classic.Level) LogControlException(com.haulmont.cuba.core.sys.logging.LogControlException) JmxControlException(com.haulmont.cuba.web.jmx.JmxControlException)

Aggregations

LogControlException (com.haulmont.cuba.core.sys.logging.LogControlException)7 Level (ch.qos.logback.classic.Level)5 JmxControlException (com.haulmont.cuba.web.jmx.JmxControlException)5 IOException (java.io.IOException)2 ParamsMap (com.haulmont.bali.util.ParamsMap)1 JmxInstance (com.haulmont.cuba.core.entity.JmxInstance)1 LogFileNotFoundException (com.haulmont.cuba.core.sys.logging.LogFileNotFoundException)1 ExportDisplay (com.haulmont.cuba.gui.export.ExportDisplay)1 LogDataProvider (com.haulmont.cuba.web.export.LogDataProvider)1 CubaScrollBoxLayout (com.haulmont.cuba.web.toolkit.ui.CubaScrollBoxLayout)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 RandomAccessFile (java.io.RandomAccessFile)1 StringReader (java.io.StringReader)1