package grails.plugin.lightweightdeploy.logging;

import ch.qos.logback.classic.Level;
import com.google.common.base.Optional;

import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;

public class FileLoggingConfiguration {

    private String logFilePath;
    private TimeZone logFileTimeZone = TimeZone.getDefault();
    private Level loggingThreshold = Level.ALL;
    private Level rootLevel = Level.INFO;
    private Map<String, Level> loggers = new HashMap<String, Level>();

    public FileLoggingConfiguration(Map<String, ?> config) {
        this.logFilePath = config.get("currentLogFilename").toString();
        if (config.containsKey("timeZone")) {
            setLogFileTimeZone(TimeZone.getTimeZone(config.get("timeZone").toString()));
        }
        if (config.containsKey("threshold")) {
            setLoggingThreshold(Level.toLevel(config.get("threshold").toString()));
        }
        if (config.containsKey("rootLevel")) {
            setRootLevel(Level.toLevel(config.get("rootLevel").toString()));
        }
        if (config.containsKey("loggers")) {
            for (Map.Entry<String, ?> entry : ((Map<String, ?>) config.get("loggers")).entrySet()) {
                addLogger(entry.getKey(), Level.toLevel(entry.getValue().toString()));
            }
        }
    }

    public void addLogger(String packagePath, Level level) {
        this.loggers.put(packagePath, level);
    }

    public Level getThreshold() {
        return loggingThreshold;
    }

    public TimeZone getTimeZone() {
        return this.logFileTimeZone;
    }
    
    public boolean isArchive() {
        //not currently supported because of bug in logback's file rolling.
        return false;
    }
    
    public String getCurrentLogFilename() {
        return this.logFilePath;
    }
    
    public String getArchivedLogFilenamePattern() {
        //not currently supported because of bug in logback's file rolling.
        return null;
    }
    
    public int getArchivedFileCount() {
        //not currently supported because of bug in logback's file rolling.
        return 0;
    }

    public Optional<String> getLogFormat() {
        //TODO: support custom log format
        return Optional.absent();
    }

    public Level getRootLevel() {
        return this.rootLevel;
    }

    public Map<String, Level> getLoggers() {
        return loggers;
    }

    public void setLogFileTimeZone(TimeZone logFileTimeZone) {
        this.logFileTimeZone = logFileTimeZone;
    }

    public void setLoggingThreshold(Level loggingThreshold) {
        this.loggingThreshold = loggingThreshold;
    }

    public void setRootLevel(Level rootLevel) {
        this.rootLevel = rootLevel;
    }
}

@@ -492,16 +492,46 @@
 evel%3E();
+%0A    private String logFormat;
 %0A%0A    pu
@@ -1330,24 +1330,143 @@
 %7D%0A        %7D%0A
+        if (config.containsKey(%22logFormat%22)) %7B%0A            setLogFormat(config.get(%22logFormat%22).toString());%0A        %7D%0A
     %7D%0A%0A    p
@@ -2312,50 +2312,8 @@
 ) %7B%0A
-        //TODO: support custom log format%0A
     
@@ -2336,15 +2336,30 @@
 nal.
-absent(
+fromNullable(logFormat
 );%0A 
@@ -2789,32 +2789,32 @@
 el rootLevel) %7B%0A
-
         this.roo
@@ -2838,11 +2838,103 @@
 l;%0A    %7D
+%0A%0A    public void setLogFormat(String logFormat) %7B%0A        this.logFormat = logFormat;%0A    %7D
 %0A%7D%0A

Allow configuration of the log format