一、需求
在k8s环境已经部署了SkyWalking 的 oap 和 ui,本地的java应用接入SkyWalking时,想要将控制台的日志信息展示
到UI
界面
预期效果:
二、步骤
参考官方文档: https://skywalking.apache.org/docs/skywalking-java/v8.14.0/en/setup/service-agent/java-agent/application-toolkit-logback-1.x/
参考文章:
https://blog.csdn.net/hxy793518971/article/details/122046939
https://blog.csdn.net/kingtok/article/details/113886294
2.1 pom文件引入依赖
<dependency>
<groupId>org.apache.skywalking</groupId>
<artifactId>apm-toolkit-logback-1.x</artifactId>
<version>8.11.0</version>
</dependency>
<dependency>
<groupId>org.apache.skywalking</groupId>
<artifactId>apm-toolkit-trace</artifactId>
<version>8.1.0</version>
</dependency>
2.2 logback–spring.xml文件修改
完整的日志配置文件参考:https://github.com/apache/skywalking/blob/3a8e0c11b782eee37480deb1a3ba44b26a5fdc2c/test/e2e-v2/java-test-service/e2e-service-provider/src/main/resources/logback.xml
官网文档内容:
<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} [%X{tid}] [%thread] %-5level %logger{36} -%msg%n</Pattern>
</layout>
</encoder>
</appender>
<appender-ref ref="grpc-log"/>
我的完整logback–spring.xml,有注释增加配置日志收集
表示新加的内容:
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod=" 5 seconds">
<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] [appName=helloTest] [%thread] %-5level %logger{36} - %msg%n</pattern>
</layout>
</encoder>
</appender>
<!-- < ! -- 配置异步记录 AsyncAppender &ndash;&gt;-->
<appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender">
<discardingThreshold>0</discardingThreshold>
<queueSize>1024</queueSize>
<neverBlock>true</neverBlock>
<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} [%X{tid}] [%thread] %-5level %logger{36} -%msg%n</Pattern>
</layout>
</encoder>
</appender>
<!--<appender name="fileAppender" class="ch.qos.logback.core.FileAppender">-->
<!-- <file>/tmp/skywalking-logs/logback/e2e-service-provider.log</file>-->
<!-- <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">-->
<!-- <layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout">-->
<!-- <Pattern> [%level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %logger:%line - %msg%n</Pattern>-->
<!-- </layout>-->
<!-- </encoder>-->
<!--</appender>-->
<root level="DEBUG">
<!-- 增加配置日志收集-->
<appender-ref ref="grpc-log"/>
<appender-ref ref="ASYNC"/>
<appender-ref ref="stdout"/>
</root>
</configuration>
2.3 修改agent的配置文件
由于我的java应用是在本地启动,而oap部署在另外一台服务器(java应用
和oap不在同一台
服务器),所以需要修改agent相关信息。
注:
如果java应用和oap同属一台服务器,则agent配置文件不需要做修改.
skywalking-agent/config/agent.config
文件:
plugin.toolkit.log.grpc.reporter.server_host=${SW_GRPC_LOG_SERVER_HOST:192.168.1xx.130} # oap的部署地址
plugin.toolkit.log.grpc.reporter.server_port=${SW_GRPC_LOG_SERVER_PORT:30078} # oap的采集数据端口
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}
2.4 启动java应用
启动配置:
-javaagent:D:/project/helloWorld/agent/skywalking-agent/skywalking-agent.jar
-DSW_AGENT_NAME=hello33
-DSW_AGENT_COLLECTOR_BACKEND_SERVICES=192.168.1xx.130:30078 # oap的采集地址端口
2.5 日志查看
检查启动agent的日志: skywalking-agent/logs/skywalking-api.log
三、验证
java应用启动之后, 请求服务的某个路径,然后查看SkyWalking UI:
四、常见问题
4.1 修改完logback配置文件,项目启动报错
4.1.1 错误
报错信息:
java.lang.IllegalStateException: Logback configuration error detected:
ERROR in ch.qos.logback.core.joran.spi.Interpreter@24:76 - no applicable action for [encoder], current ElementPath is [[configuration][appender][encoder]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@25:115 - no applicable action for [layout], current ElementPath is [[configuration][appender][encoder][layout]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@26:26 - no applicable action for [Pattern], current ElementPath is [[configuration][appender][encoder][layout][Pattern]]
at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:167)
at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithConventions(AbstractLoggingSystem.java:80)
at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:60)
at org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:118)
at org.springframework.boot.context.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:289)
at org.springframework.boot.context.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:264)
at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:226)
at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:203)
4.1.2 解决
4.2 UI的log页面没有内容
java应用配置修改完成并成功启动,然后请求应用的某个路径,但是在UI的log上数据为空
Failed to read the config file, skywalking is going to run in default config
最后是将java agent包更换
了一个更高级
的版本, 再次启动显示日志输出正常
原文地址:https://blog.csdn.net/qq_44209563/article/details/129671610
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_14545.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!