- The java logging is present in java.util.logging package.
- The logging service is useful for developers, administrator and service engineers for their debugging work.
- Different levels of logging are:-
SEVERE (highest value)
WARNING
INFO
CONFIG
FINE
FINER
FINEST (lowest value) -
Set up the Project :-
5. JavaHtmlLoggerFormatter.java
This is a custom formater to log the differernt log messages in html format
package com.sandeep.logging.example;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.LogRecord;
public class JavaHtmlLoggerFormatter extends Formatter{
@SuppressWarnings("static-access")
@Override
public String format(LogRecord record) {
StringBuffer html = new StringBuffer();
if(Level.SEVERE.intValue() == record.getLevel().SEVERE.intValue()){
html.append(""+formatMessage(record)+"");
}
if(Level.WARNING.intValue() == record.getLevel().WARNING.intValue()){
html.append(""+formatMessage(record)+"");
}
return html.toString();
}
}
6. DemoLogger.java
package com.sandeep.logging.example;
import java.io.IOException;
import java.util.logging.Formatter;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DemoLogger {
static private FileHandler htmlFile;
static private Formatter htmlFormatter;
static public void setup() throws IOException {
Logger logger = Logger.getLogger("");
logger.setLevel(Level.SEVERE);
htmlFile = new FileHandler("D:\LogFile.html");
htmlFormatter = new JavaHtmlLoggerFormatter();
htmlFile.setFormatter(htmlFormatter);
logger.addHandler(htmlFile);
}
}
7. JavaLoggingDemo.java
This java file shows how to use it
package com.sandeep.logging.example;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class JavaLoggingDemo {
private final static Logger LOGGER = Logger.getLogger(JavaLoggingDemo.class .getName());
public static void main(String[] args) {
try {
DemoLogger.setup();
} catch (IOException e) {
e.printStackTrace();
}
LOGGER.log(Level.SEVERE, " This is a Severe error");
LOGGER.log(Level.SEVERE, "This is a warning");
}
}
8.Output in D drive
The Java program is run twice and opened in Mozilla.