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;

    <%_ if (applicationType == 'microservice' || applicationType == 'gateway') { _%>
    @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);
        <%_ if (applicationType == 'microservice' || applicationType == 'gateway') { _%>
        String customFields = "{\"app_name\":\"" + appName + "\",\"app_port\":\"" + serverPort + "\"," +
            "\"instance_id\":\"" + instanceId + "\"}";
        <% } else { %>
        String customFields = "{\"app_name\":\"" + appName + "\",\"app_port\":\"" + serverPort + "\"}";
        <%_ } _%>

        // 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);
    }
}

@@ -174,24 +174,92 @@
 etAppender;%0A
+import net.logstash.logback.stacktrace.ShortenedThrowableConverter;%0A
 import org.s
@@ -2228,16 +2228,343 @@
 ields);%0A
+%0A        // Limit the maximum length of the forwarded stacktrace so that it won't exceed the 8KB UDP limit of logstash%0A        ShortenedThrowableConverter throwableConverter = new ShortenedThrowableConverter();%0A        throwableConverter.setMaxLength(7500);%0A        logstashAppender.setThrowableConverter(throwableConverter);%0A%0A
         

add a length limit for logstash-encoder forwarded stacktraces fix #3364