Srijani Ghosh
Srijani Ghosh

Reputation: 4216

How to use log4j in RCP application with Maven?

I am working on a small RCP project which has Maven nature and now I wish to add log4j dependencies with that.
For that purpose what I did was :

  1. Added log4j.properties under bin folder inside the project. bin is the folder where all the class files are getting generated. The file looks like this:
# Root logger option
log4j.rootLogger=DEBUG, stdout, file

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\Srijani\\Personal Workspace\\RCP\\EditorApp\\log\\Application.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
  1. Added this dependency in pom.xml
<dependencies>
   <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
   </dependency>
  </dependencies>
  1. In java code I wrote like this:
import org.apache.log4j.Logger;

import com.app.editor.constants.Constants;

public class DatabaseConnection {
    final static Logger logger = Logger.getLogger(DatabaseConnection.class);
}

This code does not creata any problem while compiling. But, while running the code, I got this error:

!ENTRY org.eclipse.e4.ui.workbench 4 0 2016-01-13 13:50:35.397
!MESSAGE Unable to create class 'org.eclipse.ui.internal.e4.compatibility.CompatibilityView' from bundle '55'
!STACK 0
org.eclipse.e4.core.di.InjectionException: java.lang.NoClassDefFoundError: org/apache/log4j/Logger
    at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:62)

Note: I did not add anything to MANIFEST.MF and build.properties. Please help!

Thanks

Update: Got the issue but not sure how to solve it. When I am downloading the jar manually and set it in the class path, I am able to use log4j in Eclipse RCP. But, when I am trying download it via Maven, it is not working. Any idea why this is happening?

Upvotes: 0

Views: 571

Answers (2)

Tom Seidel
Tom Seidel

Reputation: 9535

If you use Maven in combination with Eclipse RCP you should consider using Tycho[1]. Tycho uses the MANIFEST-first approach, so that you don't need to edit the pom-files.

In addition you should put your log4j.properties into a fragment with the log4j-bundle as host-plugin.

[1] https://eclipse.org/tycho/

Upvotes: 1

Nallamachu
Nallamachu

Reputation: 1488

I hope in this website explained clearly. It will helps to you.

http://www.mkyong.com/logging/log4j-hello-world-example/

My sincere suggestion is, before implement in project try to create one sample example. If it success, you can apply same into project.

Upvotes: 0

Related Questions