protected void clearLogMdc() { try { LogMdc.setup(null); } catch (Exception e) { log.debug("Unable to clear MDC", e); } } }
@Override public File getLogFile(String fileName) throws LogFileNotFoundException { File logFile = new File(logDir, FilenameUtils.getName(fileName)); if (!logFile.exists()) throw new LogFileNotFoundException(fileName); return logFile; }
@Override public void init(Map<String, Object> params) { logLevelField.setOptionsList(LoggingHelper.getLevels()); }
@Override public void setLoggerLevel(String loggerName, String level) throws LogControlException { Level logLevel = LoggingHelper.getLevelFromString(level); if (logLevel == null) throw new UnrecognizedLogLevelException(level); logControl.setLoggerLevel(loggerName, logLevel); }
File logFile = new File(logDir, fileName); if (!logFile.exists()) throw new LogFileNotFoundException(fileName); throw new LogControlException("Error reading log file: " + fileName); } finally { if (randomAccessFile != null) {
public static void writeArchivedLogTailToStream(File logFile, OutputStream outputStream) throws IOException { if (!logFile.exists()) { throw new FileNotFoundException(); } ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(outputStream); zipOutputStream.setMethod(ZipArchiveOutputStream.DEFLATED); zipOutputStream.setEncoding(ZIP_ENCODING); byte[] content = getTailBytes(logFile); ArchiveEntry archiveEntry = newTailArchive(logFile.getName(), content); zipOutputStream.putArchiveEntry(archiveEntry); zipOutputStream.write(content); zipOutputStream.closeArchiveEntry(); zipOutputStream.close(); }
@Override public boolean shouldLog(int level, String category) { if (category == null) return false; Level logbackLevel = levels.get(level); if (logbackLevel == null) return false; Logger log = getLog(category); return log.isEnabledFor(logbackLevel); } }
@Override public void setAppenderThreshold(String appenderName, Level threshold) throws AppenderThresholdNotSupported { for (Map.Entry<String, Appender> entry : getAllAppenders().entrySet()) { if (entry.getKey().equals(appenderName)) { Appender appender = entry.getValue(); for (Object filter : appender.getCopyOfAttachedFiltersList()) { if (filter instanceof ThresholdFilter) { ((ThresholdFilter) filter).setLevel(threshold.levelStr); return; } } } } throw new AppenderThresholdNotSupported(appenderName); }
private static byte[] getTailBytes(File logFile) throws FileNotFoundException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buf = null; int len; int size = 1024; try { RandomAccessFile randomAccessFile = new RandomAccessFile(logFile, "r"); long lengthFile = randomAccessFile.length(); if (lengthFile >= LOG_TAIL_FOR_PACKING_SIZE) { randomAccessFile.seek(lengthFile - LOG_TAIL_FOR_PACKING_SIZE); skipFirstLine(randomAccessFile); } buf = new byte[size]; while ((len = randomAccessFile.read(buf, 0, size)) != -1) { bos.write(buf, 0, len); } buf = bos.toByteArray(); } catch (IOException e) { log.error("Unable to get tail for log file " + logFile.getName(), e); } finally { IOUtils.closeQuietly(bos); } return buf; }
public static void writeArchivedLogToStream(File logFile, OutputStream outputStream) throws IOException { if (!logFile.exists()) { throw new FileNotFoundException(); } File tempFile = File.createTempFile(FilenameUtils.getBaseName(logFile.getName()) + "_log_", ".zip"); ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(tempFile); zipOutputStream.setMethod(ZipArchiveOutputStream.DEFLATED); zipOutputStream.setEncoding(ZIP_ENCODING); ArchiveEntry archiveEntry = newArchive(logFile); zipOutputStream.putArchiveEntry(archiveEntry); FileInputStream logFileInput = new FileInputStream(logFile); IOUtils.copyLarge(logFileInput, zipOutputStream); logFileInput.close(); zipOutputStream.closeArchiveEntry(); zipOutputStream.close(); FileInputStream tempFileInput = new FileInputStream(tempFile); IOUtils.copyLarge(tempFileInput, outputStream); tempFileInput.close(); FileUtils.deleteQuietly(tempFile); }
@Override public void setAppenderThreshold(String appenderName, String threshold) throws LogControlException { List<String> appenders = logControl.getAppenders(); if (!appenders.contains(appenderName)) throw new AppenderNotFoundException(appenderName); Level appenderThreshold = LoggingHelper.getLevelFromString(threshold); if (appenderThreshold == null) throw new UnrecognizedLogThresholdException(threshold); logControl.setAppenderThreshold(appenderName, appenderThreshold); } }
protected void setupLogMdc() { try { SecurityContext securityContext = AppContext.getSecurityContext(); if (securityContext != null) { LogMdc.setup(securityContext); } } catch (Exception e) { log.debug("Unable to set MDC", e); } }
public static Level getLevelFromString(String levelString) { for (Level logLevel : getLevels()) { if (logLevel.toString().equalsIgnoreCase(levelString)) return logLevel; } return null; } }
@Override public void log(SessionLogEntry sessionLogEntry) { Level logbackLevel = levels.get(sessionLogEntry.getLevel()); if (logbackLevel == null) return; Logger log = getLog(sessionLogEntry.getNameSpace()); if (log.isEnabledFor(logbackLevel)) { String msg = sessionLogEntry.getMessage() + (sessionLogEntry.getParameters() == null ? "" : " " + Arrays.toString(sessionLogEntry.getParameters())); switch (logbackLevel.toString()) { case "ERROR": log.error(msg); break; case "WARN": log.warn(msg); break; case "INFO": log.info(msg); break; case "DEBUG": log.debug(msg); break; case "TRACE": log.trace(msg); break; } } }
@Override public Level getAppenderThreshold(String appenderName) throws AppenderThresholdNotSupported { for (Map.Entry<String, Appender> entry : getAllAppenders().entrySet()) { if (entry.getKey().equals(appenderName)) { Appender appender = entry.getValue(); for (Object filter : appender.getCopyOfAttachedFiltersList()) { if (filter instanceof ThresholdFilter) { try { Field field = filter.getClass().getDeclaredField("level"); field.setAccessible(true); return (Level) field.get(filter); } catch (NoSuchFieldException | IllegalAccessException e) { log.error("Error getting appender " + appenderName + " level", e); throw new AppenderThresholdNotSupported(appenderName); } } } } } throw new AppenderThresholdNotSupported(appenderName); }
public void getLoggerLevel() { if (StringUtils.isNotEmpty(loggerNameField.getValue())) { String loggerName = loggerNameField.getValue(); String level; try { level = jmxRemoteLoggingAPI.getLoggerLevel(getSelectedConnection(), loggerName); } catch (LogControlException | JmxControlException e) { log.error("Error getting logger level", e); showNotification(getMessage("exception.logControl"), NotificationType.ERROR); return; } if (level != null) loggerLevelField.setValue(LoggingHelper.getLevelFromString(level)); } else { loggerNameField.setValue(null); showNotification(getMessage("logger.notSelected"), NotificationType.HUMANIZED); } }
/** * Set current thread's {@link SecurityContext}. * @param securityContext security context to be set for the current thread */ public static void setSecurityContext(@Nullable SecurityContext securityContext) { log.trace("setSecurityContext {} for thread {}", securityContext, Thread.currentThread()); securityContextHolder.set(securityContext); LogMdc.setup(securityContext); }
buttonField.setSpacing(true); for (Level logLevel : LoggingHelper.getLevels()) { if (logLevel != Level.OFF && logLevel != Level.ALL) { Button button = componentsFactory.createComponent(Button.class);
loggersMap.put(log.getKey(), LoggingHelper.getLevelFromString(log.getValue()));
public void getAppenderLevel() { if (StringUtils.isNotEmpty(appenderNameField.getValue())) { String appenderName = appenderNameField.getValue(); String threshold; try { threshold = jmxRemoteLoggingAPI.getAppenderThreshold(getSelectedConnection(), appenderName); } catch (LogControlException | JmxControlException e) { log.error("Error getting appender level", e); Throwable rootCause = ExceptionUtils.getRootCause(e); showNotification(getMessage("exception.logControl"), rootCause.getMessage(), NotificationType.ERROR); return; } if (threshold != null) appenderLevelField.setValue(LoggingHelper.getLevelFromString(threshold)); } else { appenderLevelField.setValue(null); showNotification(getMessage("appender.notSelected"), NotificationType.HUMANIZED); } }