SpringBoot项目接入阿里云SLS日志 SL4J + LogBack
以下是 Springboot + Sl4j + logback 日志打印。创建完成 Project 后,进入Project内。创建一个用户角色 AccessKey。以上是 SLS 阿里云开通基本配置。logback.xml 配置如下。创建一个 logstore。● endpoint 设置。阿里云SLS 控制台效果。
·
创建完成 Project 后,进入Project内。
创建一个 logstore
- 开启索引
创建一个 用户角色 AccessKey
赋予角色权限。
以上是 SLS 阿里云开通基本配置。
以下是 Springboot + Sl4j + logback 日志打印
● 依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--阿里云SLS需要-->
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</dependency>
<!--阿里云SLS需要-->
<dependency>
<groupId>com.aliyun.openservices</groupId>
<artifactId>aliyun-log-logback-appender</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
</dependencies>
logback.xml 配置如下
<Configuration>
<!--为了防止进程退出时,内存中的数据丢失,请加上此选项-->
<shutdownHook class="ch.qos.logback.core.hook.DelayingShutdownHook"/>
<!--输出到控制台-->
<!-- 控制台输出 -->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level [%thread] %logger{0}: %msg%n</pattern>
<charset>utf-8</charset>
</encoder>
</appender>
<appender name="aliyun" class="com.aliyun.openservices.log.logback.LoghubAppender">
<!--必选项-->
<!-- 账号及网络配置 -->
<!-- 这里是节点配置 在 -->
<endpoint>cn-shanghai.log.aliyuncs.com</endpoint>
<accessKeyId>输入刚刚创建的 ID</accessKeyId>
<accessKeySecret>输入刚刚创建的 Secret</accessKeySecret>
<!-- sls 项目配置 -->
<project>jushan-log</project>
<logStore>jushan-log</logStore>
<!--必选项 (end)-->
<!-- 可选项 -->
<topic>your topic</topic>
<source>your source</source>
<!-- 可选项 详见 '参数说明'-->
<totalSizeInBytes>104857600</totalSizeInBytes>
<maxBlockMs>0</maxBlockMs>
<ioThreadCount>8</ioThreadCount>
<batchSizeThresholdInBytes>524288</batchSizeThresholdInBytes>
<batchCountThreshold>4096</batchCountThreshold>
<lingerMs>2000</lingerMs>
<retries>10</retries>
<baseRetryBackoffMs>100</baseRetryBackoffMs>
<maxRetryBackoffMs>50000</maxRetryBackoffMs>
<!-- 可选项 通过配置 encoder 的 pattern 自定义 log 的格式 -->
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level [%thread] %logger{0}: %msg%n</pattern>
<charset>utf-8</charset>
</encoder>
<!-- 可选项 设置 time 字段呈现的格式 -->
<timeFormat>yyyy-MM-dd'T'HH:mmZ</timeFormat>
<!-- 可选项 设置 time 字段呈现的时区 -->
<timeZone>UTC</timeZone>
<!-- 可选项 设置是否要添加 Location 字段(日志打印位置),默认为 true -->
<includeLocation>true</includeLocation>
<!-- 可选项 当 encoder 不为空时,是否要包含 message 字段,默认为 true -->
<includeMessage>true</includeMessage>
<!-- 可选项 可注入自定义的 credentialsProvider,允许用户自行实现 AK 获取逻辑 -->
<!-- 参见 "自定义凭证提供者 CredentialsProvider" 一节介绍 -->
<credentialsProviderBuilder
class="com.aliyun.openservices.log.logback.example.ExampleCredentialsProviderBuilder">
<accessKeyId>输入刚刚创建的 ID</accessKeyId>
<accessKeySecret>输入刚刚创建的 Secret</accessKeySecret>
</credentialsProviderBuilder>
<!-- 可选项,exception 堆栈最大记录长度,超出此长度会被截断,默认值为 500 -->
<maxThrowable>500</maxThrowable>
</appender>
<root level="INFO">
<appender-ref ref="console" />
<appender-ref ref="aliyun" />
</root>
<!-- 配置日志记录的行为(Logger) end -->
</Configuration>
● endpoint 设置
简单代码案例
- 这里是 Log 注解
/**
* @author Hello
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Log{
String value() default "未命名";
}
- 这里是切面
@Aspect
@Order
@Component
@Slf4j
public class LogAop
{
@Pointcut("@annotation(com.jushan.aop.Log)")
private void getLogPointCut() {
}
@AfterReturning(pointcut = "getLogPointCut()", returning = "result")
public void doAfterReturning(JoinPoint joinPoint, Object result) {
Object[] args = joinPoint.getArgs();
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
Method method = methodSignature.getMethod();
Log loger = method.getAnnotation(Log.class);
for (Object arg : args)
{
log.info("AfterReturning 參數操作日志:{} ---{}",loger.value(), JSONUtil.toJsonStr(arg));
}
log.info("AfterReturning 返回操作日志:{} -----{}",loger.value(), JSONUtil.toJsonStr(result));
}
@AfterThrowing(pointcut = "getLogPointCut()", throwing = "exception")
public void doAfterThrowing(JoinPoint joinPoint, Exception exception) {
Object[] args = joinPoint.getArgs();
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
Method method = methodSignature.getMethod();
Log loger = method.getAnnotation(Log.class);
for (Object arg : args)
{
log.info("操作日志:{} ---{}",loger.value(), JSONUtil.toJsonStr(arg));
}
//异步记录日志
log.error("操作日志:{} ----{}",loger.value(), JSONUtil.toJsonStr(exception));;
}
}
- 控制器
/**
* @Author:
* @CreateTime: 2025-01-09
* @Description:
* @Version: 1.0
*/
@Slf4j
@RestController
public class SlsDemo
{
@RequestMapping("/log")
@Log(value = "记录不同级别的日志消息")
public String logExampleDemo(@RequestBody JSONObject jsonObject) {
// 记录不同级别的日志消息
log.info("方法内參數 {}",jsonObject.toJSONString());
return "ok";
}
}
控制台效果如下
阿里云SLS 控制台效果
- 以上是简单的集成
更多推荐
所有评论(0)