/*
 * Copyright 2012 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Eclipse Public License version 1.0, available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package org.jboss.aesh.util;

import org.jboss.aesh.console.Config;

import java.io.File;
import java.io.IOException;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import java.util.logging.StreamHandler;

/**
 * butt ugly logger util, but its simple and gets the job done (hopefully not too dangerous)
 * warning: made it even uglier when Settings was changed to not be a Singleton... gah!
 *
 * @author <a href="mailto:stale.pedersen@jboss.org">Ståle W. Pedersen</a>
 */
public class LoggerUtil {

    private static Handler logHandler;

    private static void createLogHandler(String log) {
        try {
            File logFile = new File(log);
            if(logFile.getParentFile() != null && !logFile.getParentFile().isDirectory()) {
                if(!logFile.getParentFile().mkdirs()) {
                    //if creating dirs failed, just create a logger without a file handler
                    createLogHandler(new ConsoleHandler());
                    return;
                }
            }
            else if(logFile.isDirectory()) {
                logFile = new File(logFile.getAbsolutePath()+ Config.getPathSeparator()+"aesh.log");
            }
            createLogHandler(new FileHandler(logFile.getAbsolutePath()));
        }
        catch (IOException e) {
            createLogHandler(new ConsoleHandler());
        }

    }

    private static void createLogHandler(StreamHandler handler) {
        logHandler = handler;
        logHandler.setFormatter(new SimpleFormatter());
    }

    /**
     *
     * @param name class
     * @return logger
     */
    public static synchronized Logger getLogger(String name) {
        if(logHandler == null) {
            //just create a default logHandler
            createLogHandler(Config.getTmpDir()+Config.getPathSeparator()+"aesh.log");
        }

        if(logHandler == null) {
            return Logger.getLogger(name);
        }

        Logger log =  Logger.getLogger(name);
        log.setUseParentHandlers(false);
        log.addHandler(logHandler);

        return log;
    }

}

@@ -784,16 +784,89 @@
 sen%3C/a%3E%0A
+ * @author %3Ca href=%22mailto:danielsoro@gmail.com%22%3EDaniel Cunha (soro)%3C/a%3E%0A
  */%0Apubl
@@ -1055,70 +1055,64 @@
     
-if(logFile.getParentFile() != null && !logFile.getParent
+createDirsToParentFile(logFile);%0A%0A            if(log
 File
-()
 .isD
@@ -1145,47 +1145,92 @@
     
-if(!logFile.getParentFile().mkdirs()) %7B
+logFile = new File(logFile.getAbsolutePath()+ Config.getPathSeparator()+%22aesh.log%22);
 %0A   
@@ -1227,32 +1227,37 @@
 log%22);%0A         
+   %7D%0A
            //if 
@@ -1255,90 +1255,113 @@
     
-//if
  creat
-ing dirs failed, just create a logger without a file handler%0A           
+eLogHandler(new FileHandler(logFile.getAbsolutePath()));%0A        %7D%0A        catch (IOException e) %7B%0A
     
@@ -1357,32 +1357,35 @@
 n e) %7B%0A         
+   
 createLogHandler
@@ -1420,219 +1420,127 @@
     
-            return;%0A                %7D%0A            %7D%0A            else if(logFile.isDirectory()) %7B%0A                logFile = new File(logFile.getAbsolutePath()+ Config.getPathSeparator()+%22aesh.log%22);%0A            %7D
+%7D%0A%0A    %7D%0A%0A    private static void createDirsToParentFile(File logFile) %7B%0A        if(isCreateNewDirsToParent(logFile)) %7B
 %0A   
@@ -1561,34 +1561,37 @@
 eLogHandler(new 
-Fi
+Conso
 leHandler(logFil
@@ -1588,35 +1588,30 @@
 ler(
-logFile.getAbsolutePath()))
+));%0A            return
 ;%0A  
@@ -1626,98 +1626,204 @@
     
+%7D%0A%0A
     
-catch (IOException e) %7B%0A            createLogHandler(new ConsoleHandler());%0A        %7D%0A
+private static boolean isCreateNewDirsToParent(File logFile) %7B%0A        return logFile.getParentFile() != null && !logFile.getParentFile().isDirectory() && !logFile.getParentFile().mkdirs();
 %0A   
@@ -1989,78 +1989,8 @@
  %7D%0A%0A
-    /**%0A     *%0A     * @param name class%0A     * @return logger%0A     */%0A
     
@@ -2085,55 +2085,8 @@
 ) %7B%0A
-            //just create a default logHandler%0A
     

Clean LoggerUtil