gwt - ServletContext.log() 不记录

标签 gwt logging guice slf4j servlets

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

对于依赖注入(inject),我使用 谷歌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?

最佳答案

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);
      }
...

Full Slf4jServletConfigWrapper.java code

在您的 Servlet 中重写 init() 方法以使用 ServletConfig 包装器
public void init(final ServletConfig config) throws ServletException {
  super.init(new Slf4jServletConfigWrapper(log, config));
}

关于gwt - ServletContext.log() 不记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5596729/

相关文章:

python - 在哪里调用 Django 中的 logging.setLoggerClass()

java - 指南:范围相关问题

java - 如何使用 google guice 将两个类绑定(bind)到一个类中?

gwt - GWT 样式单位 PC 和 PCT 之间的区别

java - IE8 不在元素中注册模糊事件 - 在 dataGrid 中使用 ConsumerEvents

c# - 为什么.NET Core DI 容器不注入(inject) ILogger?

java - 为什么在使用 Guice 时,Java 中的 @Singleton 的 @Inject 总是导致 null?

java - 设置中心 : not a LatLng or LatLngLiteral: in property lat: not a number

gwt - 如何从 GWT 中识别 http 错误代码?

logging - Logstash 输入插件 : Redis vs Elasticsearch