java - 在 DropWizard 或 Jetty 应用程序中导致 404 的日志 URL

标签 java jax-rs jetty dropwizard

我们正在运行 DropWizard 并尝试删除导致抛出 404 响应的 URL 的日志记录

我们有一个接收 NotFoundException 的包罗万象的异常映射器。令人沮丧的是,该异常没有携带导致它被抛出的 URL 的上下文。

此处的示例应用程序:https://github.com/pauldambra/not.found.example

我们正在使用 ExceptionMapper

public class NotFoundLogger implements ExceptionMapper<NotFoundException> {

    ExampleLogger logger = new ExampleLogger();

    @Override
    public Response toResponse(final NotFoundException exception) {
        logger.error(urlFrom(exception), exception);
        return Response.status(404).build();
    }

    private String urlFrom(final NotFoundException exception) {
        return "why is this not a property on the exception?!";
    }

    private class ExampleLogger {
        void error(final String notFoundUrl, final NotFoundException exception) {
            System.out.println("someone tried to load " + notFoundUrl);
            System.out.println(exception.getMessage());
        }
    }
}

如果我们在有人请求应用程序未提供的 URL 时查看应用程序日志,我们会看到应用程序可以记录它正在为路径返回 404,但我们的自定义记录器无法访问该 URL

someone tried to load why is this not a property on the exception?!
HTTP 404 Not Found
127.0.0.1 - - [08/May/2019:09:53:47 +0000] "GET /ping/pong HTTP/1.1" 404 

ExceptionMapper 是执行此操作的错误方法吗?

最佳答案

原来有两种方法

有人确实使用了异常映射器:

public class NotFoundLogger implements ExceptionMapper<NotFoundException> {

    // magically inject a thing
    // remember that magic is for evil wizards
    @Context
    private HttpServletRequest request;

    private ExampleLogger logger = new ExampleLogger();

    @Override
    public Response toResponse(final NotFoundException exception) {
        final StringBuffer absolutePath = HttpUtils.getRequestURL(request);
        logger.error("exception mapper: " + absolutePath, exception);
        return Response.status(404).build();
    }
}

这有效但不是很容易被发现。

您还可以添加响应过滤器

public class NotFoundLoggingFilter implements ContainerResponseFilter {
    private ExampleLogger logger = new ExampleLogger();

    @Override
    public void filter(ContainerRequestContext requestContext,
                       ContainerResponseContext responseContext) {
        if (responseContext.getStatus() != 404) {
            return;
        }

        final URI absolutePath = requestContext.getUriInfo().getAbsolutePath();
        logger.error("filter: " + absolutePath, new NotFoundException());
    }
}

这不需要任何魔法,所以适合我,但你可以选择你的毒药。

实际上,路径应该在 NotFoundException 上 - 如果我有更多时间,我会建议更改代码以添加它。

关于java - 在 DropWizard 或 Jetty 应用程序中导致 404 的日志 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56038001/

相关文章:

java - 获取 OAUTH2 token

java - 可嵌入的 Java HTTP 服务器

java - 嵌入式 Jetty 请求内容解压

java - Java VM 是否跳过零乘法?

java - SLF4J : Class path contains multiple SLF4J bindings. 多个 logback-classic jar

java - @Stateless webservice with JPA+JTA : How to commit changes of managed entity?

java - 在独立 Jetty 服务器中禁用 Web 应用程序自动重新加载

java - 尝试在 Ubuntu 15.04 上的 Tomcat8 上部署 WAR 时出错

java - 如何比较Java中的字符串?

java - 自定义 Json 序列化程序而不是 Jersey 的默认序列化程序?