1 23 24 package org.apache.slide.util.logger.jdk14; 25 26 import java.util.logging.ConsoleHandler ; 27 import java.util.logging.Formatter ; 28 import java.util.logging.Logger ; 29 import java.util.logging.Level ; 30 import java.util.logging.FileHandler ; 31 import java.util.logging.Handler ; 32 33 import java.io.*; 34 35 39 public class LoggerConfiguration { 40 41 private final static Logger logger = Logger.getLogger(LoggerConfiguration.class.getName()); 42 43 public final static String DEFAULT_LOG_FILE = "Slide.log"; 44 public final static Level DEFAULT_LOG_LEVEL = Level.ALL; 45 public final static String DEFAULT_FORMAT_STRING = SimpleLogfileFormatter.SIMPLE_FORMAT_STRING; 46 public final static Formatter DEFAULT_FORMATTER = new SimpleLogfileFormatter(DEFAULT_FORMAT_STRING); 48 public final static Formatter DETAIL_FORMATTER = new LogfileFormatter(DEFAULT_FORMAT_STRING); 49 50 private static boolean isConfigured = false; 51 52 public final static void configure() { 53 configure(DEFAULT_LOG_FILE, DEFAULT_LOG_LEVEL, Level.INFO); 54 } 55 56 60 public final static void configure(String logFilePath, Level logLevel, Level logToConsoleThreshold) { 61 try { 62 if (isConfigured) 63 return; 64 isConfigured = true; 65 Handler [] handlers = Logger.getLogger("").getHandlers(); 67 for (int i = 0; i < handlers.length; i++) { 68 Handler handler = handlers[i]; 69 if (handler instanceof ConsoleHandler ) { 70 handler.setFormatter(new ConsoleLogfileFormatter()); 71 handler.setLevel(logToConsoleThreshold); 72 } 74 } 75 Logger.getLogger("").setLevel(logLevel); 77 Handler fileHandler = new FileHandler (logFilePath); 78 fileHandler.setFormatter(DETAIL_FORMATTER); 79 Logger.getLogger("").addHandler(fileHandler); 80 logger.log(Level.INFO, "Logging Service configured"); 81 } catch (IOException ioe) { 82 logger.log(Level.WARNING, "Could not set logging configuration", ioe); 83 } 84 } 85 } 86 | Popular Tags |