SkyWalking8.5集成spring-logback.xml日志收集

SkyWalking8.5集成spring-logback.xml日志收集

skywalking搭建请参考:https://www.cnblogs.com/yizhipanghu/p/14738492.html

pom引入以下依赖,对应各自skywalking版本(本文使用8.5.0版本apache-skywalking-apm-es7-8.5.0.tar.gz)

       <dependency>
            <groupId>org.apache.skywalking</groupId>
            <artifactId>apm-toolkit-logback-1.x</artifactId>
            <version>8.5.0</version>
        </dependency>

skywalking下载地址:https://skywalking.apache.org/downloads/

Spirngboot+Logback集成Skywalking日志系统

logback-spring.xml配置中加入appender name为 msystem-log的配置

    <appender name="msystem-log" class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.log.GRPCLogClientAppender">
        <!-- 日志输出编码 -->
        <encoder>
            <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
            </pattern>
        </encoder>
    </appender>
    <root level="INFO">
        <appender-ref ref="msystem-log"/>
    </root>

整体包含logstash和本地日志保存的配置如下:

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

<configuration>
    <!-- ELK日志, 发送至 appender -->
    <appender name="log-logstash" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
        <!-- logstash地址和端口 -->
        <destination>10.10.10.1:9061</destination>
        <!-- encoder必须配置,有多种可选 -->
        <encoder class="net.logstash.logback.encoder.LogstashEncoder">
            <!-- skywalking插件, log加tid -->
            <provider class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.logstash.TraceIdJsonProvider" />
            <customFields>{"project":"iservice"}</customFields>
        </encoder>
    </appender>

    <!--定义日志文件的存储地址 勿在 LogBack 的配置中使用相对路径 -->
    <property name="logPath" value="/home" />
    <property name="appName" value="iservice" />

    <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${logPath}/${appName}.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${logPath}/bak/%d{yyyy-MM-dd}/${appName}-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
            <cleanHistoryOnStart>true</cleanHistoryOnStart>
            <maxHistory>15</maxHistory>
            <maxFileSize>20MB</maxFileSize>
            <!--<totalSizeCap>50GB</totalSizeCap>-->
        </rollingPolicy>
        <encoder>
            <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg %n</Pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <appender name="msystem-log" class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.log.GRPCLogClientAppender">
        <!-- 日志输出编码 -->
        <encoder>
            <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
            </pattern>
        </encoder>
    </appender>

    <!-- root级别   INFO -->
    <root level="INFO">
        <!-- logstash输出 -->
        <appender-ref ref="log-logstash"/>
        <appender-ref ref="file" />
        <appender-ref ref="msystem-log"/>
    </root>
</configuration>

agent和oap在不同服务器上时,需配置agent/config/agent.config配置文件,在文件末尾添加如下配置信息,注意skywalking的log通信用的grpc

plugin.toolkit.log.grpc.reporter.server_host=${SW_GRPC_LOG_SERVER_HOST:10.10.10.1}
plugin.toolkit.log.grpc.reporter.server_port=${SW_GRPC_LOG_SERVER_PORT:11800}
plugin.toolkit.log.grpc.reporter.max_message_size=${SW_GRPC_LOG_MAX_MESSAGE_SIZE:10485760}
plugin.toolkit.log.grpc.reporter.upstream_timeout=${SW_GRPC_LOG_GRPC_UPSTREAM_TIMEOUT:30}

各参数说明

服务运行后即可看到日志:

 

 

 官方配置指导:

https://skywalking.apache.org/docs/main/v8.5.0/en/setup/service-agent/java-agent/readme/#table-of-agent-configuration-properties

 

posted @ 2021-06-17 19:17  香菜哥哥  阅读(5940)  评论(0编辑  收藏  举报