I was thinking to use log4j for my standalone java project.
Is it possible to use it for java project(not a web app).
If its possible then how should I be able to initialize the properties file and where should
I put the properties file(directory structure).
Any help is appreciated.
Yes, it is possible, we do it all the time. You just need a log4j.properties file on your classpath, which in most cases means in the base directory of your jar file. If you wish to have multiple properties files with differing configurations, you can create a properties configurator in some entry point before your first logging statement.
Yes it's possible.
If you're planning to create a jar file.
Make sure that your log4j.properties is found in your classes folder in jar file.
Make sure that your log4j jar is included in your classpath.
Enjoy!
It's definitly possible to use log4j in a standalone project. Just set your classpath to include the log4j jar. As for the properties file, the apache documentation should say where to log4j looks for it (it's probably also configurable).
Related
I have a library project. This project uses log4j for logging.
Should I put log4j.properties into generated jar?
If it is not a good practice, could you tell me why?
You should put your log4j.properties outside your jar file because it will be easier to modify it. If the application does not force a reload of the configuration (you can do that) then a simple restart will load the config (while modifying it inside a jar file means usually a rebuild). If the application does know how to reload it, then your changes will be almost instantly applied.
This is very important when you have application in production environment, and you want to change a logging setting (like the log level) in order to get more info but not to stop the application for this (or worse, rebuild it).
No, I would not include log4j.properties in your src/main/resources if it's just a library - people will use your library, and people will have to configure how it should log.
But there is another problem: if you put it in your jar, others will have the log4j in their classpath too. So, if they want to have a custom configuration for log4j (which they will do), the ClassLoader will pick the first file it can find.
Also, if you have a library, I would suggest to use slf4j instead, so your users can choose their favourite logging framework.
For testing purpouses, it's okay to include log4j in src/test/resources.
I have a .jar file (foo.jar, for instance) that we can't alter that contains a commons-logging.properties file (that redirects to JDK14Logging) and relies on an external commons-logging.jar file for it's logging. This, I believe is set to simply output to stdout which isn't very helpful.
We have a different logging system (log4J if it matters) that we are using, and would prefer to use. Is it possible to have an external properties file (sitting local to the foo.jar) that 'overrides' the one inside foo.jar?
I'm aware that SLF4J should work, as should editing the .jar in 7zip and 'recompiling' so to speak, but I feel that the first creates an extra dependency on a Jar (as requires bringing one into the environment) and the latter creates a dependency on someone to make the same edits we made to foo.jar to foo2.jar when/if we are provided with that when/if I have left the project.
Is this possible without editing the jar or SLF4J?? Or do I need to bite one of the aforementioned bullets?
The quick fix (assuming the default classloaders are being used) would be to create your own commons-logging.properties file, contained in a directory that is on the classpath before the jar containing the unwanted commons-logging.properties file. Your commons-logging.properties file should be configured to use Log4J and in particular your log4j.properties file.
However I'd really recommend switching to SLF4J and using the SLF4J bridge implementation of commons-logging, which will make the commons-logging API being used by the libraries on which you depend irrelevant and allow you to control the log configuration from your project (via a single logback.xml file assuming you're using Logback).
I have implemented log4j in my web application project. Project is done using net beans,using tomcat 7.0.41. At first,I created log4j.property file and placed under web page->Web-INF->classes->log4j.properties in net beans and it asks me to locate the file in my project,so I manually located that file to implement log4j in my application. After that I changed the place of the log4j.properties file to myproject->build->web->WEB_INF->classes->log4j.properties in location of my project saved, now its working fine, it did not ask me to manually locate the property file, It takes automatically when my class files executed. Now my problem is that once I committed the project and again checkout the project on some day, property file does not appear and it again ask for property file. So where can I create the log4j property file in my project so that my team mates can utilize it when they checkout project in their system.
Normally you put log4j.properties to src/main/resources/ and it will be copied to the right place by the build process.
I never use net beans, but I think put log4j.properties under Classpath will work.
Not sure how Net Beans handels this, but i think that the "build" directory is where the "compiled" project is put to.
So i would not recommend to put any files there which should be versioned because mostly those directories are ignored for versioning ( see .gitignore files for example when using git).
Resources like property files should be within the sources and your IDE should copy them to the correct place when building the project.
I am using weblogic 10.3.4, I am trying to write log with log4j. but at runtime my application is not getting any log4j.properties. even this is not generating any warning as "initialization of log4j has error".
I have tried my properties file to put in src folder, classes folder and then I created one jar and put it in domain lib. still its not picking. even when I am writing log with same jar in standalone application, its working fine.
please help me with valuable suggestions.
I tried the solution proposed at Oracle forums.
Excerpt from that link at Oracle forums:
I've only modified the scritp startWebLogic.cmd:
set LOG4J_CONFIG_FILE=log4j.xml
set SAVE_JAVA_OPTIONS=%JAVA_OPTIONS% -Dlog4j.configuration=%LOG4J_CONFIG_FILE%
#REM set SAVE_CLASSPATH=%CLASSPATH%
set SAVE_CLASSPATH=%CLASSPATH%;C:\Oracle\Middleware\user_projects\domains\domain\config
In this way I've put all the config folder inside the classpath, and I can use it in future to hold other libraries configuration files (for example oracle coherence config).
I tried this approach on a different properties file as well and that worked well!
You need to either specify where the application should find its log4j.properties, or put it onto the classpath of the application. Where the classpath is varies, but in general WEB-INF/classes should work. Other options depend upon how you're deploying the application.
A better long term strategy is to configure your system so that you can change the log4j.properties depending upon the environment. When you're in production, you won't want all of the debug information to appear. Look at the answer to this question or this question for more ideas. One strategy is to define a variable on the command line which gets picked up and defines a directory which contains your configuration files. This works for Tomcat, but there may be other, better, strategies for Weblogic.
It is not a good idea to change the configuration of your server, in particular, don't replace the log4j.jar or log4j.properties in your server directories. The server will depend upon the version that it was designed to use, which may or may not be the same as your version. You can do everything you need to do by changing the war that you're deploying.
I have used this code:
ClassLoader cl = this.getClass().getClassLoader();
URL log4jCfg = cl.getResource(configFile);
if (log4jCfg != null) {
DOMConfigurator.configure(log4jCfg);
}
log.info("log4j is now working on Web App.");
In my case, we used XML configuration:
log4jCfg = "mylog4j.xml";
In WebLogic, we were able to place such file (mylog4j.xml), equivalent to your log4j.properties file, at WebLogic's domain path (specific to the domain were we deploy). This means that domain folder belongs to your application's path. I just tested it with Web applications, I'm not sure if with SOA or EJB projects it works the same way.
When you deploy any application on any server that application should use servers log4j jar.
So if you have added any log4j jar in your application jar/tar/ear, remove it and copy log4j.properties file in the conf folder of the server from where server is picking its configuration files. Or just copy your log4j property content in servers log4j property file.
I have a jar in the classpath of my server which contains the log4j.properties file on root of the jar for logging of the code. When the code in the jar runs the logging for the code is not occuring. When I analyzed the problem using java -Dlog4j.debug option I got to know that the log4j is loading the log4j.properties file of axis-ant.jar (this also is on my classpath) and using it instead of my jar's property file.
When i subsequently removed the axis-ant.jar from the classpath my jars log4j.properties file was picked immediately and logging for my code occured.
Can somebody please explain why is my log4j.properties fle not being loaded while axis-ant.jar is present?
Surprisingly the same jar with axis-ant.jar present in classpath is running with proper logging successfully in another server.. both the server have the same java 5 version installed and same version of log4j jar
You should have a look at the log4j manual. The section "Default Initialization Procedure" describes how log4j will try to find the initialization file and explains possibilities to match a special configuration (e.g. by setting the system property log4j.configuration).
If you do not want to do any special configuration you have to ensure that your configuration file will be the first one found on the classpath.
The properties file is picked up from the jar based on the order in which the jar is found in classpath by your application server. Now there is no way to conclusively tell the jar from which the properties file is to be loaded.
To get around this problem most projects include your log4j.properties file in the server classpath itself. This way this file is the first properties file to be loaded and this is the one which your logging code gets.
I'm note quite sure but it could be an order problem. Whichever log4j.properties is loaded first/last would win (I'd have to check that).
Possibly because axis-ant.jar comes before in classpath the your own jar. I assume putting your jar before in classpath then axis-ant.jar should help