XLogger.java
import java.text.MessageFormat;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
/**
*
* XLogger is wrapper around the Logger provided by apache.
* This framework will make sure that you are not building a string for
* log until it is not candidate for logging. Logger has provided isXXXEnable method
* to do this but for every logging statement, using this is tedious.
*
* It is also helpful for performance logging.
* @author Viren.Balaut
*
*/
public class XLogger
{
private Logger logger = null;
private boolean isPerfLogger = false;
private long startTime = -1;
private long endTime = -1;
public static XLogger getLogger()
{
return new XLogger(false);
}
public static XLogger getLogger(String name)
{
return new XLogger(name, false);
}
public static XLogger getLogger(Class clazz)
{
return new XLogger(clazz, false);
}
public static XLogger getPerfLogger()
{
return new XLogger(true);
}
public static XLogger getPerfLogger(String name)
{
return new XLogger(name, true);
}
public static XLogger getPerfLogger(Class clazz)
{
return new XLogger(clazz, true);
}
private XLogger(boolean isPerfLogger)
{
logger = LogManager.getRootLogger();
this.isPerfLogger = isPerfLogger;
}
private XLogger(String name, boolean isPerfLogger)
{
logger = LogManager.getLogger(name);
this.isPerfLogger = isPerfLogger;
}
private XLogger(Class clazz, boolean isPerfLogger)
{
logger = LogManager.getLogger(clazz);
this.isPerfLogger = isPerfLogger;
}
public void start()
{
if (logger.isDebugEnabled() && isPerfLogger)
{
startTime = System.currentTimeMillis();
}
}
public void stop()
{
if (logger.isDebugEnabled() && isPerfLogger)
{
if (startTime <= -1)
{
throw new IllegalStateException(
"Please call start() before calling end.");
}
endTime = System.currentTimeMillis();
}
}
public void perf(String message, String methodName)
{
if (logger.isDebugEnabled() && isPerfLogger)
{
if (startTime <= -1 endTime <= -1)
{
throw new IllegalStateException(
"Please call start() and end() both before calling logPerf.");
}
debug(
message + ", Time taken({0}) in MS : {1}",
methodName,
(endTime - startTime));
resetTimer();
}
}
public void console(String message, Object... objects)
{
System.err.println(getMessage(message, objects));
}
public void debug(String message, Object... objects)
{
if (logger.isDebugEnabled())
{
logger.debug(getMessage(message, objects));
}
}
public void info(String message, Object... objects)
{
if (logger.isInfoEnabled())
{
logger.info(getMessage(message, objects));
}
}
public void warn(String message, Object... objects)
{
if (logger.isEnabledFor(Priority.WARN))
{
logger.warn(getMessage(message, objects));
}
}
public void error(String message, Object... objects)
{
if (logger.isEnabledFor(Priority.ERROR))
{
logger.error(getMessage(message, objects));
}
}
public void error(String message, Throwable e, Object... objects)
{
if (logger.isEnabledFor(Priority.ERROR))
{
logger.error(getMessage(message, objects), e);
}
}
public void fatel(String message, Throwable e, Object... objects)
{
if (logger.isEnabledFor(Priority.FATAL))
{
logger.fatal(getMessage(message, objects), e);
}
}
private String getMessage(String message, Object... objects)
{
try
{
MessageFormat form = new MessageFormat(message);
String msg = form.format(objects);
return msg;
}
catch (Exception e)
{
System.err.println("Error occurred while parsing message for "
+ message);
return message;
}
}
private void resetTimer()
{
startTime = -1;
endTime = -1;
}
}
No comments:
Post a Comment