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.
Related
I have put my logger.properties file inside src/java/resources directory. Using this file I configure different levels of logging (for both console and to file). Now when I make a jar file of my project, how can I configure or change the logging level because then I won't have access to the logger.properties file.
Basically I want to provide users the ability to set the logging level before the run the jar.
In general you shouldn't keep the configuration file inside the jar.
Instead, place it somewhere (depends on your distribution, usually it can be something like $YOUR_PROJECT_HOME_DIR/conf folder). When running the application specify the following property:
java.util.logging.config.file=<PATH TO PROPERTY FILE GOES HERE>
Here is a good tutorial on java.util.Logging that covers this point among others.
You could use enviorment variables https://docs.oracle.com/javase/tutorial/essential/environment/env.html
You could also take the logging level you want to use as an argument for your program.
https://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html
I can't figure out how to implement changes to a local storm project using Eclipse. I can implement changes no problem on server deployed code by modifying the ./logback/clusters.xml file (e.g. changing log level from INFO to ERROR).
How could I go about doing this?
I am not sure if I understand correctly. What do you mean by "local storm project"? Do you execute using LocalCluster? In this case, you need to create a file "logback.xml" and put you logger configuration there. logback.xml must be in your classpath, ie, you can put it into "src" folder to enable Eclipse to find it. Or you put it somewhere else and extend your classpath accordingly.
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 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 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).