Skip to content

日志组件使用

本文档介绍后端工程引入数字底座日志组件 SDK,进行应用的开发。

实现原理

Y9 平台的操作日志利用 AOP 的方式实现,对这些注解 @RiseLog@Path@RequestMapping 进行拦截,执行日志记录的行为,将日志信息上报日志平台,然后日志管理平台监听日志信息,接收到日志数据并保存这些数据

具体的代码的实现可查看 y9-digitalbase-starter/risenet-y9boot-starter-log/src/main/java/net/risesoft/log/aop/RiseLogAdvice.java

目前有两种上报日志信息的方式,选择其中一种方式即可:

  • 利用消息中间件 Kafka
  • 调用日志平台提供的 HTTP API

maven pom.xml 修改

添加日志组件依赖包

xml
    <dependency>
        <groupId>net.risesoft</groupId>
        <artifactId>risenet-y9boot-starter-log</artifactId>
        <version>[最新版本]</version>
    </dependency>

如果需要使用 snapshot 版本,还需添加私服仓库方能下载

xml
    <repositories>
        <repository>
            <id>nexus</id>
            <name>local private nexus</name>
            <url>https://svn.youshengyun.com:9900/nexus/repository/maven-public/</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>nexus</id>
            <name>local private nexus</name>
            <url>https://svn.youshengyun.com:9900/nexus/repository/maven-public/</url>
        </pluginRepository>
    </pluginRepositories>

修改属性文件 application.yml

根据选择的上报方式修改配置文件上报日志信息:

yaml
spring:
    kafka:
        bootstrap-servers: localhost:9092
        consumer:
            group-id: y9demo-kernel-api-server
y9:
    feature:
        log:
            enabled: true
            logSaveTarget: kafka
yaml
y9:
    common:
        logBaseUrl: http://localhost:7055/log
    feature:
        log:
            enabled: true
            logSaveTarget: api

在方法上添加注解 @RiseLog

  • operationName 方法名称
  • logLevel 日志级别
  • moduleName 业务系统名称
java
package net.risesoft.log;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import net.risesoft.log.annotation.RiseLog;

@RestController
public class LogDemoController {

    @GetMapping
    @RiseLog(operationName = "测试记录日志", moduleName = "日志记录演示系统")
    public String log() {
        return "log success";
    }
}

示例代码

码云地址:https://gitee.com/risesoft-y9/y9-core

仓库代码中示例代码路径为 y9-digitalbase-example/risenet-y9demo-log

Released under the GPL-3.0 License.