package alien4cloud.deployment;

import javax.annotation.Resource;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import alien4cloud.dao.IGenericSearchDAO;
import alien4cloud.paas.model.PaaSDeploymentLog;
import groovy.util.logging.Log4j;

/**
 * Service is used by some premium plugins for archive the deployments logs.
 */
@Log4j
@Service
public class DeploymentLoggingService {
    @Resource(name = "alien-monitor-es-dao")
    private IGenericSearchDAO alienMonitorDao;

    @Value("${logs_deployment_appender.enable}")
    private boolean isEnabled;

    private final Logger deployments_logger = LogManager.getLogger("DEPLOYMENT_LOGS_LOGGER");

    public synchronized void save(final PaaSDeploymentLog deploymentLog) {
        try {
            if (isEnabled) {
                deployments_logger.info(deploymentLog.toCompactString());
            }
        } finally {
            alienMonitorDao.save(deploymentLog);
        }
    }

    public synchronized void save(final PaaSDeploymentLog[] deploymentLogs) {
        try {
            if (isEnabled) {
                for (PaaSDeploymentLog log : deploymentLogs) {
                    deployments_logger.info(log.toCompactString());
                }
            }
        } finally {
            alienMonitorDao.save(deploymentLogs);
        }
    }

}
@@ -797,43 +797,30 @@
    p
-ublic synchronized void save(final 
+rivate void logToFile(
 PaaS
@@ -862,114 +862,702 @@
     
-try %7B%0A            if (isEnabled) %7B%0A                deployments_logger.info(deploymentLog.toCompactString()
+switch (deploymentLog.getLevel()) %7B%0A            case DEBUG:%0A                deployments_logger.debug(deploymentLog.toCompactString());%0A                break;%0A            case ERROR:%0A                deployments_logger.error(deploymentLog.toCompactString());%0A                break;%0A            case INFO:%0A                deployments_logger.info(deploymentLog.toCompactString());%0A                break;%0A            case WARN:%0A                deployments_logger.warn(deploymentLog.toCompactString());%0A                break;%0A        %7D%0A    %7D%0A%0A    public synchronized void save(final PaaSDeploymentLog deploymentLog) %7B%0A        try %7B%0A            if (isEnabled) %7B%0A                logToFile(deploymentLog
 );%0A 
@@ -1815,17 +1815,27 @@
 mentLog 
-l
+deploymentL
 og : dep
@@ -1873,53 +1873,31 @@
     
-deployments_logger.info(log.toCompactString()
+logToFile(deploymentLog
 );%0A 

use the log level to when we call the logger