/**
 * $Revision: $
 * $Date: $
 *
 * Copyright (C) 2006 Jive Software. All rights reserved.
 *
 * This software is published under the terms of the GNU Lesser Public License (LGPL),
 * a copy of which is included in this distribution.
 */

package org.jivesoftware.spark.util.log;

import org.jivesoftware.Spark;

import java.io.File;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.SimpleFormatter;

/**
 * Creates and writes out messages to a a log file. This should be used for all error handling within
 * the Agent application.
 */
public class Log {
    private static File LOG_FILE;
    private static java.util.logging.Logger LOGGER;

    private Log() {
        // Do not allow initialization
    }

    static {
        if (!Spark.getLogDirectory().exists()) {
            Spark.getLogDirectory().mkdirs();
        }

        LOG_FILE = new File(Spark.getLogDirectory(), "errors.log");


        try {
            // Create an appending file handler
            boolean append = true;
            FileHandler handler = new FileHandler(LOG_FILE.getCanonicalPath(), append);
            handler.setFormatter(new SimpleFormatter());

            // Add to the desired logger
            LOGGER = java.util.logging.Logger.getAnonymousLogger();
            LOGGER.addHandler(handler);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Logs all error messages to default error logger.
     *
     * @param message a message to append to log file.
     * @param ex      the exception being thrown.
     */
    public static void error(String message, Throwable ex) {
        LOGGER.log(Level.SEVERE, message, ex);
    }

    /**
     * Logs all error messages to default error logger.
     *
     * @param ex the exception being thrown.
     */
    public static void error(Throwable ex) {
        LOGGER.log(Level.SEVERE, "", ex);
    }

    /**
     * Log a warning message to the default logger.
     *
     * @param message the message to log.
     * @param ex      the exception.
     */
    public static void warning(String message, Throwable ex) {
        ex.printStackTrace();
    }

    public static void warning(String message) {
        LOGGER.log(Level.WARNING, message);
    }

    /**
     * Logs all error messages to default error logger.
     *
     * @param message a message to append to log file.
     */
    public static void error(String message) {
        LOGGER.log(Level.SEVERE, message);
    }

    /**
     * Logs all messages to standard errout for debugging purposes.
     * To use, pass in the VM Parameters debug.mode=true.
     * <p/>
     * ex. (-Ddebug.mode=true)
     *
     * @param message the message to print out.
     */
    public static void debug(String message) {
        if (System.getProperty("debug.mode") != null) {
            LOGGER.info(message);
        }
    }

}

@@ -445,16 +445,50 @@
 Level;%0D%0A
+import java.util.logging.Logger;%0D%0A
 import j
@@ -708,16 +708,22 @@
 ic File 
+ERROR_
 LOG_FILE
@@ -769,22 +769,116 @@
 .Logger 
-LOGGER
+ERROR_LOGGER;%0D%0A%0D%0A    private static Logger WARNING_LOGGER;%0D%0A    private static File WARNING_LOG_FILE
 ;%0D%0A%0D%0A   
@@ -1076,16 +1076,22 @@
         
+ERROR_
 LOG_FILE
@@ -1143,16 +1143,91 @@
 log%22);%0D%0A
+        WARNING_LOG_FILE = new File(Spark.getLogDirectory(), %22warn.log%22);%0D%0A
 %0D%0A%0D%0A    
@@ -1346,17 +1346,186 @@
 Handler 
-h
+errorHandler = new FileHandler(ERROR_LOG_FILE.getCanonicalPath(), append);%0D%0A            errorHandler.setFormatter(new SimpleFormatter());%0D%0A%0D%0A            FileHandler warnH
 andler =
@@ -1541,16 +1541,24 @@
 Handler(
+WARNING_
 LOG_FILE
@@ -1600,17 +1600,21 @@
         
-h
+warnH
 andler.s
@@ -1702,24 +1702,30 @@
             
+ERROR_
 LOGGER = jav
@@ -1777,24 +1777,30 @@
             
+ERROR_
 LOGGER.addHa
@@ -1805,17 +1805,136 @@
 Handler(
-h
+errorHandler);%0D%0A%0D%0A            WARNING_LOGGER = Logger.getAnonymousLogger();%0D%0A            WARNING_LOGGER.addHandler(warnH
 andler);
@@ -2285,32 +2285,38 @@
  ex) %7B%0D%0A        
+ERROR_
 LOGGER.log(Level
@@ -2523,32 +2523,38 @@
  ex) %7B%0D%0A        
+ERROR_
 LOGGER.log(Level
@@ -2817,27 +2817,53 @@
     
-ex.printStackTrace(
+WARNING_LOGGER.log(Level.WARNING, message, ex
 );%0D%0A
@@ -2921,32 +2921,40 @@
 age) %7B%0D%0A        
+WARNING_
 LOGGER.log(Level
@@ -3174,24 +3174,30 @@
  %7B%0D%0A        
+ERROR_
 LOGGER.log(L
@@ -3594,16 +3594,22 @@
         
+ERROR_
 LOGGER.i

Update logger.