Skip to content

Instantly share code, notes, and snippets.

@jeffcrowe
Created July 4, 2012 18:30
Show Gist options
  • Save jeffcrowe/3048784 to your computer and use it in GitHub Desktop.
Save jeffcrowe/3048784 to your computer and use it in GitHub Desktop.
ServletConfig Wrapper to enable Slf4j Logging
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;
}
@Override
@SuppressWarnings("deprecation")
public ServletContext getServletContext() {
return new ServletContext() {
@Override
public void log(String message, Throwable throwable) {
log.info(message, throwable);
}
@Override
public void log(Exception exception, String msg) {
log.info(msg, exception);
}
@Override
public void log(String msg) {
log.info(msg);
}
@Override
public void setAttribute(String name, Object object) {
config.getServletContext().setAttribute(name, object);
}
@Override
public void removeAttribute(String name) {
config.getServletContext().removeAttribute(name);
}
@Override
public Enumeration<?> getServlets() {
return config.getServletContext().getServlets();
}
@Override
public Enumeration<?> getServletNames() {
return config.getServletContext().getServletNames();
}
@Override
public String getServletContextName() {
return config.getServletContext().getServletContextName();
}
@Override
public Servlet getServlet(String name) throws ServletException {
return config.getServletContext().getServlet(name);
}
@Override
public String getServerInfo() {
return config.getServletContext().getServerInfo();
}
@Override
public Set<?> getResourcePaths(String path) {
return config.getServletContext().getResourcePaths(path);
}
@Override
public InputStream getResourceAsStream(String path) {
return config.getServletContext().getResourceAsStream(path);
}
@Override
public URL getResource(String path) throws MalformedURLException {
return config.getServletContext().getResource(path);
}
@Override
public RequestDispatcher getRequestDispatcher(String path) {
return config.getServletContext().getRequestDispatcher(path);
}
@Override
public String getRealPath(String path) {
return config.getServletContext().getRealPath(path);
}
@Override
public RequestDispatcher getNamedDispatcher(String name) {
return config.getServletContext().getNamedDispatcher(name);
}
@Override
public int getMinorVersion() {
return config.getServletContext().getMinorVersion();
}
@Override
public String getMimeType(String file) {
return config.getServletContext().getMimeType(file);
}
@Override
public int getMajorVersion() {
return config.getServletContext().getMajorVersion();
}
@Override
public Enumeration<?> getInitParameterNames() {
return config.getServletContext().getInitParameterNames();
}
@Override
public String getInitParameter(String name) {
return config.getServletContext().getInitParameter(name);
}
@Override
public String getContextPath() {
return config.getServletContext().getContextPath();
}
@Override
public ServletContext getContext(String uripath) {
return config.getServletContext().getContext(uripath);
}
@Override
public Enumeration<?> getAttributeNames() {
return config.getServletContext().getAttributeNames();
}
@Override
public Object getAttribute(String name) {
return config.getServletContext().getAttribute(name);
}
};
}
@Override
public String getServletName() {
return config.getServletName();
}
@Override
public Enumeration<?> getInitParameterNames() {
return config.getInitParameterNames();
}
@Override
public String getInitParameter(String name) {
return config.getInitParameter(name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment