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 :
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
Added this dependency in pom.xml
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
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?
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/
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.
Related
I a new user of Eclipse. I put log4j.properties into src-folder. For some reason my src-folder has "default package" and I don't know how to put log4j.properties inside of it.
As a consiquence, I have this kind of exception:
log4j:ERROR Could not read configuration file [log4j.properties].
java.io.FileNotFoundException: log4j.properties (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:372)
at org.apache.log4j.PropertyConfigurator.configure(PropertyConfigurator.java:403)
at HelloWorld.main(HelloWorld.java:9)
log4j:ERROR Ignoring configuration file [log4j.properties].
Moving log4j.properties into src/main/resources does not solve the prolem.
Here is my code:
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class HelloWorld {
static final Logger logger = Logger.getLogger(HelloWorld.class);
public static void main(String[] args) {
PropertyConfigurator.configure("log4j.properties");
logger.debug("Sample debug message");
logger.info("Sample info message");
logger.warn("Sample warn message");
logger.error("Sample error message");
logger.fatal("Sample fatal message");
}
}
log4j.properties file:
# Root logger option
log4j.rootLogger=INFO, file, errorfile
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
#Redirect to Tomcat logs folder
#log4j.appender.file.File=${catalina.home}/logs/logging.log
log4j.appender.file.File=C:\\log\\logging.log
log4j.appender.file.MaxFileSize=10MB
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
log4j.appender.errorfile=org.apache.log4j.RollingFileAppender
log4j.appender.errorfile.Threshold=ERROR
log4j.appender.errorfile.File=C:\\log\\errorlogging.log
log4j.appender.errorfile.MaxFileSize=10MB
log4j.appender.errorfile.MaxBackupIndex=10
log4j.appender.errorfile.layout=org.apache.log4j.PatternLayout
log4j.appender.errorfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
How to solve this problem?
You have to add the log4j.properties file in the following location.
For main code base -> src/main/resources
For test code base -> src/test/resources
Besides, if you are using Maven, add the below code in pom.xml in the build section.
<resources>
<resource>
<directory>src/main/resources</directory>
<targetPath>${project.build.directory}</targetPath>
<includes>
<include>log4j.properties</include>
</includes>
</resource>
</resources>
After doing all the above, refresh the project in Eclipse. Build the maven project with the command mvn clean package or mvn clean install. Inside the project, you should be able to see the log4j.properties file inside build/classes directory.
O must put on src/main/resources folder.
https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
I migrated our spring boot application from the web.xml configuration file to java based configuration. In this application we use log4j2 for logging however we also have log4j because of some surrounding system which uses it, so we can not remove it.
After finishing with all the migration the logs were not working. I figured out that I have to add the following code into my application.properties file
logging.level.project.base.package=TRACE
logging.level.org.springframework=INFO
logging.level.org.hibernate=INFO
Adding all these made logs to appear, however only the property file log4j.properties is recognized and the logs which are using log4j2 have the default logging pattern.
If I remove the log4j.properties file the log4j2 still doesn't work, moreover I get the following warning message
log4j:WARN No appenders could be found for logger (org.springframework.web.context.support.StandardServletEnvironment).
log4j:WARN Please initialize the log4j system properly.
I also tried to specify the config file path in application.properties in bought of the following ways
logging.config=application.properties
logging.config=classpath:application.properties
Our pom.xml dependencies are the following
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
We are not running using an embedded tomcat, so we have a config class that extends SpringBootServletInitializer. We use the 2.0.1.RELEASE Spring Boot Version.
Bellow you can find my 2 log4j config files
log4j.properties
conversionPatternShort=OLDLOGGGGER %5p %d %-30C{1}:%4L [%X{username}] - %m%n
### APPENDER
log4j.appender.crt=org.apache.log4j.ConsoleAppender
log4j.appender.crt.layout=org.apache.log4j.PatternLayout
log4j.appender.crt.layout.ConversionPattern=${conversionPatternShort}
### Define Levels per package
log4j.rootLogger=ERROR,crt
log4j.logger.some.package1=DEBUG,crt
log4j.logger.some.package2=DEBUG,crt
log4j2.properties
status=ERROR
dest=err
name=LocalConfig
### PATTERN
property.conversionPatternShort=%5p %d %-30C{1}:%4L [%X{username}] - %m%n
### APPENDER
appenders=crt
appender.crt.type=Console
appender.crt.name=crt
appender.crt.layout.type=PatternLayout
appender.crt.layout.pattern=${conversionPatternShort1}
## LOGGER
## defining some loggers, foreach logger I have defined
## (name, level, additivity, appenderRef.crt.ref)
logger.xxxxx.name=namex
logger.xxxxx.level=DEBUG
logger.xxxxx.additivity=false
logger.xxxxx.appenderRef.crt.ref=crt
logger.yyyyy.name=namey
logger.yyyyy.level=DEBUG
logger.yyyyy.additivity=false
logger.yyyyy.appenderRef.crt.ref=crt
logger.hibernateUtil.name=org.hibernate.engine.jdbc.spi.SqlExceptionHelper
logger.hibernateUtil.level=ERROR
logger.hibernateUtil.additivity=false
logger.hibernateUtil.appenderRef.crt.ref=crt
rootLogger.level=WARN
rootLogger.appenderRef.crt.ref=crt
I really can not find what change made the log4j2 not work anymore. Could you please help?
I'm trying to train with ClearParser and I get this error. Before execute the command I put export CLASSPATH=nlp4j-1.1.0.jar:. and doing java edu.emory.mathcs.nlp.bin.Version I get the version info, so it's installed correctly.
Command line: java -Xmx5g -XX:+UseConcMarkSweepGC edu.emory.mathcs.nlp.bin.NLPTrain -mode dep -c config-train-dep.xml -t /home/iago/Escritorio/idiomasClearParser/UD_English/en-ud-train.conllu -d /home/iago/Escritorio/idiomasClearParser/UD_English/en-ud-dev.conllu -m bestModel-dep.xz
I'm using this config file: https://github.com/emorynlp/nlp4j/blob/master/src/main/resources/edu/emory/mathcs/nlp/configuration/config-train-dep.xml
Error: log4j:WARN No appenders could be found for logger (edu.emory.mathcs.nlp.common.util.BinUtils). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. java.io.FileNotFoundException: edu/emory/mathcs/nlp/lexica/en-brown-clusters-simplified-lowercase.xz (No existe el archivo o el directorio) at java.io.FileInputStream.open0(Native Method) at java.io.FileInputStream.open(FileInputStream.java:195) at java.io.FileInputStream.<init>(FileInputStream.java:138) at java.io.FileInputStream.<init>(FileInputStream.java:93) at edu.emory.mathcs.nlp.common.util.IOUtils.createFileInputStream(IOUtils.java:147) at edu.emory.mathcs.nlp.common.util.IOUtils.getInputStream(IOUtils.java:316) at edu.emory.mathcs.nlp.component.template.util.GlobalLexica.getLexiconFieldPair(GlobalLexica.java:82) at edu.emory.mathcs.nlp.component.template.util.GlobalLexica.getLexiconFieldPair(GlobalLexica.java:72) at edu.emory.mathcs.nlp.component.template.util.GlobalLexica.<init>(GlobalLexica.java:64) at edu.emory.mathcs.nlp.component.template.util.GlobalLexica.<init>(GlobalLexica.java:55) at edu.emory.mathcs.nlp.bin.NLPTrain$1.createGlobalLexica(NLPTrain.java:108) at edu.emory.mathcs.nlp.component.template.train.OnlineTrainer.train(OnlineTrainer.java:193) at edu.emory.mathcs.nlp.component.template.train.OnlineTrainer.train(OnlineTrainer.java:187) at edu.emory.mathcs.nlp.bin.NLPTrain.train(NLPTrain.java:76) at edu.emory.mathcs.nlp.bin.NLPTrain.main(NLPTrain.java:115) java.io.IOException: Stream closed at java.io.BufferedInputStream.getInIfOpen(BufferedInputStream.java:159) at java.io.BufferedInputStream.fill(BufferedInputStream.java:246) at java.io.BufferedInputStream.read1(BufferedInputStream.java:286) at java.io.BufferedInputStream.read(BufferedInputStream.java:345) at java.io.DataInputStream.readFully(DataInputStream.java:195) at java.io.DataInputStream.readFully(DataInputStream.java:169) at org.tukaani.xz.SingleXZInputStream.initialize(Unknown Source) at org.tukaani.xz.SingleXZInputStream.<init>(Unknown Source) at org.tukaani.xz.XZInputStream.<init>(Unknown Source) at org.tukaani.xz.XZInputStream.<init>(Unknown Source) at edu.emory.mathcs.nlp.common.util.IOUtils.createXZBufferedInputStream(IOUtils.java:220) at edu.emory.mathcs.nlp.common.util.IOUtils.createObjectXZBufferedInputStream(IOUtils.java:259) at edu.emory.mathcs.nlp.component.template.util.GlobalLexica.getLexiconFieldPair(GlobalLexica.java:82) at edu.emory.mathcs.nlp.component.template.util.GlobalLexica.getLexiconFieldPair(GlobalLexica.java:72) at edu.emory.mathcs.nlp.component.template.util.GlobalLexica.<init>(GlobalLexica.java:64) at edu.emory.mathcs.nlp.component.template.util.GlobalLexica.<init>(GlobalLexica.java:55) at edu.emory.mathcs.nlp.bin.NLPTrain$1.createGlobalLexica(NLPTrain.java:108) at edu.emory.mathcs.nlp.component.template.train.OnlineTrainer.train(OnlineTrainer.java:193) at edu.emory.mathcs.nlp.component.template.train.OnlineTrainer.train(OnlineTrainer.java:187) at edu.emory.mathcs.nlp.bin.NLPTrain.train(NLPTrain.java:76) at edu.emory.mathcs.nlp.bin.NLPTrain.main(NLPTrain.java:115) Exception in thread "main" java.lang.NullPointerException at java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2338) at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2351) at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2822) at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:804) at java.io.ObjectInputStream.<init>(ObjectInputStream.java:301) at edu.emory.mathcs.nlp.common.util.IOUtils.createObjectXZBufferedInputStream(IOUtils.java:259) at edu.emory.mathcs.nlp.component.template.util.GlobalLexica.getLexiconFieldPair(GlobalLexica.java:82) at edu.emory.mathcs.nlp.component.template.util.GlobalLexica.getLexiconFieldPair(GlobalLexica.java:72) at edu.emory.mathcs.nlp.component.template.util.GlobalLexica.<init>(GlobalLexica.java:64) at edu.emory.mathcs.nlp.component.template.util.GlobalLexica.<init>(GlobalLexica.java:55) at edu.emory.mathcs.nlp.bin.NLPTrain$1.createGlobalLexica(NLPTrain.java:108) at edu.emory.mathcs.nlp.component.template.train.OnlineTrainer.train(OnlineTrainer.java:193) at edu.emory.mathcs.nlp.component.template.train.OnlineTrainer.train(OnlineTrainer.java:187) at edu.emory.mathcs.nlp.bin.NLPTrain.train(NLPTrain.java:76) at edu.emory.mathcs.nlp.bin.NLPTrain.main(NLPTrain.java:115)
Why I'm getting this error? I unpacked the .jar and there is no "lexica" folder neither "en-brown-clusters-simplified-lowercase.xz". Where I can found it?
Regards
I found the solution, this error happens because you don't have the "log4j.properties" configured so "nlp4j" can't find it. To fix it only create the file on the same folder that the .jar with this simple code (if you want more details adapt it to your needs)
# Root logger option
log4j.rootLogger=INFO, file, stdout
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File= /path/to/file
log4j.appender.file.MaxFileSize=5MB #Set what you need
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
# Direct log messages to stdout
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
Moreover, to solve the problem of lexica. Go to this url and download the jars lexica url
Then, on the config xml set the correct path to the jars.
And now it should works. Hope it helps someone.
In my pom.xml, I've added a dependency for using Log4j
pom.xml
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.9</version>
</dependency>
Then, I executed the following command
mvn clean install
After this command, I notice that the log4j-1.2.9.jar is well added in my lib directory itself in the target directory
Then, I started the google app engine server with the following command in my project :
appengine:devserver
And my log4j.properties in src/main/resources
# Direct log messages to stdout
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{ABSOLUTE} %5p %c{1}:%L - %m%n
After, by opening the jar file in winzip, I noticed that the Logger.class was in org/apache/log4j directory, so, when I tried to import the Logger class in a java file, it didn't detect the package 'log4j'.
Do you have any solutions ?
Thank you
I am using Struts2,hibernate web application. On that file i used following dependency for log4j in pom.xml
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.9</version>
</dependency>
In my Actionclass i used the following code to write log file:
public class loginAction extends action{
static Logger log = Logger.getLogger(com.action.LoginAction.class);
public String checklogin(){
log.debug("Debug Message(LOGIN)!");
log.info("Info Message(LOGIN)!");
log.warn("Warn Message(LOGIN)!");
log.error("Error Message(LOGIN)!");
log.fatal("Fatal Message(LOGIN)!");
//my coding for checking logged status.
}
}
I have placed log4j.properties file under WEB-INF/classes folder with the following code,
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C\:\\logfile.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
# Root logger option
log4j.rootLogger=debug, file
When i run and deploy this application on server the property file is placed in correct path. but after running statement in action class nothing get affect from log4j.properties file. I dont know where i did wrong.
So anyone please help me to find this issue. Thanks in Advance.