on 06-15-2009 5:11 AM
Hi,
I'm implementing Log4j in my EJB. Now i'm having problem to read/locate my log4j.properties file. I not sure the path i put my log4j.properties. I did try in few places but seem not working also. My EJB is packaged with a WAR in an EAR file.
Currently i'm placing my log4j.properties file in the root folder of my EJB. (CustomEJB>log4j.properties)
This is my code in my EJB
....
public class GatewayBean implements SessionBean {
static Logger logger = Logger.getLogger("GatewayBean.class");
public String[] testLog() {
try{
PropertyConfigurator.configure("log4j.properties");
logger.info(new Date() + " testLog");
logger.info(new Date() + " Testing for logging");
}catch (Exception e){
e.printStackTrace();
}
}
...
When it try to load the properties file, it unable to find. This is the error that i got.
log4j:ERROR Could not read configuration file [log4j.properties].#
java.io.FileNotFoundException: log4j.properties (The system cannot find the file specified)#
log4j:ERROR Ignoring configuration file [log4j.properties].#
No problem for WEB application. But only for EJB. I try to put in the EJB>META-INF, also not working. So i not sure where to put the properties file and it can load. I try to include in my Java Build Path but cannot as is only can include .jar n .zip file.
Thanks in advance for the help.
-
Just now i google for this and i try to get my the path when running this program. I realized the path is \usr\X01\DVEBMGS01\j2ee\cluster\server0
So i put the log4j.properties file in that path and now it able to read it. But i don't think this is the correct way as it should be in the deployment file. What i think is weird that isn't the path should be at \usr\X01\DVEBMGS01\j2ee\cluster\server0\apps\sap.com\GatewayEAR......?
Edited by: Adrian Chan on Jun 15, 2009 8:53 AM
Hello,
When you provide the path to the file as "log4j.properties", then the JVM will search the file in the current working directory (CWD). For the server processes the CWD is /usr/sap/SID/INST/j2ee/cluster/serverN, where DIS is your 3-alphanumeric system ID, INST is the instance name, and N is the number of the server node.
In your case it is:
/usr/sap/X01/DVEBMGS01/j2ee/cluster/server0
So you need to drop the file there:
/usr/sap/X01/DVEBMGS01/j2ee/cluster/server0/log4j.properties
However, this is not a good practice! Why?
The SAP system is a cluster solution. Once you add another server node, the new server process will search the file in its own CWD, which of course is different. So adding a server node you have to keep in mind to copy the file.
That's why, the best would be to keep the file in the cluster share directly, i.e. /usr/sap/X01/SYS
For example, you could create a folder for log4j there and copy the file in the new folder:
/usr/sap/X01/SYS/log4j/log4j.properties
Then in your code call the method configure(String) with the absolute path:
PropertyConfigurator.configure("/usr/sap/X01/SYS/log4j/log4j.properties");
Hope this helps.
Kind regards,
Tsvetomir
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for your clarification.
Regarding your suggestion for the path, that would be a hardcode path right? I can't hardcode the log path in the program as this J2EE application will deploy in another server. I think of getting the context path then i map it back to get the entire path for the log4j.properties. Another thing that i concern is, if i unable to login to the SAP Netweaver server in order to drop my log4j.properties, how can i do it? I just finding a way where i can deploy my EAR file and i no need do any setting for the log4j.
Thanks.
Yes, correct.
The hardcoded path is applicable only if you deploy the application on once specific AS Java system (X01 in this case).
If you are not going to change the content of the file log4j.properties, then you can put it as part of the Java package hierarchy. In order to get the data, you can load it as a resource.
Example:
Let's assume your GatewayBean class has a fully qualified name com.some.company.app.ejb.GatewayBean. Then the file log4j.properties must be located in the Java class filestructure at:
com/some/company/app/ejb/log4j.properties
Of course, when you build the application, the log4j will be zipped in the JAR file together with the Java classes.
Then you can configure log4j as follow:
PropertyConfigurator.configure(GatewayBean.class.getResource("log4j.properties"));
The drawback of this approach is that you will need to redeploy the application if you have to change the log4j.properties file. However, should you need to change the content of the file frequently, then the log4j config data must be provided via java.util.Properties dynamically.
Kind regards,
Tsvetomir
User | Count |
---|---|
76 | |
10 | |
8 | |
6 | |
6 | |
5 | |
5 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.