package com.javacodegeeks.corejava.util.logging; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; public class LoggerExample { private static final Logger LOGGER = Logger.getLogger(LoggerExample.class.getName()); public static void main(String[] args) throws SecurityException, IOException { LOGGER.info("Logger Name: "+LOGGER.getName()); LOGGER.warning("Can cause ArrayIndexOutOfBoundsException"); //An array of size 3 int []a = {1,2,3}; int index = 4; LOGGER.config("index is set to "+index); try{ System.out.println(a[index]); }catch(ArrayIndexOutOfBoundsException ex){ LOGGER.log(Level.SEVERE, "Exception occur", ex); } } }
package com.javacodegeeks.corejava.util.logging; import java.io.IOException; import java.util.logging.ConsoleHandler; import java.util.logging.FileHandler; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.Logger; public class HandlerExample { private static final Logger LOGGER = Logger.getLogger(LoggerExample.class .getName()); public static void main(String[] args) { Handler consoleHandler = null; Handler fileHandler = null; try { // Creating consoleHandler and fileHandler consoleHandler = new ConsoleHandler(); fileHandler = new FileHandler("./javacodegeeks.log"); // Assigning handlers to LOGGER object LOGGER.addHandler(consoleHandler); LOGGER.addHandler(fileHandler); // Setting levels to handlers and LOGGER consoleHandler.setLevel(Level.ALL); fileHandler.setLevel(Level.ALL); LOGGER.setLevel(Level.ALL); LOGGER.config("Configuration done."); // Console handler removed LOGGER.removeHandler(consoleHandler); LOGGER.log(Level.FINE, "Finer logged"); } catch (IOException exception) { LOGGER.log(Level.SEVERE, "Error occur in FileHandler.", exception); } LOGGER.finer("Finest example on LOGGER handler completed."); } }
package com.javacodegeeks.corejava.util.logging; import java.io.IOException; import java.util.logging.Formatter; import java.util.logging.FileHandler; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.Logger; import java.util.logging.SimpleFormatter; public class FormatterExample { private static final Logger LOGGER = Logger.getLogger(LoggerExample.class .getName()); public static void main(String[] args) { Handler fileHandler = null; Formatter simpleFormatter = null; try { // Creating FileHandler fileHandler = new FileHandler("./javacodegeeks.formatter.log"); // Creating SimpleFormatter simpleFormatter = new SimpleFormatter(); // Assigning handler to logger LOGGER.addHandler(fileHandler); // Logging message of Level info (this should be publish in the // default format i.e. XMLFormat) LOGGER.info("Finnest message: Logger with DEFAULT FORMATTER"); // Setting formatter to the handler fileHandler.setFormatter(simpleFormatter); // Setting Level to ALL fileHandler.setLevel(Level.ALL); LOGGER.setLevel(Level.ALL); // Logging message of Level finest (this should be publish in the // simple format) LOGGER.finest("Finnest message: Logger with SIMPLE FORMATTER"); } catch (IOException exception) { LOGGER.log(Level.SEVERE, "Error occur in FileHandler.", exception); } } }
package com.javacodegeeks.corejava.util.logging; import java.util.logging.Filter; import java.util.logging.LogRecord; import java.util.logging.Logger; public class FilterExample implements Filter{ private static final Logger LOGGER = Logger.getLogger(LoggerExample.class.getName()); public static void main(String[] args) { //Setting filter FilterExample LOGGER.setFilter(new FilterExample()); //Since this message string does not contain the word important. Despite of being the Level SEVERE this will be ignored LOGGER.severe("This is SEVERE message"); //This will get published LOGGER.warning("This is important warning message"); } // This method will return true only if the LogRecord object contains the message which contains the word important @Override public boolean isLoggable(LogRecord record) { if(record == null) return false; String message = record.getMessage()==null?"":record.getMessage(); if(message.contains("important")) return true; return false; } }
package com.javacodegeeks.corejava.util.logging; import java.io.FileInputStream; import java.io.IOException; import java.util.logging.Level; import java.util.logging.LogManager; import java.util.logging.Logger; public class ConfigurationExample { private static final LogManager logManager = LogManager.getLogManager(); private static final Logger LOGGER = Logger.getLogger("confLogger"); static{ try { logManager.readConfiguration(new FileInputStream("./javacodegeeks.properties")); } catch (IOException exception) { LOGGER.log(Level.SEVERE, "Error in loading configuration",exception); } } public static void main(String[] args) { LOGGER.fine("Fine message logged"); } }
javacodegeeks.propertiesjava
handlers=java.util.logging.ConsoleHandler .level=ALL java.util.logging.ConsoleHandler.level=ALL java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter confLogger.level=ALL