public AccessLogHandler(final HttpHandler next, final AccessLogReceiver accessLogReceiver, String formatString, final ExchangeAttribute attribute, Predicate predicate) { this.next = next; this.accessLogReceiver = accessLogReceiver; this.predicate = predicate; this.formatString = handleCommonNames(formatString); this.tokens = attribute; }
public DefaultAccessLogReceiver build() { return new DefaultAccessLogReceiver(logWriteExecutor, outputDirectory, logBaseName, logNameSuffix, rotate, logFileHeaderGenerator); } }
public static Builder builder() { return new Builder(); }
@Override public void start(StartContext context) throws StartException { if (useServerLog) { logReceiver = new JBossLoggingAccessLogReceiver(); } else { if (pathRelativeTo != null) { DefaultAccessLogReceiver.Builder builder = DefaultAccessLogReceiver.builder().setLogWriteExecutor(worker.getValue()) .setOutputDirectory(directory) .setLogBaseName(filePrefix) .setLogNameSuffix(fileSuffix) .setRotate(rotate); if(extended) { builder.setLogFileHeaderGenerator(new ExtendedAccessLogParser.ExtendedAccessLogHeaderGenerator(pattern)); extendedPattern = new ExtendedAccessLogParser(getClass().getClassLoader()).parse(pattern); } else { extendedPattern = null; logReceiver = builder.build(); } catch (IllegalStateException e) { throw new StartException(e);
protected ExchangeAttribute getProxyElement(PatternTokenizer tokenizer) throws IOException { String token = null; if (tokenizer.hasSubToken()) { tokenizer.getToken(); return new ConstantExchangeAttribute("-"); } else if (tokenizer.hasParameter()) { tokenizer.getParameter(); return new ConstantExchangeAttribute("-"); } UndertowLogger.ROOT_LOGGER.extendedAccessLogCannotDecode(token); return null; }
@Override public HttpHandler wrap(HttpHandler handler) { if (category == null || category.trim().isEmpty()) { return new AccessLogHandler(handler, new JBossLoggingAccessLogReceiver(), format, Wrapper.class.getClassLoader()); } else { return new AccessLogHandler(handler, new JBossLoggingAccessLogReceiver(category), format, Wrapper.class.getClassLoader()); } } }
public ExchangeAttribute parse(String pattern) { List<ExchangeAttribute> list = new ArrayList<ExchangeAttribute>(); PatternTokenizer tokenizer = new PatternTokenizer(pattern); try { tokenizer.getWhiteSpaces(); if (tokenizer.isEnded()) { UndertowLogger.ROOT_LOGGER.extendedAccessLogEmptyPattern(); return null; String token = tokenizer.getToken(); while (token != null) { if (UndertowLogger.ROOT_LOGGER.isDebugEnabled()) { UndertowLogger.ROOT_LOGGER.debug("token = " + token); ExchangeAttribute element = getLogElement(token, tokenizer); if (element == null) { break; String whiteSpaces = tokenizer.getWhiteSpaces(); if (whiteSpaces.length() > 0) { list.add(new ConstantExchangeAttribute(whiteSpaces)); if (tokenizer.isEnded()) { break; token = tokenizer.getToken();
private Closeable configureAccessLogHandler(Undertow.Builder builder, HttpHandler handler) { try { createAccessLogDirectoryIfNecessary(); XnioWorker worker = createWorker(); String prefix = (this.accessLogPrefix != null) ? this.accessLogPrefix : "access_log."; DefaultAccessLogReceiver accessLogReceiver = new DefaultAccessLogReceiver( worker, this.accessLogDirectory, prefix, this.accessLogSuffix, this.accessLogRotate); String formatString = ((this.accessLogPattern != null) ? this.accessLogPattern : "common"); builder.setHandler(new AccessLogHandler(handler, accessLogReceiver, formatString, Undertow.class.getClassLoader())); return () -> { try { accessLogReceiver.close(); worker.shutdown(); } catch (IOException ex) { throw new IllegalStateException(ex); } }; } catch (IOException ex) { throw new IllegalStateException("Failed to create AccessLogHandler", ex); } }
protected AccessLogHandler configureAccessLogHandler(HttpHandler handler) { if(extendedPattern != null) { return new AccessLogHandler(handler, logReceiver, pattern, extendedPattern, predicate); } else { return new AccessLogHandler(handler, logReceiver, pattern, getClass().getClassLoader(), predicate); } }
private void writeMessage(final List<String> messages) { if (System.currentTimeMillis() > changeOverPoint) { doRotate(); } try { if (writer == null) { boolean created = !Files.exists(defaultLogFile); writer = Files.newBufferedWriter(defaultLogFile, StandardCharsets.UTF_8, StandardOpenOption.APPEND, StandardOpenOption.CREATE); if(Files.size(defaultLogFile) == 0 && fileHeaderGenerator != null) { String header = fileHeaderGenerator.generateHeader(); if(header != null) { writer.write(header); writer.write("\n"); writer.flush(); } } } for (String message : messages) { writer.write(message); writer.write('\n'); } writer.flush(); } catch (IOException e) { UndertowLogger.ROOT_LOGGER.errorWritingAccessLog(e); } }
private DefaultAccessLogReceiver(final Executor logWriteExecutor, final Path outputDirectory, final String logBaseName, final String logNameSuffix, boolean rotate, LogFileHeaderGenerator fileHeader) { this.logWriteExecutor = logWriteExecutor; this.outputDirectory = outputDirectory; this.logBaseName = logBaseName; this.rotate = rotate; this.fileHeaderGenerator = fileHeader; this.logNameSuffix = (logNameSuffix != null) ? logNameSuffix : DEFAULT_LOG_SUFFIX; this.pendingMessages = new ConcurrentLinkedDeque<>(); this.defaultLogFile = outputDirectory.resolve(logBaseName + this.logNameSuffix); calculateChangeOverPoint(); }
@Override public HandlerWrapper build(Map<String, Object> config) { return new Wrapper((String) config.get("format"), (String) config.get("category")); }
public String getWhiteSpaces() throws IOException { if (isEnded()) return ""; StringBuilder whiteSpaces = new StringBuilder(); if (buf.length() > 0) { whiteSpaces.append(buf); buf = new StringBuilder(); } int c = sr.read(); while (Character.isWhitespace((char) c)) { whiteSpaces.append((char) c); c = sr.read(); } if (c == -1) { ended = true; } else { buf.append((char) c); } return whiteSpaces.toString(); }
@Override public void exchangeEvent(final HttpServerExchange exchange, final NextListener nextListener) { try { if(predicate == null || predicate.resolve(exchange)) { accessLogReceiver.logMessage(tokens.readAttribute(exchange)); } } finally { nextListener.proceed(); } } }
@Override public void contextDestroyed(ServletContextEvent sce) { try { this.accessLogReceiver.close(); this.worker.shutdown(); } catch (IOException ex) { throw new IllegalStateException(ex); } }
protected ExchangeAttribute getServerToClientElement( PatternTokenizer tokenizer) throws IOException { if (tokenizer.hasSubToken()) { String token = tokenizer.getToken(); if ("status".equals(token)) { return ResponseCodeAttribute.INSTANCE; } else if ("comment".equals(token)) { return new ConstantExchangeAttribute("?"); } } else if (tokenizer.hasParameter()) { String parameter = tokenizer.getParameter(); if (parameter == null) { UndertowLogger.ROOT_LOGGER.extendedAccessLogMissingClosing(); return null; } return new QuotingExchangeAttribute(new ResponseHeaderAttribute(new HttpString(parameter))); } UndertowLogger.ROOT_LOGGER.extendedAccessLogCannotDecode(tokenizer.getRemains()); return null; }
private AccessLogHandler createAccessLogHandler(HttpHandler handler, AccessLogReceiver accessLogReceiver) { createAccessLogDirectoryIfNecessary(); String formatString = (this.accessLogPattern != null) ? this.accessLogPattern : "common"; return new AccessLogHandler(handler, accessLogReceiver, formatString, Undertow.class.getClassLoader()); }
public AccessLogHandler(final HttpHandler next, final AccessLogReceiver accessLogReceiver, final String formatString, ClassLoader classLoader, Predicate predicate) { this.next = next; this.accessLogReceiver = accessLogReceiver; this.predicate = predicate; this.formatString = handleCommonNames(formatString); this.tokens = ExchangeAttributes.parser(classLoader, new SubstituteEmptyWrapper("-")).parse(this.formatString); }
private void doRotate() { forceLogRotation = false; if (!rotate) { return; } try { if (writer != null) { writer.flush(); writer.close(); writer = null; } if (!Files.exists(defaultLogFile)) { return; } Path newFile = outputDirectory.resolve(logBaseName + currentDateString + "." + logNameSuffix); int count = 0; while (Files.exists(newFile)) { ++count; newFile = outputDirectory.resolve(logBaseName + currentDateString + "-" + count + "." + logNameSuffix); } Files.move(defaultLogFile, newFile); } catch (IOException e) { UndertowLogger.ROOT_LOGGER.errorRotatingAccessLog(e); } finally { calculateChangeOverPoint(); } }
protected ExchangeAttribute getXParameterElement(PatternTokenizer tokenizer) throws IOException { if (!tokenizer.hasSubToken()) { UndertowLogger.ROOT_LOGGER.extendedAccessLogBadXParam(); return null; final String token = tokenizer.getToken(); if (!tokenizer.hasParameter()) { UndertowLogger.ROOT_LOGGER.extendedAccessLogBadXParam(); return null; String parameter = tokenizer.getParameter(); if (parameter == null) { UndertowLogger.ROOT_LOGGER.extendedAccessLogMissingClosing(); return new SubstituteEmptyWrapper.SubstituteEmptyAttribute(parser.parse("%{s," + parameter + "}"),"-"); } else if ("H".equals(token)) { return getServletRequestElement(parameter); } else if ("P".equals(token)) { return new SubstituteEmptyWrapper.SubstituteEmptyAttribute(parser.parse("%{rp," + parameter + "}"),"-");