ServletContext.log() 不记录日志

发布于 2024-10-31 02:30:31 字数 752 浏览 2 评论 0原文

使用 getServletContext().log("anything"); 时,我的 RemoteServiceServlet (GWT) 的日志输出未显示在日志文件或标准输出中;

对于依赖项注入,我使用 谷歌Guice。对于我自己的日志输出,我使用 slf4j-jdk14。我在 Tomcat 6 和 Jetty(GWT devmode)中尝试过。

说清楚一点,我的Servlet:

@Singleton
public class MyServiceServlet extends RemoteServiceServlet implements MyService {

    private static final Logger log = LoggerFactory.getLogger(MyServiceServlet.class);
    private final ADependency dep;

    @Inject
    public MyServiceServlet(ADependency dep) {
        getServletContext().log("THIS IS NOT SHOWN IN MY LOGS");
        log.error("THIS IS SHOWN IN MY LOGS");
        this.dep = dep;
    }
}

那么,我在哪里可以找到丢失的日志输出或者在哪里可以配置ServletContext-Log?

Log output of my RemoteServiceServlet (GWT) is not shown in Logfiles or Stdout when using getServletContext().log("anything");

For dependency injection I use Google Guice. For my own log output I use slf4j-jdk14. I tried this in Tomcat 6 as well as in Jetty (GWT devmode).

To make it clear, my Servlet:

@Singleton
public class MyServiceServlet extends RemoteServiceServlet implements MyService {

    private static final Logger log = LoggerFactory.getLogger(MyServiceServlet.class);
    private final ADependency dep;

    @Inject
    public MyServiceServlet(ADependency dep) {
        getServletContext().log("THIS IS NOT SHOWN IN MY LOGS");
        log.error("THIS IS SHOWN IN MY LOGS");
        this.dep = dep;
    }
}

So, where can I find the missing log output or where can I configure the ServletContext-Log?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦旅人picnic 2024-11-07 02:30:33

ServletContext.log 方法行为是特定于容器的。我用来使其保持一致的方法是包装通过 init() 传入的 ServletConfig,以便创建一个包装的 ServletContext,它使用我们自己提供的记录器(在本例中为 Slf4j)。

public class Slf4jServletConfigWrapper implements ServletConfig {
  private final ServletConfig config;
  private final Logger log;

  public Slf4jServletConfigWrapper(Logger log, ServletConfig config) {
    this.log = log;
    this.config = config;
  }

  public ServletContext getServletContext() {
    return new ServletContext() {
      public void log(String message, Throwable throwable) {
        log.info(message, throwable);
      }

      public void log(Exception exception, String msg) {
        log.info(msg, exception);
      }

      public void log(String msg) {
        log.info(msg);
      }
...

完整的 Slf4jServletConfigWrapper.java 代码

在您的 Servlet 中重写 init() 方法以使用 ServletConfig 包装器

public void init(final ServletConfig config) throws ServletException {
  super.init(new Slf4jServletConfigWrapper(log, config));
}

The ServletContext.log method behavior is container specific. The method I have used to make it consistent is to wrap the ServletConfig passed in through init() in order to create a wrapped ServletContext which uses our own provided logger (Slf4j in this case).

public class Slf4jServletConfigWrapper implements ServletConfig {
  private final ServletConfig config;
  private final Logger log;

  public Slf4jServletConfigWrapper(Logger log, ServletConfig config) {
    this.log = log;
    this.config = config;
  }

  public ServletContext getServletContext() {
    return new ServletContext() {
      public void log(String message, Throwable throwable) {
        log.info(message, throwable);
      }

      public void log(Exception exception, String msg) {
        log.info(msg, exception);
      }

      public void log(String msg) {
        log.info(msg);
      }
...

Full Slf4jServletConfigWrapper.java code

In your Servlet override the init() method to use the ServletConfig wrapper

public void init(final ServletConfig config) throws ServletException {
  super.init(new Slf4jServletConfigWrapper(log, config));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文