I have an EAR that is made up of two modules. Both expose services and share common code.
Imagine that the ear has a common.jar shared by a webservices.war and webapp.war.
I use log4j to log the activities. I would like to be able to have two log files (webservices.log and webapp.log) capturing the events that are specific to each of them plus all the stuff that is handled by the common.jar.
How should I configure my categories and my appenders to achieve this?
At the moment I have the following packages:
com.myapp for shared stuff
com.myapp.webservices for the webservices and
com.myapp.webapp for the webapp.
My problem is that I don't know how I can capture the com.myapp (common stuff) in both log files by using a single log4j configuration file.
I have tried setting up multiple configuration files but when JBoss would work OK Websphere would break and the other way round...
Thank you
You can keep log4j configuration anywhere you like, just make sure Logger is able to initialize the engine from the configuration file. I don't see any reason why one configuration file is not working for both Jboss and WebSphere. Could you be more specific on what is breaking?
You need to configure two file appenders named webservices and webapp in same log4j configuration files and using the package names redirect to the relevant appender.
<logger name="com.myapp.webservices">
<appender-ref ref="webservices" />
</logger>
<logger name="com.myapp.webapp">
<appender-ref ref="webapp" />
</logger>
Related
I'm working on a Strus2+iBatis project.
When I config the log4j.properties for my project in WEB-INF/classes/...
the iBatis and Struts2 show it's debug information. but sometimes I only want to see my code's log.
I know there is a Hierrachy Logger object in my project. so quesiton is:
1, Is my RootLogger alse the root logger of the iBatis and Struts2 ?
2, How to swith off those 3rd lib's log information?
3, In which way the log4j put my logger in the Hierrachy ? (I guess is: if the logger name is "com.domain.proj.pkg" then log4j will create "com"->"domain"->"proj" 3 namespace and put my logger "pkg" in this namespace)
Update the log4j configuration file to set the log level for Struts2 and iBatis packages to WARN. Following is the settings for Struts2 packages. Similarly add iBatis packages to the list.
log4j.logger.com.opensymphony.xwork2=WARN
log4j.logger.ognl=WARN
log4j.logger.org.apache=WARN
log4j.logger.org.apache.struts2=WARN
log4j.logger.org.apache.struts2.util.StrutsTypeConverter=WARN
If your log4j configuration is a xml file, you will need to add the struts2 and iBatis packages as below.
<logger name="com.opensymphony.xwork2" additivity="false">
<level value="WARN" />
<appender-ref ref="fileappender" />
</logger>
I am using Log4J v2.0 Beta3 in my application for logging and I am getting log messages generated when I run my unit tests. I checked the API for some way to set the log level to something like CRITICAL but I could not find any way to change the logger configuration.
In fact, I read this on the log4j2 website:
Note that unlike Log4j 1.x, the public Log4j 2 API does not expose
methods to add, modify or remove appenders and filters or manipulate
the configuration in any way.
So with that said. What is the correct way to disable logging from within unit tests?
I found my answer on the log4j2 website under 'Testing in Maven'. The recommended way seems to be to place a log4j2-test.xml file in src/test/resources. Placing the xml file into this directory will cause it to be used instead of a log4j2.xml.
You can disable logging with setting root level to off (<Root level="off"/>).
So place log4j2.xml file into src/test/resources with following content:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Loggers>
<Root level="off"/>
</Loggers>
</Configuration>
EDIT:
Removed <!DOCTYPE xml> (based on suggestion from sprynter) to make xml valid.
Both the answers from #Jbug and #Dove work as long as you are placing the file in src/test/resources of the current module. If your app depends on a library to include log4j2.xml it won't work for test runs since library jar won't included test classes and resources.
With Weblogic 11g I have done the following:
1 Created log4j.xml file where I created a new appender:
<appender name="WEBLOGIC" class="weblogic.logging.log4j.ServerLoggingAppender">
<param name="Threshold" value="ERROR"/>
</appender>
<root>
<priority value="WARN"/>
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
<appender-ref ref="WEBLOGIC"/>
</root>
2 Updated the ${DOMAIN_HOME}/bin/setDomainEnv.sh script with these changes:
LOG4J_CONFIG_FILE="${DOMAIN_HOME}/config/log4j.xml"
if [ "${LOG4J_CONFIG_FILE}" != "" ] ; then
JAVA_PROPERTIES="${JAVA_PROPERTIES} Dlog4j.configuration=file:${LOG4J_CONFIG_FILE}"
export JAVA_PROPERTIES
fi
JAVA_OPTIONS="${JAVA_OPTIONS} ${JAVA_PROPERTIES} -Dweblogic.log.Log4jLoggingEnabled=true -Dwlw.iterativeDev=${iterativeDevFlag} -Dwlw.testConsole=${testConsoleFlag} -Dwlw.logErrorsToConsole=${logErrorsToConsoleFlag}"
3 Copied the log4j jars to the domain/lib
cp ./wlserver_10.3/server/lib/wllog4j.jar user_projects/domains/my_domain/lib/
cp ./wlserver_10.3/server/lib/consoleapp/APP-INF/lib/log4j-1.2.8.jar user_projects/domains/my_domain/lib/
4 Starts the AdminServer, but I get this error:
java.lang.ClassCastException:
weblogic.logging.log4j.ServerLoggingAppender cannot be cast to
org.apache.log4j.Appender
Keep step 1 and remove changes done in other steps.
Now copy the log4j.xml to $DOMAIN_HOME/lib folder. This will keep your log4j.xml in the server's classpath and the server will use this log4j.xml as its log4j configuration. No additional changes are required.
In setDomainEnv.xml file, please add the set log4j.xml location as below:
set LOG4J_CONFIG_FILE=C:\bea\user_projects\domains\dev\lib\log4j.xml
if NOT "%LOG4J_CONFIG_FILE%"=="" (
set JAVA_PROPERTIES=%JAVA_PROPERTIES% -Dlog4j.configuration=file:%LOG4J_CONFIG_FILE%
)
What you have to see is if despite the warning that you get, if the ServerLogging is effectively not working on your appliction on your domain.
Probably not even there since you are only copying the log4j to the domain/lib folder but not the wllog4j.jar.
So your setup looks like is doomed not to work in any case.
To me, It looks to me that you are undergoing Class Loader Vodoo Messup.
(1) You set a global log4j properties file for the entire app server.
(2) it looks like weblogic console app is itself a ear and it bundles its own LOG4J implementation library, which you are copying into your domain.
When the console app runs, it must for sure make use of Log4j and the class loader loading the Log4j Appender definition is most likely a level lower than the class loader that knows about the ServerLoggin bridge adapter.
I am beting its for reason like this that weblogic is geting rid of LOG4j in future gnerations of the product.
They have too many class loader issues - JUL logging you have the APP class loader behind the core classes - such as Handlers/Adapters.
Anyway.
(3) When weblogic runs, namley when it bootstraps the console it probably runs some sort class loader that gives a level of isolation to the libs bundled in the applicaiton and it sees:
Oh! How nice, LOG4j is here, leets initialize it.
Second, LOG4j bootstraps and hits head on your log4j.properties where you put the server logging appender in there - cross cutingly for everybody (including the weblogic application console).
He goes hunting for this library and where does it find it?
One of them, the wllog4j.jar he finds somewhere in the weblogic generic container libraries. While the base core classes of Log4j he finds a level lower in the domain configuration or in the EAR configuraiton of the console applicaiton.
This is not good.
Try the following:
(a) go into the weblogic console app, go to the Meta Inf folder and re-write the weblogi-application.xml
It will have a prefer applicaiton packages that looks like this:
org.apache.log4j.*</package-name--> <---- here I've commented this line.
If you comment the above line, the application class loader will search for log4j libraries in the highest parent that knows about these classes.
So if you put at the same level where your ServerLogger is found you should be fine.
(b) Pug the log4j implementation library at the same level where the wllog4j.jar will be found.
Check if you still get the casting exceptions.
Be very careful when you make server wide cross cuting configurations.
Good luck.
I am new to JavaEE SSH environment, and currently I use log4j as my application's log system. But the problem is that if I set the log output level at DEBUG there are too many console output in MyEclipse, switch the output level to WARN will reduce the amount of the messages but also lost some information I interested in. So my question is how to let the log4j ONLY output ALL the log message generated by the Java file I am editing and DO NOT output ANY messages generated by others.
Assuming you are configuring log4j with a log4j.properties file, you can set a specific class or package to a different level like this:
log4j.logger.com.foo.MyClass=DEBUG
See http://logging.apache.org/log4j/1.2/manual.html for more introductory log4j stuff.
http://logging.apache.org/log4j/1.2/manual.html
You can configure the log-level of every Logger you created via Logger.getLogger("org.springframework") . You must search the configuration file for log4j.
Example for XML (from http://en.wikipedia.org/wiki/Log4j):
...
<logger name="org.springframework">
<level value="info"/>
</logger>
<!--
everything of spring was set to "info" but for class
PropertyEditorRegistrySupport we want "debug" logging
-->
<logger name="org.springframework.beans.PropertyEditorRegistrySupport">
<level value="debug"/>
</logger>
Hope that helps.
if you are running in the unix/linux environment, you can tail and grep the log file what exactly you are looking for. this is more flexible than modifying log4j configuration and much more powerful
I had the same issue whereby my log4j debug logging was being overwhelmed by Spring debugging in my JUnit unit tests (in Eclipse). I got around this by adding the following to my log4j properties file.
log4j.logger.org.springframework=FATAL
I am starting up a Seam + Hibernate + Logback project deployed to Tomcat 6. I use Eclipse as an IDE. Hibernate prints many debug statements, so I have put the level to ERROR, so I don't see them.
<logger name="org.hibernate">
<level value="ERROR" />
</logger>
I don't paste my whole logback.xml, because the problem I have, is not connected with its contents.
I have the following classpath problem - If I put the logback.xml in WEB-INF\classesof my project, it is not found by the classloader and I see all debug statements. On the other hand, if I move the logback.xml to the lib directory of Tomcat, everything works perfect and the debug statements are not printed.
But I want to have my logback.xml inside my project, not in the root lib directory of Tomcat.
And here is my question - isn't the WEB-INF\classes a correct place for logback.xml? Why isn't it found?
I have always known that the files under the WEB-INF\classes directory of the project, are put to the classpath. Thank you for any ideas you have.
All the best, Petar
It looks like your logback.jar is also under ${catalina.home}\lib.
If you want to put your logback.xml to \WEB-INF\classes, the logback.jar should be only under \WEB-INF\lib. The ${catalina.home}\lib\logback.jar should be removed.