Monday, June 15, 2009

Java.Util.Logging Example

This example is a simple illustration for using Java.Util.Logging. Java.Util.logging comes with J2SE.

create the following directory structure

->loggingdemo
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp|->Main.java
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp|->logging.properties

logging.properties

# Java Logging Properties
# Author : Deepak Dhanukodi
handlers=java.util.logging.FileHandler, java.util.logging.ConsoleHandler
# Default global logging level.
.level=SEVERE
# Loggers.
loggingdemo.level=ALL
# --- ConsoleHandler ---
# Override of global logging level
java.util.logging.ConsoleHandler.level=ALL
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
# --- FileHandler ---
# Override of global logging level
java.util.logging.FileHandler.level=ALL
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
# Unlimiting size for output file
java.util.logging.FileHandler.limit=0
# Number of output files to cycle through, by appending an
# integer to the base file name:
java.util.logging.FileHandler.count=1
# Append to already existing file
java.util.logging.FileHandler.append=true
java.util.logging.FileHandler.pattern=system.log

Main.java

package loggingdemo;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Filter;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

/**
* Java.utils.logging Demo
* @author Deepak Dhanukodi
*/
public class Main {
static String LOGGING_PROPERTIES = "loggingdemo/logging.properties";
private LogManager logManager;
private Logger logger;
/**
* @param args the command line arguments
*/
public void initialize() throws IOException
{
logManager = LogManager.getLogManager();
Properties prob = new Properties();
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(LOGGING_PROPERTIES);
logManager.readConfiguration(inputStream);
logger = Logger.getLogger(this.getClass().getName());
}
public void log()
{
logger.log(Level.WARNING, "I have hit a warning here.");
logger.warning("Same warning again");
//User defined filter
Filter filter= new Filter() {

public boolean isLoggable(LogRecord record) {
// Allow only FINE and filter the rest
if(Level.FINE == record.getLevel())
return true;
return false;
}
};
Filter oldFilter = logger.getFilter();
logger.setFilter(filter);
logger.severe("You can't reach me.");
logger.finer("You can't reach me, either.");
logger.fine("Horray,I am through.");
logger.setFilter(oldFilter);
logger.severe("You can reach me.");
logger.finer("You can reach me, either.");
logger.fine("Horray,I am through,again.");
}
public static void main(String[] args) throws IOException {
Main main = new Main();
// Initialize logger
main.initialize();
main.log();
}
}

Run

D:\>javac /loggingdemo/Main.java -d .

D:\>java -cp . loggingdemo.Main
15 Jun, 2009 5:10:53 PM loggingdemo.Main log
WARNING: I have hit a warning here.
15 Jun, 2009 5:10:53 PM loggingdemo.Main log
WARNING: Same warning again
15 Jun, 2009 5:10:53 PM loggingdemo.Main log
FINE: Horray,I am through.
15 Jun, 2009 5:10:53 PM loggingdemo.Main log
SEVERE: You can reach me.
15 Jun, 2009 5:10:53 PM loggingdemo.Main log
FINER: You can reach me, either.
15 Jun, 2009 5:10:53 PM loggingdemo.Main log
FINE: Horray,I am through,again.

D:\>

system.log

This log file is created in the same dir where you had executed java process.

15 Jun, 2009 5:10:53 PM loggingdemo.Main log
WARNING: I have hit a warning here.
15 Jun, 2009 5:10:53 PM loggingdemo.Main log
WARNING: Same warning again
15 Jun, 2009 5:10:53 PM loggingdemo.Main log
FINE: Horray,I am through.
15 Jun, 2009 5:10:53 PM loggingdemo.Main log
SEVERE: You can reach me.
15 Jun, 2009 5:10:53 PM loggingdemo.Main log
FINER: You can reach me, either.
15 Jun, 2009 5:10:53 PM loggingdemo.Main log
FINE: Horray,I am through,again.

No comments:

Post a Comment