需求:实现logback日志写入数据库,并且logback关于数据库链接使用yml已有的数据源信息在logback.xml改造如下<!– 将日志存储到oracle数据库中 –> <appender name=“db-classic-oracle” class=“ch.qos.logback.classic.db.DBAppender”> <connectionSource class=“ch.qos.logback.core.db.DriverManagerConnectionSource”> </connectionSource> </appender> <!– 日志输出级别 –> <root level=“ERROR”> <appender-ref ref=“console” /> <appender-ref ref=“db-classic-oracle” /> </root> 正常上述appender部分需要设置数据源参数,类似<url>jdbc:oracle:thin:@XX:1521:orcl</url> <user>d</user> <password>111111</password> 但这部分内容实际上应用的主yml已经存在,所以想办法从yml已有的值去替换。logback本身应该能获取yml 参数。类似 <springProperty scope=“context” name=“dataUrl” source=“spring.datasource.username” defaultValue=“localhost”/>但实验了很多次,未成功,不知道为何。所以采取修改DriverManagerConnectionSource源码的方式去解决。查看源码发现下图设计的源码存在创建conn 的情况,所以已后面的代码形式去读取yml,数据库连接的相关参数即可。两种代码都能解决。//读取yml的方式1 YamlPropertiesFactoryBean yamlMapFactoryBean = new YamlPropertiesFactoryBean(); yamlMapFactoryBean.setResources(new ClassPathResource(“application.yml”)); Properties properties = yamlMapFactoryBean.getObject(); String username1=properties.getProperty(“spring.datasource.username”); //读取yml的方式2 ClassPathResource resource = new ClassPathResource(“application.yml”); InputStream inputStream = resource.getInputStream(); Map map = null; Yaml yaml = new Yaml(); map = (Map) yaml.load(inputStream);