Java - general

Java logging

For the moment, the simple example of a logging shall be enough on the console:

import java.util.logging.*;
 
public class LoggingDemo {
   static Logger LOGGER = Logger.getLogger("LoggingDemo");
   static Handler handler;
 
   public LoggingDemo() {
      // Create Console Handler:
      handler = new ConsoleHandler();
 
      // Put Log level from the Handler on FINEST:
      handler.setLevel(Level.FINEST);
      LOGGER.addHandler(handler);
      LOGGER.setUseParentHandlers(false);
 
      // Hand out a Log-notification:
      LOGGER.finest("Juju, we created a Log-notification.");
   }
}




The Logging can also be controled by a file logging.properties.
Determine from where the file shall be loaded:

try {
   String strLogPath = "/log/logging.properties";  // Determine place of logging.properties 
   File fileLog = new File(strLogPath);
   LogManager.getLogManager().readConfiguration(new FileInputStream(fileLog));
}catch (Exception e){
   e.printStackTrace();
}


An exemplary logging.properties:

#determine Handler:
#The ConsoleHandler hands out the Log-notification on the Console:
handlers = java.util.logging.ConsoleHandler
#There can also be more Handler active; here Console- and FileHandler:
#handlers = java.util.logging.ConsoleHandler, java.util.logging.FileHandler

#Determination of the Default Loglevels:
.level= INFO

#Maximal size of the Log-file in Byte:
java.util.logging.FileHandler.limit = 100000
#Number of files which shall be used:
java.util.logging.FileHandler.count = 1
#The formatter; determines the formatting of the distribution:
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.FileHandler.pattern = MyApplication.log


Logging the stacktrace:

LOGGER.log(Level.SEVERE, "Message", e);

Will be completed…


Eigene Werkzeuge
Werkzeuge

gratis Counter by GOWEB
seit 9.10.2007