1

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 e.g.: 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.

sidd
  • 195
  • 3
  • 20
  • What is the message your want to log? And in which log file? – Hassam Abdelillah Apr 18 '20 at 17:52
  • @Hassam: I want to log message content, tracing message flow, logs message from Java code, etc. I want to logs messages to a separate log file e.g. myapplication.log as well as in Console. – sidd Apr 19 '20 at 05:22
  • Which slf4j implementation are you pulling in? Are you sure it actually is log4j? – Ralf Apr 20 '20 at 11:57

1 Answers1

1

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"));
   }
}
Nagaraj Kandoor
  • 305
  • 5
  • 18
  • shouldn't it pick my log4j2.properties file if its in classpath i.e. src/main/resources folder instead of doing the above. – sidd Apr 19 '20 at 06:21
  • But you mentioned in the question class folder. – Nagaraj Kandoor Apr 19 '20 at 06:24
  • log4j2.properties is present in the src/main/resources folder. Still it's not being picked up and my message are not being printed to an external log file :-( – sidd Apr 19 '20 at 06:46
  • Are you adding log4j properties while running? if not Add the log4j.properties file to the Classpath. Right-click on your project name and select Run As -> Run Configurations. Go to the Classpath tab and click on the Advanced button. Select the Add Folders option and click on OK button. And select resources. – Nagaraj Kandoor Apr 19 '20 at 07:06
  • I did as per your directions. Still its not able to pick up the log4j.properties file :-( – sidd Apr 19 '20 at 08:25
  • why can't you try above mentioned code and initialize logger in your main class? – Nagaraj Kandoor Apr 19 '20 at 13:57