- 阿里云日志服务
- 配置application.yml
- 配置log4j2.xml
- 配置springboot启动程序
文档:https://help.aliyun.com/product/28958.html
材料准备:project、logstore、accessKeyId、accessKeySecret、endpoint
配置application.yml将准备好的accessKeyId、accessKeySecret配置到application.yml
代码示例:
配置springboot启动程序
1.创建事件监听类
public class LoggingListener implements ApplicationListener, Ordered {
@Override
public int getOrder() {
return LoggingApplicationListener.DEFAULT_ORDER - 1;
}
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ApplicationEnvironmentPreparedEvent) {
ConfigurableEnvironment environment = ((ApplicationEnvironmentPreparedEvent) event)
.getEnvironment();
String accessKeyId = Optional.ofNullable(environment.getProperty("aliyun.log.accessKeyId"))
.orElse("");
String accessKeySecret = Optional.ofNullable(environment.getProperty("aliyun.log.accessKeySecret"))
.orElse("");
System.setProperty("aliyun.log.accessKeyId", accessKeyId);
System.setProperty("aliyun.log.accessKeySecret", accessKeySecret);
}
}
}
- 添加到SpringApplication
public static void main(String[] args) {
SpringApplication application = new SpringApplication(MyApplication.class);
application.addListeners(new LoggingListener());
application.run(args);
}



