首页 > 解决方案 > Configure Log4J 2 programatically using a dynamically generated YAML file

问题描述

I need to dynamically configure Log4j 2 with a YAML configuration loaded from a web service.

Is there any way to load this configuration?

My best approach has been to download the YAML configuration and write it to a temp file, then make Log4j 2 load it. It is working, but it doesn't feel good.

File f = File.createTempFile("config",".yaml");
Files.write(f.toPath(),yamlString.getBytes());
Configurator.initialize(null, f.getAbsolutePath());
f.delete();
Logger LOGGER = LoggerFactory.getLogger(MyClass.class);
LOGGER.info("This is a INFO message");

Shortened, I would like to be able to:

ConfigurationSource source = new ConfigurationSource(new ByteArrayInputStream(yamlString.getBytes()));
Configurator.initialize(new YamlConfiguration(null,source));
Logger LOGGER = LoggerFactory.getLogger(MyClass.class);
LOGGER.info("This is a INFO message");

标签: javayamllog4j2

解决方案


推荐阅读