我们使用JNDI属性(在Tomcat We服务器中设置)来确定阶段(DEV/TEST/QA/PRD),以便配置一些应用程序细节。
现在,我们想用一个外部工具替换自制的日志记录,并想尝试一下tinylog。但是我们想知道是否可以从JNDI上下文中读取环境变量来配置tinylog设置?
文档中对JNDI查找只字不提。也许基于Java的配置可能是解决方案。但是基于文本的声明性配置又如何呢?
任何建议都可以接受!谢谢!
发布于 2019-05-21 05:44:27
tinylog是一个适用于所有Java应用程序的通用日志库。没有对上下文查找的本地支持,因为它是特定的Java EE特性。但是,您可以在启动时通过ServletContextListener加载您的自定义tinylog配置。
@WebListener
public class LoggingInitializer implements ServletContextListener {
public void contextInitialized(ServletContextEvent event) {
try {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
String stage = (String) new InitialContext().lookup("java:comp/env/stage");
String file = "tinylog_" + stage + ".properties";
Properties configuration = new Properties();
try (InputStream stream = classLoader.getResourceAsStream(file)) {
configuration.load(stream);
}
Configuration.replace((Map) configuration);
} catch (IOException | NamingException ex) {
Logger.error(ex, "Failed to load tinylog configuration");
}
}
public void contextDestroyed(ServletContextEvent event) { }
}后台文件可以设置为context.xml中的环境变量:
<Context>
<Environment name="stage" value="PROD" type="java.lang.String" />
<!-- Other Settings -->
</Context>https://stackoverflow.com/questions/56186203
复制相似问题