Where in my Eclipse project should I add the log4j.properties
file so that it will work as intended?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
If you have a library and you want to append the log4j:
(I have assumed that you have the log4j library added.)
PD: Sorry for my english.
In general I put it in a special folder "res" or "resources as already said, but after for the web application, I copy the
log4j.properties
with the ant task to the WEB-INF/classes directory. It is the same like letting the file at the root of the src/ folder but generally I prefer to see it in a dedicated folder.With Maven, the usual place to put is in the folder
src/main/resources
as answered in this other post. All resources there will go to your build in the root classpath (e.g.target/classes/
)If you want a powerful logger, you can have also a look to slf4j library which is a logger facade and can use the log4j implementation behind.
Put
log4j.properties
in the runtime classpath.This forum shows some posts about possible ways to do it.
Add the log4j.properties file to the runtime class path of the project. Some people add this to the root of the source tree (so that it gets copied to the root of the compiled classes).
Edit: If your project is a maven project, you can put the log4j.properties in the src/main/resources folder (and the src/test/resources for your unit tests).
If you have multiple environments (for example development and production), want different logging for each environment, and want to deploy the same jar (or war, or ear) file to each environment (as in one build for all environments) then store the log4j.properties file outside of the jar file and put it in the class path for each environment (configurable by environment). Historically, I would include some known directory in each environment in the classpath and deploy environment specific stuff there. For example, ~tomcat_user/localclasspath where ~tomcat_user is the home directory of the user that will be running the tomcat instance to which my war file will be deployed.
The safest way IMO is to point at the file in your run/debug config
! Be aware: when using the eclipse launch configurations the specification of the
file:
protocol is mandatory.In this way the logger will not catch any logging.properties that come before in the classpath nor the default one in the JDK.
Also, consider actually use the log4j.xml which has a richer expression syntax and will allow more things (log4j.xml tahe precedence over log4j.properties.
This question is already answered here
The classpath never includes specific files. It includes directories and jar files. So, put that file in a directory that is in your
classpath
.Log4j
properties aren't (normally) used in developing apps (unless you're debugging Eclipse itself!). So what you really want to to build the executable Java app (Application, WAR, EAR or whatever) and include theLog4j
properties in the runtime classpath.