栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot配置阿里云日志服务与log4j2 lookup

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot配置阿里云日志服务与log4j2 lookup

阿里云日志服务与log4j lookup配置springboot
  • 阿里云日志服务
  • 配置application.yml
  • 配置log4j2.xml
  • 配置springboot启动程序

阿里云日志服务

文档:https://help.aliyun.com/product/28958.html

材料准备:project、logstore、accessKeyId、accessKeySecret、endpoint

配置application.yml

将准备好的accessKeyId、accessKeySecret配置到application.yml

配置log4j2.xml


代码示例:


	
	      
	    
	
	    
	      
	    
	


配置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);
        }
    }
}
  1. 添加到SpringApplication
    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(MyApplication.class);
        application.addListeners(new LoggingListener());
        application.run(args);
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/425498.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号