/*
 * RESTHeart - the Web API for MongoDB
 * Copyright (C) 2014 - 2015 SoftInstigate Srl
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.restheart.utils;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Appender;
import ch.qos.logback.core.rolling.FixedWindowRollingPolicy;
import ch.qos.logback.core.rolling.RollingFileAppender;
import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Andrea Di Cesare <andrea@softinstigate.com>
 */
public class LoggingInitializer {

    /**
     *
     * @param level
     */
    public static void setLogLevel(Level level) {
        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
        Logger logger = loggerContext.getLogger("org.restheart");

        logger.setLevel(level);
    }

    /**
     *
     */
    public static void stopConsoleLogging() {
        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
        Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);

        Appender<ILoggingEvent> appender = rootLogger.getAppender("STDOUT");
        
        appender.stop();
    }

    /**
     *
     * @param logFilePath
     */
    public static void startFileLogging(String logFilePath) {
        Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);

        LoggerContext loggerContext = logger.getLoggerContext();

        RollingFileAppender<ILoggingEvent> rfAppender = new RollingFileAppender<>();
        rfAppender.setContext(loggerContext);
        rfAppender.setFile(logFilePath);

        FixedWindowRollingPolicy fwRollingPolicy = new FixedWindowRollingPolicy();
        fwRollingPolicy.setContext(loggerContext);
        fwRollingPolicy.setFileNamePattern(logFilePath + "-%i.log.zip");
        fwRollingPolicy.setParent(rfAppender);
        fwRollingPolicy.start();

        SizeBasedTriggeringPolicy<ILoggingEvent> triggeringPolicy = new SizeBasedTriggeringPolicy<>();
        triggeringPolicy.setMaxFileSize("5MB");
        triggeringPolicy.start();

        PatternLayoutEncoder encoder = new PatternLayoutEncoder();
        encoder.setContext(loggerContext);
        encoder.setPattern("%d{HH:mm:ss.SSS} [%thread] %highlight(%-5level) %logger{36} - %msg%n");
        encoder.start();

        rfAppender.setEncoder(encoder);
        rfAppender.setRollingPolicy(fwRollingPolicy);
        rfAppender.setTriggeringPolicy(triggeringPolicy);
        rfAppender.start();

        logger.addAppender(rfAppender);
    }

    private LoggingInitializer() {
    }
}

@@ -789,16 +789,61 @@
 utils;%0A%0A
+import ch.qos.logback.classic.AsyncAppender;%0A
 import c
@@ -1413,16 +1413,129 @@
 izer %7B%0A%0A
+    private static final int ASYNC_MAX_FLUSH_TIME = 5000;%0A    private static final int ASYNC_QUEUE_SIZE = 1024;%0A%0A
     /**%0A
@@ -2128,24 +2128,16 @@
 DOUT%22);%0A
-        
 %0A       
@@ -2287,22 +2287,20 @@
  Logger 
-logger
+root
  = (Logg
@@ -2392,22 +2392,20 @@
 ntext = 
-logger
+root
 .getLogg
@@ -3337,24 +3337,67 @@
 r(encoder);%0A
+        rfAppender.setName(%22ROLLINGFILE%22);%0A
         rfAp
@@ -3537,37 +3537,498 @@
     
-logger.addAppender(rfAppender
+AsyncAppender asyncAppender = new AsyncAppender();%0A        asyncAppender.setName(%22ASYNC%22);%0A        asyncAppender.setQueueSize(ASYNC_QUEUE_SIZE);%0A        asyncAppender.setMaxFlushTime(ASYNC_MAX_FLUSH_TIME);%0A        asyncAppender.addAppender(rfAppender);%0A        asyncAppender.start();%0A%0A        root.addAppender(asyncAppender);%0A    %7D%0A%0A    public static void stopLogging() %7B%0A        Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);%0A        root.getLoggerContext().stop(
 );%0A 

Add AsyncAppender to LoggingInitializer for better performances under load