Logback Spring不将日志发送到Logstash

时间:2020-04-15 21:23:09

标签: java spring-boot elasticsearch logstash logback

我有一个spring boot应用程序设置,用于将日志发送到logstash,后者将日志转发到elasticsearch。 但是,我的日志似乎并没有发货(我正在使用Elasticsearch的api检查索引,但都没有出现。

logback-sping.xml

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/>
    <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
    <conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
    <conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
    <property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
    <property name="FILE_LOG_PATTERN" value="${FILE_LOG_PATTERN:-%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>

    <logger name="org.apache.catalina.startup.DigesterFactory" level="ERROR"/>
    <logger name="org.apache.catalina.util.LifecycleBase" level="ERROR"/>
    <logger name="org.apache.coyote.http11.Http11NioProtocol" level="WARN"/>
    <logger name="org.apache.sshd.common.util.SecurityUtils" level="WARN"/>
    <logger name="org.apache.tomcat.util.net.NioSelectorPool" level="WARN"/>
    <logger name="org.eclipse.jetty.util.component.AbstractLifeCycle" level="ERROR"/>
    <logger name="org.hibernate.validator.internal.util.Version" level="WARN"/>
    <logger name="org.springframework.data.elasticsearch.client.WIRE" level="trace"/>

    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>INFO</level>
        </filter>
        <encoder>
            <pattern>${CONSOLE_LOG_PATTERN}</pattern>
        </encoder>
    </appender>


    <appender name="FILE"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>TRACE</level>
        </filter>
        <encoder>
            <pattern>${FILE_LOG_PATTERN}</pattern>
        </encoder>
        <file>${LOG_FILE}</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <cleanHistoryOnStart>${LOG_FILE_CLEAN_HISTORY_ON_START:-false}</cleanHistoryOnStart>
            <fileNamePattern>${ROLLING_FILE_NAME_PATTERN:-${LOG_FILE}.%d{yyyy-MM-dd}.%i.gz}</fileNamePattern>
            <maxFileSize>${LOG_FILE_MAX_SIZE:-10MB}</maxFileSize>
            <maxHistory>${LOG_FILE_MAX_HISTORY:-7}</maxHistory>
            <totalSizeCap>${LOG_FILE_TOTAL_SIZE_CAP:-0}</totalSizeCap>
        </rollingPolicy>
    </appender>
    <appender name="loggingLogstash" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>TRACE</level>
        </filter>
        <destination>logstash-data.myapp.com</destination>
        <!-- encoder is required -->
        <encoder class="net.logstash.logback.encoder.LogstashEncoder">
            <customFields>{"service_name":"SpringBootLogger"}</customFields>
            <fieldNames>
                <message>log_message</message>
            </fieldNames>
        </encoder>
        <keepAliveDuration>5 minutes</keepAliveDuration>

    </appender>


        <appender name="accessLogstash" class="net.logstash.logback.appender.LogstashAccessTcpSocketAppender">
            <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
                <level>TRACE</level>
            </filter>
            <destination>logstash.myapp.com</destination>
            <!-- encoder is required -->
            <encoder class="net.logstash.logback.encoder.LogstashAccessEncoder" >
                <customFields>{"service_name":"SpringBootLogger"}</customFields>
                <fieldNames>
                    <message>log_message</message>
                </fieldNames>
            </encoder>
            <keepAliveDuration>5 minutes</keepAliveDuration>
        </appender>



    <root level="TRACE">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="FILE" />
        <appender-ref ref="loggingLogstash" />
        <appender-ref ref="accessLogstash" />
    </root>
</configuration>

我有nginx设置将这些网址转发到我的logstash docker容器:

server {
    listen 80;
    server_name logstash-data.myapp.com;
    keepalive_timeout 70;
    allow all;

    location / {
      proxy_pass     http://logstash:5001;
    }
  }

server {
    listen 80;
    server_name logstash.myapp.com;
    keepalive_timeout 70;
    allow all;

    location / {
      proxy_pass     http://logstash:5000;
    }
  }

logstash具有以下配置设置:

 input {
    tcp {
      port => 5000
      codec => "json_lines"
    }
}
output {
  elasticsearch {
    hosts => "elasticsearch:9200"
    index => "myappjirawebhooklogs"
  }
}

 input {
    tcp {
      port => 5001
      codec => "json_lines"
    }
}
output {
  elasticsearch {
    hosts => "elasticsearch:9200"
    index => "myappjirawebhooklogsdata"
  }
}

我将logback日志级别切换为DEBUG,但仍然没有看到特别有意义的日志。此设置有什么问题?

我正在使用lombok的@ Slf4j日志记录我的课程内

0 个答案:

没有答案