Configuring CXF Logging To Go Through Log4J
Apparently, CXF has chosen the JSE logging API as their API against logging. But you can make CXF log through Log4J quite easily. In the org.apache.cxf.common.logging package of the CXF apidocs, I found the Log4JLogger class, which is a specialization of the JSE logging api java.util.logging.Logger class, delegating logging to a Log4J implementation.
Configuring Log4J in CXF
In the same package as Log4JLogger, I also found the LogUtils class, which CXF uses to obtain logger instances from. This class also contains a static final string KEY, which is set to the value org.apache.cxf.Logger. CXF uses this value, to bootstrap which logging API to use.
You can either put a META-INF/cxf/org.apache.cxf.Logger file onto the classpath with the org.apache.cxf.common.logging.Log4jLogger class name in it as a single line, or you can supply a -Dorg.apache.cxf.Logger=org.apache.cxf.common.logging.Log4jLogger system property to your code, when executing.
January 30, 2008
Tags: cxf, Java, log4j, logging Posted in: Programming

25 Responses
Tech Per » Blog Archive » Viewing Inbound and Outbound Messages in CXF - January 30, 2008
[...] messages through its logging API. As a standard, the logging API in CXF is JDK logging, but you can configure CXF to use Log4J logging instead. You will need to ensure, that at least INFO level logging has been enabled for these [...]
Tech Per » Blog Archive » Making CXF Log RuntimeExceptions From Server Implementations - February 28, 2008
[...] mentioned in my post on how to configure log4j as logging engine in cxf, cxf comes out of the box with JavaSE logging (java.util.logging) as its logging engine. If you [...]
I can’t find META-INF/cxf/org.apache.cxf.Logger file in my CXF directory.
Any idea how can I achieve logging.
That is because you are supposed to supply that file yourself, instead of relying on the default.
Create the META-INF/cxf/org.apache.cxf.Logger file somewhere on your classpath. If you are building a war, for instance, it should end up in WEB-INF/classes/META-INF/cxf/org.apache.cxf.Logger (or inside a jar in WEB-INF/lib).
Really gr8! helped me to solve longing issues in CXF
I have created META-INF/cxf/org.apache.cxf.Logger file under WEB-INF/classes directory.
Satheesh
Great info. I’ve found that a simple
System.setProperty(”org.apache.cxf.Logger”, “org.apache.cxf.common.logging.Log4jLogger”);
before any CXF invocation, solves the problem for me.
Cheers!
Great, thanks a lot
put org.apache.cxf.Logger file under WEB-INF/classes/META-INF/cxf/ directory
Thanks for the tips Per (and everyone else). I’ve opted for the same approach Camilo is using. It *does* log a great deal of information, but the “raw” request/response is not recorded. What am I missing?
To log inbound and outbound messages you alse need to do what this post says logging inbound and outbound messages
where do we find the org.apache.cxf.Logger file. I am not having cxf directory in META-INF directory.
You are supposed to create that directory and file in it yourself. So,..
1. in your source code make a META-INF/cxf directory
2. create a file in that directory, specificly named “org.apache.cxf.Logger”
3. Write one single line in that file, with the content “org.apache.cxf.common.logging.Log4jLogger”
Make sure that your build process, being it ant, maven or something else, includes the META-INF/cxf catalogue in your compiled output (e.g., ensure it is included in your runtime classpath when your app executes somewhere).
Or, use the other approach of a system property, if that fits your needs better.
I have tried what you are suggesting (step 1 to 3) but no luck for me. Is there any additional jar file needed for this (I have log4j jar) or any other configuration in log4j.property file.
I tried with another method that is to use System property before the cxf invocation like
System.setProperty(”org.apache.cxf.Logger”, “org.apache.cxf.common.logging.Log4jLogger”);
This too does not work for me.
Please suggest me what I have to check.
@Saravanan:
Hmm, here’s a couple of things you could try
* if you would like some information from cxf, yes you should enable DEBUG or INFO or the like og org.apache.cxf package in log4j.properties
* if you are unsure if log4j config is read properly, try putting -Dlog4j.debug=true on the VM when starting your app, which should make log4j log to stdout when it is initializing and where it is loading log4j.properties from
* it seems like you can explicitly set the logger on CXF to the logging class you prefer, by doing “LogUtils.setLoggerClass(org.apache.cxf.common.logging.Log4jLogger.class)” (I haven’t tried this myself)
@polesen,
Thanks for your suggestions.
i tried with LogUtils.setLoggerClass(org.apache.cxf.common.logging.Log4jLogger.class and it works fine.
But it logs twice. I cant able to fix this.Any idea?Please help me to trigger out this issue.
@Saravanan:
“logs twice”.
Hmm. Strange. Could you provide a small sample of a couple of log statements as they appear when you see them twice?
@polesen
The extra log is due to I have configured Logging Interceptors in log4j.properties. Thanks for your continuous help. Finally it works fine. Thanks polesen.
Final clarification:
Now the logged Inbound and Outbound messages are printed in single line. Is there possible to log the messages as seperate lines.
@Saravanan: “…printed in single line”.
That is strange. Don’t know what happen there. The inbound and outbound messages are logged on two separately named loggers, so they should come as separate lines too.
FYI : CXF will also support logging through SLF4J in the forthcoming 2.2.8 version thanks to the org.apache.cxf.common.logging.Slf4jLogger .
Details are available on http://cxf.apache.org/docs/debugging-and-logging.html , paragraph “Using SLF4J Instead of java.util.logging (since 2.2.8)”.
Hope this helps,
Cyrille
Hi Guys
Thanks for the useful info. i tried the steps above but still cannot see the logging work. no one mentioned where to create the log4j property or xml file to specify the log file name and location. can some one advise pl….
regards
Paul Henderson
@Paul
The post as such is not about configuring log4j, but about if you already have log4j configured for your own system, how to make CXF log through that same configuration instead of JDK logger.
So, if your problems are related to log4j configuration itself, this post will not help you that much.
One tip though: A good place to start with log4j config debugging is starting your app with “-Dlog4j.debug=true”, as this will make it output how it initializes itself at bootstrap.
Hi Friends, ur post are really useful. I tried them and it works fine.
But I have a concern, It now logs all the activities…. is it possible to restrict it by any means.
–Thanks
Dharm
@Dharm: Don’t know about that. But I don’t think so.
Thanks polesen,
Let me eloborate my Question,
Now my logger is logging all the activities like
———————————————————
2010-06-22 10:30:23,243 DEBUG [WebContainer : 0] phase.PhaseInterceptorChain – Invoking handleMessage on interceptor org.apache.cxf.jaxws.interceptors.HolderOutInterceptor@52a852a8
2010-06-22 10:30:23,243 DEBUG [WebContainer : 0] interceptors.HolderOutInterceptor – op: [OperationInfo: {http://services.vzw.com/}getReferenceData]
2010-06-22 10:30:23,243 DEBUG [WebContainer : 0] interceptors.HolderOutInterceptor – op.hasOutput(): true
2010-06-22 10:30:23,243 DEBUG [WebContainer : 0] interceptors.HolderOutInterceptor – op.getOutput().size(): 1
2010-06-22 10:30:23,243 DEBUG [WebContainer : 0] phase.PhaseInterceptorChain – Invoking handleMessage on interceptor org.apache.cxf.jaxws.interceptors.SwAOutInterceptor@51fa51fa
2010-06-22 10:30:23,243 DEBUG [WebContainer : 0] phase.PhaseInterceptorChain – Invoking handleMessage on interceptor org.apache.cxf.jaxws.interceptors.WrapperClassOutInterceptor@52885288
2010-06-22 10:30:23,258 DEBUG [WebContainer : 0] phase.PhaseInterceptorChain – Invoking handleMessage on interceptor org.apache.cxf.binding.soap.interceptor.SoapHeaderOutFilterInterceptor@4d9c4d9c
2010-06-22 10:30:23,258 DEBUG [WebContainer : 0] phase.PhaseInterceptorChain – Invoking handleMessage on interceptor org.apache.cxf.binding.soap.interceptor.SoapPreProtocolOutInterceptor@4ef84ef8
2010-06-22 10:30:23,258 DEBUG [WebContainer : 0] phase.PhaseInterceptorChain – Invoking handleMessage on interceptor org.apache.cxf.interceptor.MessageSenderInterceptor@4fee4fee
2010-06-22 10:30:23,258 DEBUG [WebContainer : 0] phase.PhaseInterceptorChain – Adding interceptor org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor@50065006 to phase prepare-send-ending
2010-06-22 10:30:23,258 DEBUG [WebContainer : 0] phase.PhaseInterceptorChain – Chain org.apache.cxf.phase.PhaseInterceptorChain@67b667b6 was modified. Current flow:
pre-logical [HolderOutInterceptor, SwAOutInterceptor, WrapperClassOutInterceptor, SoapHeaderOutFilterInterceptor]
post-logical [SoapPreProtocolOutInterceptor]
prepare-send [MessageSenderInterceptor]
pre-stream [LoggingOutInterceptor, AttachmentOutInterceptor, StaxOutInterceptor]
write [SoapOutInterceptor]
marshal [WrappedOutInterceptor, BareOutInterceptor]
prepare-send-ending [MessageSenderEndingInterceptor]
2010-06-22 10:30:23,258 DEBUG [WebContainer : 0] phase.PhaseInterceptorChain – Invoking handleMessage on interceptor org.apache.cxf.interceptor.LoggingOutInterceptor@67ac67ac
2010-06-22 10:30:23,258 DEBUG [WebContainer : 0] phase.PhaseInterceptorChain – Invoking handleMessage on interceptor org.apache.cxf.interceptor.AttachmentOutInterceptor@4d424d42
2010-06-22 10:30:23,258 DEBUG [WebContainer : 0] phase.PhaseInterceptorChain – Invoking handleMessage on interceptor org.apache.cxf.interceptor.StaxOutInterceptor@4d784d78
2010-06-22 10:30:23,274 DEBUG [WebContainer : 0] phase.PhaseInterceptorChain – Adding interceptor org.apache.cxf.interceptor.StaxOutInterceptor$StaxOutEndingInterceptor@47cc47cc to phase pre-stream-ending
2010-06-22 10:30:23,274 DEBUG [WebContainer : 0] phase.PhaseInterceptorChain – Chain org.apache.cxf.phase.PhaseInterceptorChain@67b667b6 was modified. Current flow:
pre-logical [HolderOutInterceptor, SwAOutInterceptor, WrapperClassOutInterceptor, SoapHeaderOutFilterInterceptor]
post-logical [SoapPreProtocolOutInterceptor]
prepare-send [MessageSenderInterceptor]
pre-stream [LoggingOutInterceptor, AttachmentOutInterceptor, StaxOutInterceptor]
write [SoapOutInterceptor]
marshal [WrappedOutInterceptor, BareOutInterceptor]
——————————————————-
Where as what we actually need is the inbound and outbound soap msg/fault….
is it possible by any means to stop this and log only required part.
Thanks
Dharm
Aaah, I see, yes. I think what you need is what I have described in this blogpost about outbound and inbound cxf logging.
It looks like, that you have turned on log4j debugging of all of cxf. For outbound and inbound logging, you need only turn debugging on for these two logger names:
org.apache.cxf.interceptor.LoggingInInterceptor
org.apache.cxf.interceptor.LoggingOutIntercepto
Thanks for the straight-forward and working logging configuration information! I now have some information to work with.
Regards,
Frank
Leave a Reply