ServletContext.log () does not register

The log output of my RemoteServiceServlet (GWT) does not appear 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 on Tomcat 6 as well as on Jetty (GWT devmode).

To be 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?

+4
source share
1 answer

The behavior of the ServletContext.log method is container specific. The method I used to make it sequential is to wrap a ServletConfig passed through init () to create a wrapped ServletContext that 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 code Slf4jServletConfigWrapper.java

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)); } 
+3
source

Source: https://habr.com/ru/post/1347265/


All Articles