Loading properties file from WEB-INF folder in a log4j appender

我的未来我决定 提交于 2019-12-11 08:24:23

问题


We're writing a custom log4j appender for our application. The appender should log its events to a database. Now the problem I'm having is setting up the database connection. Our jdbc settings are in a file called jdbc.properties which is located directly under the WEB-INF folder.

I've tried accessing the properties file using the following code

InputStream stream = Thread.currentThread().getContextClassLoader()
                    .getResourceAsStream("jdbc.properties");

... but stream results in being null. Any ideas how I can load a properties file from the WEB-INF folder in a log4j appender without moving the properties file to another location?


回答1:


May be you can try,

 String  path =Thread.currentThread().getContextClassLoader().getResource("/").toURI().resolve("../jdbc.properties").getPath();
 Properties ps=new Properties();
 ps.load(new FileInputStream(path));



回答2:


You should be able to get the file via the ServletContext. i.e.:

ServletContext ctx = ...
InputStream stream = ctx.getResourceAsStream("/WEB-INF/jdbc.properties"); 

Okay, just saw, that you don't have access to the ServletContext - forget the answer.

Isn't it possible to add the information for the jdbc connection into the log4j.properties? Why are you seperating the two?



来源:https://stackoverflow.com/questions/8501284/loading-properties-file-from-web-inf-folder-in-a-log4j-appender

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!