edu.cornell.mannlib.vitro.webapp.utils.log.LogUtils Maven / Gradle / Ivy
/* $This file is distributed under the terms of the license in LICENSE$ */
package edu.cornell.mannlib.vitro.webapp.utils.log;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
/**
* Some static methods that might help with logging for debug purposes.
*/
public class LogUtils {
// ----------------------------------------------------------------------
// Public Static methods
// ----------------------------------------------------------------------
public static String deepFormatForLog(Log log, String level, Object o) {
if (!isLevelEnabled(log, level)) {
return "";
}
return new LogUtils(log).deepFormat(o);
}
public static String formatRequestProperties(Log log, String level,
HttpServletRequest req) {
if (!isLevelEnabled(log, level)) {
return "";
}
return new LogUtils(log).requestProperties(req);
}
// ----------------------------------------------------------------------
// The instance
// ----------------------------------------------------------------------
private String requestProperties(HttpServletRequest req) {
@SuppressWarnings("unchecked")
Map map = req.getParameterMap();
StringBuilder s = new StringBuilder(req.getRequestURL().append('\n').toString());
for (String name : new TreeSet(map.keySet())) {
s.append(" ").append(name).append(" = ").append(Arrays.toString(map.get(name))).append('\n');
}
return s.toString().trim();
}
private static boolean isLevelEnabled(Log log, String level) {
if ("fatal".equalsIgnoreCase(level)) {
return log.isFatalEnabled();
} else if ("error".equalsIgnoreCase(level)) {
return log.isErrorEnabled();
} else if ("warn".equalsIgnoreCase(level)) {
return log.isWarnEnabled();
} else if ("info".equalsIgnoreCase(level)) {
return log.isInfoEnabled();
} else if ("debug".equalsIgnoreCase(level)) {
return log.isDebugEnabled();
} else {
return log.isTraceEnabled();
}
}
private final Log log;
private final List