https://zhuanlan.zhihu.com/p/506119895
logback
上报日志到Skywalking
配置logback
上报日志到skywalking
需要引入toolkit
依赖项,如在maven
中添加:
<dependency>
<groupId>org.apache.skywalking</groupId>
<artifactId>apm-toolkit-logback-1.x</artifactId>
<version>{skywalking.version}</version>
</dependency>
在gradle
中添加:
dependencies {
implementation "org.apache.skywalking:apm-toolkit-logback-1.x:${skywalkingVersion}"
}
如果是通过容器加载的skywalking-agent
插件,由于其自带了这些jar
包,可以参考后面的kubernetes
配置。
配置logback
日志:
<appender name="skywalking-log" class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.log.GRPCLogClientAppender">
<encoder>
<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%n</Pattern>
</layout>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="skywalking-log"/>
<appender-ref ref="file" />
</root>
如果agent
与oap
不在同一服务器上时,需要配置agent/config/agent.config
配置文件,内容如下:
# 指定要向其报告日志数据的GRPC服务器主机
plugin.toolkit.log.grpc.reporter.server_host=${SW_GRPC_LOG_SERVER_HOST:10.10.10.1}
# 指定要向其报告日志数据的GRPC服务器端口
plugin.toolkit.log.grpc.reporter.server_port=${SW_GRPC_LOG_SERVER_PORT:11800}
# 指定GRPC客户端要报告的日志数据的最大大小
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}
如果是通过kubernetes
配置的,追加环境变量LD_LIBRARY_PATH
可实现动态加载(此处省略了配置SW_
相关的环境变量):
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
type: service
language: java
k8s-app: test
name: test
namespace: myapp
spec:
progressDeadlineSeconds: 600
replicas: 2
revisionHistoryLimit: 10
selector:
matchLabels:
k8s-app: test
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
type: RollingUpdate
template:
spec:
containers:
image: xxx.mycompany.com/test:1.0.0
- env:
- name: JAVA_TOOL_OPTIONS
value: -javaagent:/skywalking/agent/skywalking-agent.jar
- name: LD_LIBRARY_PATH
value: $LD_LIBRARY_PATH:/skywalking/agent/activations/
imagePullPolicy: Always
name: test
resources:
limits:
cpu: 2000m
memory: 2Gi
requests:
cpu: 250m
memory: 512Mi
livenessProbe:
failureThreshold: 3
initialDelaySeconds: 60
periodSeconds: 10
successThreshold: 1
tcpSocket:
port: 80