apache / skywalking

APM, Application Performance Monitoring System
https://skywalking.apache.org/
Apache License 2.0
23.85k stars 6.52k forks source link

logstash logback plugin get tid is "TID:N/A" #5746

Closed HuangDayu closed 4 years ago

HuangDayu commented 4 years ago

zh_CN:

你好,我的配置如下,控制台的tid是有值,但是logstash 日志的tid值是获取不到的,不知是什么原因,请帮忙看一下,谢谢。

en_US:

Hello, my configuration is as follows. The tid of the console has a value, but the pid value of the logstash log is not available. I don't know the reason. Please help to take a look. Thank you.

logging:
  logstash: '192.168.2.246:4560'
  config: classpath:logback-config.xml
  level:
    com.cloud.*: debug
    org.apache.kafka.clients.*: error
    org.springframework.scheduling.support.*: off
    org.springfromework.*: info
  pattern:
    console: '%clr(%d{${LOG_DATEFORMAT_PATTERN:yyyy-MM-dd HH:mm:ss.SSS}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(%tid){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:%wEx}'
  file:
    path: /home/cloud/logs/${spring.application.name}/${spring.application.instance-id}
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="10 seconds">

    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>

    <springProperty name="spring.application.name" scope="context" source="spring.application.name"/>
    <springProperty name="spring.application.instance-id" scope="context" source="spring.application.instance-id"/>
    <springProperty name="logging.logstash" scope="context" source="logging.logstash"/>
    <springProperty name="logging.pattern.console" scope="context" source="logging.pattern.console"/>
    <springProperty name="logging.file.path" scope="context" source="logging.file.path"/>
    <springProperty name="logging.level" scope="context" source="logging.level"/>

    <property name="LOG_FILE_PATH"
              value="/home/cloud/logs/${spring.application.name}/${spring.application.instance-id}"/>
    <property name="CONSOLE_LOG_PATTERN"
              value="%clr(%d{${LOG_DATEFORMAT_PATTERN:yyyy-MM-dd HH:mm:ss.SSS}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(%tid){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx"/>
    <property name="FILE_LOG_PATTERN"
              value="%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } %tid --- [%t] %-40.40logger{39} : %m%n%wEx"/>

    <!--add converter for %tid -->
    <conversionRule conversionWord="tid"
                    converterClass="org.apache.skywalking.apm.toolkit.log.logback.v1.x.LogbackPatternConverter"/>

    <appender class="ch.qos.logback.core.ConsoleAppender">
        <encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder">
            <provider class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.logstash.TraceIdJsonProvider"/>
        </encoder>
    </appender>

    <appender name="logstash" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
        <destination>${logging.logstash}</destination>
        <encoder charset="UTF-8" class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
            <providers>
                <timestamp>
                    <timeZone>UTC</timeZone>
                </timestamp>
                <pattern>
                    <pattern>
                        {
                        "level": "%level",
                        "serviceName": "${spring.application.name:-}",
                        "serviceId": "${spring.application.instance-id:-}",
                        "pid": "${PID:-}",
                        "tid": "%tid",
                        "thread": "%thread",
                        "class": "%logger{1.}:%L",
                        "message": "%message",
                        "stackTrace": "%exception{10}"
                        }
                    </pattern>
                </pattern>
            </providers>
        </encoder>
    </appender>

    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
            <layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout">
                <Pattern>${CONSOLE_LOG_PATTERN}</Pattern>
            </layout>
        </encoder>
    </appender>

    <appender name="output" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${LOG_FILE_PATH}/spring.log</File>

        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <FileNamePattern>${LOG_FILE_PATH}/%d{yyyy-MM-dd}.%i.log.gz</FileNamePattern>
            <MaxHistory>30</MaxHistory>
            <maxFileSize>10MB</maxFileSize>
        </rollingPolicy>

        <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
            <layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout">
                <Pattern>${CONSOLE_LOG_PATTERN}</Pattern>
            </layout>
        </encoder>

    </appender>

    <root level="INFO">
        <appender-ref ref="logstash"/>

        <appender-ref ref="console"/>
    </root>

    <springProfile name="test | dev | prod">
        <logger name="com.cloud.iot.*" level="debug"/>
        <logger name="org.apache.kafka.clients.*" level="error"/>
        <logger name="org.springframework.scheduling.support.*" level="off" addtivity="false">
            <appender-ref ref="console"/>
        </logger>
        <logger name="org.springfromework.*" level="info"/>
    </springProfile>

</configuration>

image

image

image

kezhenxu94 commented 4 years ago

Duplicate of #3713

kezhenxu94 commented 4 years ago

Are you using version <8.1.0?

HuangDayu commented 4 years ago

解决了

    <appender name="logstash" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
        <destination>${logging.logstash}</destination>
        <encoder charset="UTF-8" class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
            <providers>
                <provider class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.logstash.TraceIdJsonProvider"/>
                <timestamp>
                    <timeZone>UTC</timeZone>
                </timestamp>
                <pattern>
                    <pattern>
                        {
                        "level": "%level",
                        "serviceName": "${spring.application.name:-}",
                        "serviceId": "${spring.application.instance-id:-}",
                        "pid": "${PID:-}",
                        "tid": "%tid",
                        "thread": "%thread",
                        "class": "%logger{1.}:%L",
                        "message": "%message",
                        "stackTrace": "%exception{10}"
                        }
                    </pattern>
                </pattern>
            </providers>
        </encoder>
    </appender>

    <appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender">
        <discardingThreshold>0</discardingThreshold>
        <queueSize>256</queueSize>
        <appender-ref ref="logstash"/>
    </appender>
wuxingye commented 4 years ago

my logback-spring.xml file

<appender name="STASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
    <destination>127.0.0.1:4560</destination>
    <reconnectionDelay>1 second</reconnectionDelay>
    <encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder">
        <provider class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.logstash.TraceIdJsonProvider">
        </provider>
        <customFields>{"app":"server"}</customFields>
    </encoder>
</appender>