Skip to content

Instantly share code, notes, and snippets.

@electrum
Created September 23, 2010 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save electrum/594525 to your computer and use it in GitHub Desktop.
Save electrum/594525 to your computer and use it in GitHub Desktop.
Java log wrapper that uses string formatting
import org.apache.commons.logging.LogFactory;
@SuppressWarnings({"UnusedDeclaration"})
public class Log
{
private final org.apache.commons.logging.Log log;
private Log(String name)
{
log = LogFactory.getLog(name);
}
public static Log getLogger()
{
return getLogger(getCallerClassName());
}
public static Log getLogger(Class clazz)
{
return getLogger(clazz.getName());
}
public static Log getLogger(String name)
{
return new Log(name);
}
public void debug(Throwable t)
{
log.debug(t.getMessage(), t);
}
public void info(Throwable t)
{
log.info(t.getMessage(), t);
}
public void warn(Throwable t)
{
log.warn(t.getMessage(), t);
}
public void error(Throwable t)
{
log.error(t.getMessage(), t);
}
public void debug(String format, Object... args)
{
if (log.isDebugEnabled()) {
log.debug(String.format(format, args));
}
}
public void info(String format, Object... args)
{
if (log.isInfoEnabled()) {
log.info(String.format(format, args));
}
}
public void warn(String format, Object... args)
{
if (log.isWarnEnabled()) {
log.warn(String.format(format, args));
}
}
public void error(String format, Object... args)
{
if (log.isErrorEnabled()) {
log.error(String.format(format, args));
}
}
public void debug(Throwable t, String format, Object... args)
{
if (log.isDebugEnabled()) {
log.debug(String.format(format, args), t);
}
}
public void info(Throwable t, String format, Object... args)
{
if (log.isInfoEnabled()) {
log.info(String.format(format, args), t);
}
}
public void warn(Throwable t, String format, Object... args)
{
if (log.isWarnEnabled()) {
log.warn(String.format(format, args), t);
}
}
public void error(Throwable t, String format, Object... args)
{
if (log.isErrorEnabled()) {
log.error(String.format(format, args), t);
}
}
private static String getCallerClassName()
{
return Thread.currentThread().getStackTrace()[3].getClassName();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment