Saturday, 2 August 2014

Log Records

The Log Record wrap the message sent to the logger. LogRecord class provides number of methods to access various fields in log record like Log level, message, logger name, parameters passed etc.,

Getter methods in LogRecord class
Method Description
getLevel() Get the logging message level
getLoggerName() Get the source Logger's name.
getMessage() Get the raw message before formatting
getMillis() Get event time in milliseconds since 1970.
getParameters() Get the parameters to the log message.
getResourceBundle() Get the localization resource bundle
getResourceBundleName() Get the localization resource bundle name
getSequenceNumber() Get the sequence number.
getSourceClassName() Get the name of the class that issued the logging request.
getSourceMethodName() Get the name of the method that issued the logging request.
getThreadID() Get an identifier for the thread where the message originated.
getThrown() Get any throwable associated with the log record.

Setter methods in LogRecord class

Method Description
setLevel(Level level) Set the logging message level
setLoggerName(String name) Set the source Logger's name.
setMessage(String message) Set the "raw" log message, before localization or formatting.
setMillis(long millis) Set event time.
setParameters(Object[] parameters) Set the parameters to the log message.
setResourceBundle(ResourceBundle bundle) Set the localization resource bundle.
setResourceBundleName(String name) Set the localization resource bundle name.
setSequenceNumber(long seq) Set the sequence number.
setSourceClassName(String sourceClassName) Set the name of the class that issued the logging request.
setSourceMethodName(String sourceMethodName) Set the name of the method that issued the logging request.
setThreadID(int threadID) Set an identifier for the thread where the message originated.
setThrown(Throwable thrown) Set a throwable associated with the log event.

Example
import java.util.logging.*;

public class CustomFormat extends Formatter{
    @Override
    public String format(LogRecord record) {
       StringBuilder str = new StringBuilder();
       
       str.append("Logger Name : ");
       str.append(record.getLoggerName() + "\n");
       str.append("Level of the logger : ");
       str.append(record.getLevel() +"\n");
       str.append("Message : ");
       str.append(record.getMessage() +"\n");
       
       return str.toString();
    }
}

import java.io.IOException;
import java.util.logging.*;

public class FormatEx {
    static final Logger myLogger = Logger.getLogger("Simple Logger");
    
    public static void main(String args[]) throws IOException{
        Handler handler1 = new FileHandler("file1.out");
        CustomFormat format = new CustomFormat();
        
        myLogger.addHandler(handler1);
        myLogger.setLevel(Level.ALL);
        
        handler1.setFormatter(format);
        
        myLogger.log(Level.INFO, "Information Message");
        myLogger.log(Level.WARNING, "Warning Message");
        myLogger.log(Level.SEVERE, "Severe Message");
        
        handler1.close();
    }
}



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment