One Star

[resolved] Logging to a file

I was wondering if anyone knew how to implement logging to a file in Camel? I have read of all the documentation and its still not clear to me how to accomplish it...
I have several logs in my route and I would like to log those in one location. I also want to log the exceptions that I am catching.

  • ESB
11 REPLIES
Employee

Re: [resolved] Logging to a file

hi Dave
Use the camel-log component for logging the current message using log4j. See http://camel.apache.org/log.html . In Studio this is available using the cMessageEndpoint in 5.0.2. It may have its own icon in 5.1.1.
Use the log eip to log messages, see http://camel.apache.org/logeip.html. In Studio 5.0.2 use the JavaDSL to implement the log eip.
For error handling see http://camel.apache.org/error-handler.html . I would suggest using the error handler with the exception handler rather than the try-catch components.
Note that some exceptions thrown by endpoints (rather than occurring within the eip's and processors) may result in faults rather than exceptions.
Note that there is a LoggingErrorHandler available out-of-the-box.
Some exceptions thrown by endpoints will result in faults. By default these are not handled by exceptions unless you set handleFault to true. This can be set on the context or route scope using setHandleFault(true) or handleFault() respectively.
One Star

Re: [resolved] Logging to a file

Thanks Ed. I am using the log and exception handler (I will look into the error-handler).
I guess what I meant to ask is right now Talend logs everything to the tesb.log folder is there a way to log to a specific file and not just the tesb.log file? I would like to log to a different directory for each project that I have. I could write a bean and use log4j to do what I need to do but I wanted to see if there was something built in that would do it for me.
One Star

Re: [resolved] Logging to a file

I figured out that you can turn on logging for each container by enabling sift logging in the /container/etc/org.ops4j.pax.logging.cfg file, but I dont see it logging for my deployed containers. If I turn on debug it does some logging saying that my route is starting, but after that it doesnt log anything. Is there something I am missing here? Has anyone gotten this to work?
Employee

Re: [resolved] Logging to a file

This is really just a log4j question. What you need to do is first set up a new appender. For example:
log4j.appender.myAppender=org.apache.log4j.RollingFileAppender 
log4j.appender.myAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.myAppender.layout.ConversionPattern=%d{ABSOLUTE} | %-5.5p | %-16.16t | %C | %-32.32C %4L | %m%n
log4j.appender.myAppender.file=${karaf.data}/log/myLog.log
log4j.appender.myAppender.append=true
log4j.appender.file.maxFileSize=1MB
log4j.appender.myAppender.maxBackupIndex=10

Then attach the appender to a logger
log4j.logger.myLogger=INFO,myAppender

Now specify the logger as the in camel-log component. If you use the log component (which you can do from Studio) then the URI should be something like
log:myLogger
If you use log EIP with Java DSL it would be
from(...).log(LoggingLevel.INFO,"myLogger","Some message");
One Star

Re: [resolved] Logging to a file

Thanks Ed! This is exacly what I was looking for and works as expected.
Do you know what I would need to do to get the same kind of logging working from a Bean? I found a few different log4j examples but it seems that camel doesn't like that and throws this error: An Exception Occurred...org.apache.camel.CamelExecutionException: Exception occurred during execution on the exchange: Exchange
One Star

Re: [resolved] Logging to a file

bump
Employee

Re: [resolved] Logging to a file

Hi Dave
Sorry, I assumed you would just use a standard slf4j logger from your cBean component. Can you provide a snippet of code and some more detail on the error.
Ed
One Star

Re: [resolved] Logging to a file

Hey Ed,
Sorry I didnt see that you responded. We were actually trying to use log4j and not slf4j...
Here is an example of the code we had using Log4j, I guess we can try slf4j. Btw...the code in the else clause worked in eclipse but did not work when run as a bean...
package beans;
import java.io.File;
import org.apache.camel.Header;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class CheckForMarkerFile {

public String CheckForMarker(
@Header(value = "orginalFileFullPath") String originalFileFullPath,
@Header(value = "markerFileExtension") String markerFileExtension
) throws Exception
{
System.out.println("Checking for marker file " + originalFileFullPath + markerFileExtension);
try{

File aFile = new File(originalFileFullPath + markerFileExtension);
if (!aFile.exists()){
String result = "False - Marker File " + originalFileFullPath + markerFileExtension + " does not exist";
//tried this first without specifying the config file
Logger log = Logger.getLogger("keystoneWorkflow");
log.info(result);
System.out.println(result);
return result;
}

else{
String result = "True - Marker File " + originalFileFullPath + markerFileExtension + " exists";
//tried this with specifying the log file. This worked in eclipse but not as a bean.
PropertyConfigurator.configure("C:/Talend/Talend-ESB-V5.0.2/container/etc/org.ops4j.pax.logging.cfg");
Logger log = Logger.getLogger("keystoneWorkflow");
log.info(result);
System.out.println(result);
return result;
}

}catch(Exception e){
e.printStackTrace();
String result = "False - " + e + " - " ;
System.out.println(result);
return result;
}
}

}
Employee

Re: [resolved] Logging to a file

Yes, please try the slf4j approach. I'm not sure what will happen if you short circuit it with a direct call to the log4j api. slf4j as the name suggests is just another (albeit better) wrapper api for different logging implementations, including log4j. So you will still have all the power of log4j.
Employee

Re: [resolved] Logging to a file

See this post http://www.talendforge.org/forum/viewtopic.php?id=24692 regarding issues using slf4j. I will see if I can do a quick example with a cbean for you.
One Star

Re: [resolved] Logging to a file

Haven't updated this post, but I finally got it working. here is how the code looks in the bean.
Thanks for the help Ed!
package beans;
import java.io.File;
import org.apache.camel.Body;
import org.apache.camel.Header;
import org.apache.camel.Property;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;

public class Log2 {
final Logger logger = LoggerFactory.getLogger("myAppender");

public void logMessage(){

logger.info("This is logging from a Bean");
}

}