How can we tell log4j where is the properties file? - java

I know we can put it in the home directory of the project and it works as a charm. But really I want to put it under a directory name "Conf" for convenience of later maintenance. How can we do this?

log4j gives option to use different file as input. Also you can do a watch on the contents. See http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PropertyConfigurator.html#configureAndWatch(java.lang.String, long) for details

I don't know about 'we', but the log4j documentation is perfectly clear.
http://logging.apache.org/log4j/1.2/manual.html will tell you that log4j searches for log4j.properties or log4j.xml in the classpath, until and unless you use a system property to tell it to look for a different file or in a different place.
You might also find http://robertmaldon.blogspot.com/2007/09/programmatically-configuring-log4j-and.html interesting as an example of explicit configuration from code; it does not use a log4j.properties At All.

Related

who is reading logback.xml

Good day,
I have a existing Java web application host by websphere developed by senior, that using org.slf4j.Logger for logging. And I found that I have a logback.xml in my workspace, I do some changes on the logback.xml and its take effect.
However, I am curious which file is reading this logback.xml, I search for whole workspace, didnt found any place that is point to logback.xml.
I try to google on this, some is code to read this file, but didnt see any case like mine.
It it read by the the org.slf4j.Logger jar file?
Kindly advise.
You're already looking to the right direction. The configuration is read by logback (how this is done is described on a high-level here at the logback documentation). So the loading of the configuration happens in logback but not in slf4j (which is "just" a facade for different logging frameworks like log4j or in your case logback) as you suspected. Find some more details about SLF4J and the relation to logback here.
Hope that makes it a little more clear.

logging frameworks and logback.xml

