Skywalking日志上报


 

配置方式

1、官方默认支持三种日志框架 1.1、log4j 依赖引入
<dependency>
    <groupId>org.apache.skywalkinggroupId>
    <artifactId>apm-toolkit-log4j-1.xartifactId>
    <version>8.4.0version>
dependency>
配置 log4j.properties log4j.appender.CONSOLE.layout=TraceIdPatternLayout # 设置日志输出格式
log4j.appender.CONSOLE.layout.ConversionPattern=%d [%T] %-5p %c{1}:%L - %m%n

1.2、log4j2

依赖引入
<dependency>
    <groupId>org.apache.skywalkinggroupId>
    <artifactId>apm-toolkit-log4j-2.xartifactId>
    <version>8.4.0version>
dependency>
配置 log4j2.xml
<Configuration status="debug">
    <Appenders>
          
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d [%traceId] %-5p %c{1}:%L - %m%n"/>
        Console>
          
          <GRPCLogClientAppender name="grpc-log">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        GRPCLogClientAppender>
    Appenders>
    <Loggers>
        <logger name="com.a.eye.skywalking.ui" level="debug" additivity="false">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="grpc-log"/>
        logger>
        <logger name="org.apache.kafka" level="INFO">logger>
        <logger name="org.apache.skywalking.apm.dependencies" level="INFO">logger>
        <Root level="debug">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="grpc-log"/>
        Root>
    Loggers>
Configuration>

 1.3、Logback

依赖引入
<dependency>
    <groupId>org.apache.skywalkinggroupId>
    <artifactId>apm-toolkit-logback-1.xartifactId>
    <version>8.4.0version>
dependency>
gradle:compile 'org.apache.skywalking:apm-toolkit-logback-1.x:8.4.0' 配置 logback.xml
<configuration>  
      
    <appender name="STDOUT" 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>%d{yyyy-MM-dd HH:mm:ss.SSS} [%tid] [%thread] %-5level %logger{36} -%msg%nPattern>
            layout>
        encoder>
    appender>
       -- 配置异步记录 AsyncAppender -->
    <appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender">
        <discardingThreshold>0discardingThreshold>
        <queueSize>1024queueSize>
        <neverBlock>trueneverBlock>
        <appender-ref ref="STDOUT"/>
    appender>
    
    <appender name="grpc-log" class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.log.GRPCLogClientAppender">
        <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
            <layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.mdc.TraceIdMDCPatternLogbackLayout">
                <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%tid] [%thread] %-5level %logger{36} -%msg%nPattern>
            layout>
        encoder>
    appender>

    
    <root level="DEBUG">  
          <appender-ref ref="STDOUT"/> 
        <appender-ref ref="ASYNC"/> 
          <appender-ref ref="grpc-log"/>
    root>
configuration>

2、添加日志插件配置或使用默认值

打开agent/config/agent.config配置文件,添加如下配置信息:
plugin.toolkit.log.grpc.reporter.server_host=${SW_GRPC_LOG_SERVER_HOST:127.0.0.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}
以上配置是默认配置信息,agent语oap在本地的可以不用配置,不在一起部署必须要配置。 配置说明: 使用工程 Live-Demo 工程 projectB 项目 使用上面的 log4j2.xml 文件替换原本内容 (原本配置的OFF) 效果展示 可以看到,projectB 项目中的 debug 日志都被展示出来了。但这里发现一个问题,v8.4.0 版本的 Skywalking 日志时间戳存在格式问题,如上图所示 github上也有相关issue,新版本已被解决:https://github.com/apache/skywalking/pull/6335#issue-568815160 主要是将时间戳从 String 类型变成了 Long 类型,可以通过修改以下几个文件解决 org.apache.skywalking.oap.server.core.query.type.Log
public class Log {
    private String serviceName;
    private String serviceId;
    private String serviceInstanceName;
    private String serviceInstanceId;
    private String endpointId;
    private String endpointName;
    private String traceId;
    private Long timestamp;
    private ContentType contentType = ContentType.NONE;
    private String content;
    private final List tags;

    public Log() {
        tags = new ArrayList<>();
    }
}
oap-server/server-query-plugin/query-graphql-plugin/src/main/resources/query-protocol/log.graphqls
type Log {
    serviceName: String
    serviceId: ID
    serviceInstanceName: String
    serviceInstanceId: ID
    endpointName: String
    endpointId: ID
    traceId: String
    timestamp: Long! # 修改时间戳类型
    contentType: ContentType!
    content: String
    tags: [KeyValue!]
}
org.apache.skywalking.oap.server.storage.plugin.influxdb.query.LogQuery#queryLogs
log.setTimestamp(((Number) data.get(TIMESTAMP)).longValue());
org.apache.skywalking.oap.server.storage.plugin.elasticsearch.query.LogQueryEsDAO #queryLogs
log.setTimestamp(((Number) searchHit.getSourceAsMap().get(AbstractLogRecord.TIMESTAMP)).longValue());
org.apache.skywalking.oap.server.storage.plugin.elasticsearch.query .LogQueryEs7DAO#queryLogs
log.setTimestamp(((Number) searchHit.getSourceAsMap().get(AbstractLogRecord.TIMESTAMP)).longValue());
修改后的效果如下图所示 zhuan:https://blog.csdn.net/wb4927598/article/details/119192594