简体   繁体   中英

Logging to an external file using Apache Camel

I have a Camel Route which consumes a CSV file, converts it to XML and streams the data to ActiveMQ topic. I just want to log messages from my Camel Route, also detail log message about processing a message, etc.

Code:

public void configure() throws Exception {

    from("file:src/main/resources?fileName=data-sample.csv")
         .log("My first log message")                               
         .process(new MyTransformRevised1())
         .to("file:src/main/resources/?fileName=emp.xml")               
         .split(body().tokenizeXML("equityFeeds", null))
         .streaming()
         .to("jms:topic:reuters.inbound.Topic");
}

Adding log4j.properties file:

# Root logger option
log4j.rootLogger=INFO, file, console

log4j.logger.org.apache.camel=DEBUG

log4j.logger.camelprojectupdated.CSVToXMLTransformationRevised1=INFO, file

# camelprojectupdated.CSVToXMLTransformationRevised1 - fully qualified class name of my project. camelprojectupdated is my package name and CSVToXMLTransformationRevised1 is my class name.  

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=camel.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d | %p | %F %L | %m%n

# Direct log messages to stdout
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{HH:mm}| %p | %F %L | %m%n

Should it be log4j.properties or log4j2.properties? Also should I have log4j or log4j2 for eg: log4j2.appender.console instead of log4j.appender.console

Requirements:

  1. I would like to use slf4j and log output to both the file and the console.
  2. Where do I give the log file name and location, ConsoleFileAppender and RollingFileAppender , etc.

Issues:

I am not able to see the logs even in Console. My log file is not getting created. I have placed the log4j.properties file in the classes folder. Do I have to place the log4j.jar in the classpath even if I want to use slf4j?

It's getting very difficult.

You can load the logger property file and set up the logger for usage(log4j).

public enum Logger {
INSTANCE;
public void initializeLogger() {
        PropertyConfigurator.configure(Logger.class.getResource("log4j.properties"));
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM