public

ServletConfig Wrapper to enable Slf4j Logging

  • Download Gist
Slf4jServletConfigWrapper.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
 
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);
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.