While creating an example using Java and the com.alvazan.orm.api library, the use of "System.out.println" is prohibited.
One of the first and most simple Java exercises learned is "Hello World", also using the
"System.out.println" (...also known as logging, or returned requested data found in the console?)
When using Eclipse, logging is turned off by modifying the logback.xml file (ctrl-shift-R and typing in logback.xml)
From there;
<logger name="com.alvazan.orm" level="WARN"/>
is the line to add to the logback.xml file so that only startup logs appear.
In addition, two more logs such as....
2012-09-14 22:05:08,067 com.alvazan.test.FactorySingleton createFactory
INFO: CREATING FACTORY FOR TESTS
2012-09-14 22:05:08,809 com.impetus.annovention.ClasspathDiscoverer processFile
INFO: adding folder to scan=file:/C:/AAROOT/workareas/area1/playorm/eclipsegen/
are used.
Just clarifying that all information is typed into the logback.xml file?
Is there a diffrent file to use(other than logback.xml)?
Or is the end-user to use, for instance, "com.alvazan.test.FactorySingleton createFactory"; and "com.impetus.annovention.ClasspathDiscoverer processFile"?
Finalizing this question, is the file path for the preceeding necessary?
Thanks for your time,
Ryan
In response to Brett, and additional information/questions,
How is your root logger configured? You are only setting WARN for com.alvazan.orm, so if your root logger is INFO, then com.alvazan.test INFO's will be logged.
Hey Brett, thanks for the reply...
As for the root logger configuration, I believe the value is set at "INFO".
That being said, I would want to set "INFO" to "WARN", to prevent the use of
"System.out.println"
Also in the previous question, I mentioned:
2012-09-14 22:05:08,067 com.alvazan.test.FactorySingleton createFactory
INFO: CREATING FACTORY FOR TESTS
2012-09-14 22:05:08,809 com.impetus.annovention.ClasspathDiscoverer processFile
INFO: adding folder to scan=file:/C:/AAROOT/workareas/area1/playorm/eclipsegen/
com.alvazan.test.FactorySingleton
and
com.impetus.annovention.ClasspathDiscoverer (diffrent package within same library)
Different locations found within the same library...
Do i need to do the logback process for the other files, or *package, or doing it one time within the same library, should suffice for all? Or do I adjust additional values?
Your first statement....
"While creating an example using Java and the com.alvazan.orm.api
library, the use of "System.out.println" is prohibited."
In general, no one uses in System.out.println. Hibernate does not, JBoss does not, tomcat does not. They all use a logging framework so you can configure each and every log in production and each company using tomcat or jboss or hibernate can configure it differently. If these programs use System.out.println, customers would have no control and your server would ALWAYS run slow as you NEVER want "all" logging and there is no way to turn System.out.println's off!!!!! they are always on. log.info can be turned off and on.
The most complete answer on configuring logback can be found in their documentation
http://logback.qos.ch/manual/configuration.html
Just clarifying that all information is typed into the logback.xml
file?
I am not sure what you mean by this question. logback.xml is the configuration for a logging library called logback which you can find at the link above.
Is there a diffrent file to use(other than logback.xml)?
logback has other options like a groovy file to configure it, but playOrm is using only logback.xml though any client can decide what configuration file they use since playOrm discards the logback.xml file that is checked in when delivering to other projects.
Or is the end-user to use, for instance, "com.alvazan.test.FactorySingleton createFactory";
and "com.impetus.annovention.ClasspathDiscoverer processFile"?
I am very confused by this question. The end user should not be using FactorySingleton(neither directly nor indirectly AND that class is not even in the jar because it's in the test package). The end-user will be using ClasspathDiscoverer only indirectly...in fact, end-user won't even know about these classes.
Finalizing this question, is the file path for the preceeding necessary?
Are you trying to ask is the file path in logback.xml necessary? If you want to know more about how logback works, you need to read alot of the documentation. Basically, you can do stuff like com.alvazan level=WARN to turn any classes in com.alvazan.** to warn level(This is recursive and applies to children, grandchildren, etc. etc.). The ROOT logger is always defined as well in logback.xml and is the level for ALL classes in a all packages unless overridden.
yes, the root logger in playorm is set to warn.
At the bottom of the picture you show(your picture is cut off so not in that picture), there is a "source" tab and you may want to click that to view the xml better and it would match up with logback's documentation better as well. Here is a link to the file I bet you are looking at..
https://github.com/deanhiller/playorm/blob/486079cfefbd2b4b79e99652b24c146572663dda/input/javasrc/logback.xml
root logger is clearly set to info and could be set to warn if you want.
So, what do you want. Do you want those two log statements to go away and be turned off? Do you want ALL log statements to be turned off except WARN?
If you want all to be turned off in ALL software libraries, just change the root logger to "WARN". If you want to turn just FactorySingleton off you can add this line
<logger name="com.alvazan.test.FactorySingleton" level=""WARN"/>
If you want all of com.alvazan logging turned off instead of just everything in com.alvazan.orm as you also want com.alvazan.test off as well, BUT you want all other libraries to still be on(BUT always want WARN on which you generally should want), then you could change this
<logger name="com.alvazan.orm" level=""WARN"/>
to the following instead
<logger name="com.alvazan" level=""WARN"/>
Your best bet to understand logback though is to read logback documentation.
How is your root logger configured? You are only setting WARN for com.alvazan.orm, so if your root logger is INFO, then com.alvazan.test INFO's will be logged.

Overview of log4j configuration

I am rather confused by how to configure Log4j I have picked up following snippets, but something written that pulls these concepts together would be useful.
Log4j looks for properties/xml files in the CLASSPATH
You can supply
your own file using PropertyConfigurator.configure(filename) - what
if you call this twice. Are the files effectively merged or is only
the last one used.
You can supply an explicit file using -D on the
command line
I want to be able to
Supply a basic config file that remains static
Allow an individual developer to add an additional file with extra options without having ot exit this main file.
According to this documentation
The existing configuration is not cleared nor reset.
log4j itself stops at the first log4j.properties it find.
So when you call configure, my guess is:
if some configuration is already done then the current will be merged.
if configuration has not been done then log4j will not attempt to read any other log4j.properties
Your best best is to probably call configure with global and then with developer-specific file. But this should be easy to test.

add logging to a utility package

I'm creating a java open source utility package and I would like to know if it is ok to include logging (like log4j) into that package.
The dilemma is if I include log4j into my package, where will i output the log file, I wouldn't want the log file to be at a wrong place for the user, but i would still want to create logs for debugging.
As well there is the issue of the user wanting to integrate my logs into the project itself.
and how will he be able to do that.
What would you recommend is the best way of doing this?
Thanks.
I think the normal way to do it, is not include the Log4j Jar in the package and let the library user to decide which version of Log4j to use.
Also you don't have to worry about where to output the log file, since the user will have its own log4j.properties or log4j.xml configuration.
The library user can also decide which level of logging to use from your library or whether to suppress it. For instance with Amazon AWS libraries, I can tell that I only want warning messages or the output will be too verbose. In that case I add to my log4j.properties:
log4j.logger.com.amazonaws=WARN
Said that, I'd also remind you that a modern alternative to log4j is SL4J.

java log4j.xml multiple appenders sharing a layout

Is there a way to configure a log4j.xml file to have multiple appenders share the same layout? I have copied the layout parameter into each of the appenders but it's a pain (and seems weird that I would need to do this) to update it in multiple places if the pattern changes.
thanks,
Jeff
Have you considered using log4j.properties instead of log4j.xml? The properties version accepts variable substitution for the values.
You can create your own properties inside the log4j.properties file (someProp=value) and later use ${someProp} to get the value.
From what I remember (not sure though :D) you can also have this in the log4j.xml file but the variables must be defined as system variables (-DsomeProp=value) and in your log4j.xml you again use ${someProp}. This version though is a little messy because you do not have the params declared in the same place you are using them, as you do in log4j.properties.

Categories

Resources