I want to have a log4j configuration such that the log file name should have like ${System-name}log.log. that is if the application is launched on any system then without changing the configuration file or code. it should generate the log file name as mentioned. thanks.
Asked
Active
Viewed 8,069 times
2 Answers
3
I do it this way:
1) init logger by:
System.setProperty("my.logsDir", vcsLogsDir);
DOMConfigurator.configure("c:/log4j.xml");
2) in log4j.xml i use variable:
<param name="File" value="${my.logsDir}Default.log"/>d

bugs_
- 3,544
- 4
- 34
- 39
-
Can you please tell me, how it can set log4j.xml file's related relative path rather than absolute path ex: DOMConfigurator.configure("../log4j.xml") . thanks. – Channa Nov 19 '14 at 09:56
-
1You can set relative path exactly as you has written ("../log4j.xml"). It will find your log4j.xml relatively to your working directory. – bugs_ Nov 20 '14 at 08:38
-
Thanks for feedback. I got it with ContextClassLoader as: DOMConfigurator.configure(Thread.currentThread().getContextClassLoader.getResource(".").getPath() + "log4j.xml") – Channa Nov 21 '14 at 02:00
-
1I am afraid, that it will not work if your log4j.xml is zipped inside jar/war. You should load your log4j.xml regullary from classpath. Or copy it from classpath into some temp directory. – bugs_ Nov 21 '14 at 09:14
-
Yes your are correct, it will not work with a executable jar. Many thanks for the worth full feedback. As you mention I have tried with relative path for log4j.xml but it was not working. When it call on under "com.org.abc.util" package and "log4j.xml" is on inside the jar file, whether I can define relative path as "..\\...\\..\\..\\log4j.xml" or whether I should copy the log4j.xml some directory out side the jar file and have to give that path as "Dlog4j.configuration=resources/log4j.xml". Can you please help me to set log4j.xml file path. Thanks a lot. – Channa Nov 24 '14 at 05:55
-
1I have accomplished the issue as, URL url = ClassLoader.getSystemResource("log4j.xml"); DOMConfigurator.configure(url). Many thanks. Have a nice time! – Channa Nov 24 '14 at 06:25
2
You can use Java system properties with the ${property}
syntax. There are number of system properties that are defined by default including
"os.arch" Operating system architecture
"os.name" Operating system name
"os.version" Operating system version
For a full list of default properties see API of System or list them with System.getProperties().list(System.out)
.
If the default properties won't do then you'll have top add your own properties using System.setProperty(property, value)
.

Aleksi Yrttiaho
- 8,266
- 29
- 36