我们使用 JNDI 属性(在 Tomcat 网络服务器中设置)来确定阶段(DEV/TEST/QA/PRD),以便配置一些应用程序细节。
现在我们想用外部工具替换 homebrew-logging 并想试试 tinylog。但是我们想知道是否可以从 JNDI 上下文中读取环境变量来配置 tinylog 设置?
该文档没有说明 JNDI 查找。也许基于 Java 的配置可能是解决方案。但是声明性的基于文本的配置呢?
任何建议都可以接受!谢谢!
我们使用 JNDI 属性(在 Tomcat 网络服务器中设置)来确定阶段(DEV/TEST/QA/PRD),以便配置一些应用程序细节。
现在我们想用外部工具替换 homebrew-logging 并想试试 tinylog。但是我们想知道是否可以从 JNDI 上下文中读取环境变量来配置 tinylog 设置?
该文档没有说明 JNDI 查找。也许基于 Java 的配置可能是解决方案。但是声明性的基于文本的配置呢?
任何建议都可以接受!谢谢!
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>