Switch to full style
General Java code examples
Post a reply

Logging based on severity Level : INFO,Warning,ERROR,Debu

Thu Apr 25, 2013 11:58 pm

Logging based on severity
java code
import java.util.logging.Level;
import java.util.logging.Logger;

public class MyLogger {

protected Logger logger;

private final String className;

protected MyLogger(final Class sourceClass) {
this.className = sourceClass.getSimpleName();
this.logger = Logger.getLogger(sourceClass.getName());

}

public static MyLogger getInstance(final Class sourceClass) {
return new MyLogger(sourceClass);
}

public void logInfo(final String methodName, final String msg) {
if (this.isInfoLoggable()) {
this.logger.logp(Level.INFO, this.className, methodName, msg);
}
}

/**
Log Information about something
*/
public void logInfo(final String methodName, final String msg,
final Object param) {
if (this.isInfoLoggable()) {
this.logger.logp(Level.INFO, this.className, methodName, msg, param);
}
}



/**
* Log a Warning message
*/
public void logWarning(final String methodName, final String msg) {
if (this.isWarningLoggable()) {
this.logger.logp(Level.WARNING, this.className, methodName, msg);
}
}

/**
Log error
*/
public void logError(final String methodName, final String msg) {
if (this.isErrorLoggable()) {
this.logger.logp(Level.SEVERE, this.className, methodName, msg);
}
}

public void debug(final String methodName, final String msg) {
if (this.isDebugLoggable()) {
this.logger.logp(Level.FINE, this.className, methodName, msg);
}
}

public void traceEntry(final String methodName) {

if (this.isTraceEntryLoggable()) {
this.logger.entering(this.className, methodName);
}
}

public void traceExit(final String methodName) {
if (this.isTraceExitLoggable()) {
this.logger.exiting(this.className, methodName);
}
}
public void traceExit(final String methodName, final Object result) {
if (this.isTraceExitLoggable()) {
this.logger.exiting(this.className, methodName, result);
}
}

public void traceException(final String methodName, final String msg,
final Throwable thrown) {
if (this.isTraceExceptionLoggable()) {
this.logger.logp(Level.FINE, this.className, methodName, msg, thrown);
}
}


public Boolean isInfoLoggable() {
return this.logger.isLoggable(Level.INFO);
}
public Boolean isWarningLoggable() {
return this.logger.isLoggable(Level.WARNING);
}
public Boolean isErrorLoggable() {
return this.logger.isLoggable(Level.SEVERE);
}

public Boolean isDebugLoggable() {
return this.logger.isLoggable(Level.FINE);
}

public Boolean isTraceEntryLoggable() {
return this.logger.isLoggable(Level.FINER);
}

public Boolean isTraceExitLoggable() {
return this.logger.isLoggable(Level.FINER);
}

public Boolean isTraceExceptionLoggable() {
return this.logger.isLoggable(Level.FINE);
}
}




Post a reply
  Related Posts  to : Logging based on severity Level : INFO,Warning,ERROR,Debu
 Java Logging     -  
 Free Work at Home Info Pack     -  
 Network Interface Adapter Info using Java APIs.     -  
 Sound Level Meter     -  
 level of conformance in multicast     -  
 high-level thread states     -  
 file exists in upper level folder link     -  
 Cookie based login in php     -  
 prapose me a web based mini project     -  
 Takes a string apart based on a delimiter     -  

Topic Tags

Java Files and I/O