package <%=packageName%>.config;

import ch.qos.logback.classic.AsyncAppender;
import ch.qos.logback.classic.LoggerContext;
import net.logstash.logback.appender.LogstashSocketAppender;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

@Configuration
public class LoggingConfiguration {

    private final Logger log = LoggerFactory.getLogger(LoggingConfiguration.class);
    private LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();

    @Value("${spring.application.name}")
    private String appName;

    @Value("${server.port}")
    private String serverPort;

    @Value("${eureka.instance.instanceId}")
    private String instanceId;

    @Inject
    private JHipsterProperties jHipsterProperties;

    @PostConstruct
    private void init() {
        if (jHipsterProperties.getLogging().getLogstash().isEnabled()) {
            addLogstashAppender();
        }
    }

    public void addLogstashAppender() {
        log.info("Initializing Logstash logging");

        LogstashSocketAppender logstashAppender = new LogstashSocketAppender();
        logstashAppender.setName("LOGSTASH");
        logstashAppender.setContext(context);

        String customFields = "{\"app_name\":\"" + appName + "\",\"port\":\"" + serverPort + "\"," +
            "\"instance_id\":\"" + instanceId + "\"}";

        // Set the Logstash appender config from JHipster properties
        logstashAppender.setSyslogHost(jHipsterProperties.getLogging().getLogstash().getHost());
        logstashAppender.setPort(jHipsterProperties.getLogging().getLogstash().getPort());
        logstashAppender.setCustomFields(customFields);
        logstashAppender.start();

        // Wrap the appender in an Async appender for performance
        AsyncAppender asyncLogstashAppender = new AsyncAppender();
        asyncLogstashAppender.setContext(context);
        asyncLogstashAppender.setName("ASYNC_LOGSTASH");
        asyncLogstashAppender.setQueueSize(jHipsterProperties.getLogging().getLogstash().getQueueSize());
        asyncLogstashAppender.addAppender(logstashAppender);
        asyncLogstashAppender.start();

        context.getLogger("ROOT").addAppender(asyncLogstashAppender);
    }
}

@@ -779,16 +779,101 @@
 rPort;%0A%0A
+    %3C%25_ if (applicationType == 'microservice' %7C%7C applicationType == 'gateway') %7B _%25%3E%0A
     @Val
@@ -904,24 +904,24 @@
 stanceId%7D%22)%0A
-
     private 
@@ -939,17 +939,30 @@
 anceId;%0A
+    %3C%25_ %7D _%25%3E
 %0A
-
     @Inj
@@ -1448,24 +1448,113 @@
 (context);%0A%0A
+        %3C%25_ if (applicationType == 'microservice' %7C%7C applicationType == 'gateway') %7B _%25%3E%0A
         Stri
@@ -1608,16 +1608,20 @@
 + %22%5C%22,%5C%22
+app_
 port%5C%22:%5C
@@ -1700,16 +1700,161 @@
 + %22%5C%22%7D%22;
+%0A        %3C%25 %7D else %7B %25%3E%0A        String customFields = %22%7B%5C%22app_name%5C%22:%5C%22%22 + appName + %22%5C%22,%5C%22app_port%5C%22:%5C%22%22 + serverPort + %22%5C%22%7D%22;%0A        %3C%25_ %7D _%25%3E
 %0A%0A      

remove appending of eureka instanceId to the logs for monoliths