2

I have trouble creating the log file

I added in my pom.xml

<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.16</version>
</dependency>

then I placed log4j.properies under resources folder

# Define the root logger with appender file
log = /home/soumya/log4j
log4j.rootLogger = DEBUG, FILE

# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=${log}/log.out

# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n

Then in My java code I use

public class Test
{
static Logger logger = Logger.getLogger(Test.class.getName());
 public void testLog(){
 logger.info("testing ..!!!");
}

But when running I got below error and the log file is not created !!!

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation

My maven version is Apache Maven 2.2.1 (rdebian-6) and I am using ecclipse Indigo Appreciate any help

2 Answers 2

2

SLF4j library needs to be included manually in your pom.xml

        <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>1.6.1</version>
        </dependency>
        <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>jcl-over-slf4j</artifactId>
                <version>1.6.1</version>
        </dependency>
        <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>1.6.1</version>
        </dependency>
9
  • what's the jcl-over-slf4j for? If I don't include it, what's the result?
    – Drogba
    Nov 9, 2012 at 4:59
  • Some other third party libraries may use JCL (legacy code). You can exclude it. Just that if some third party library is not producing the logs, it may use JLC. Nov 9, 2012 at 5:05
  • By including these dependencies the error has gone .But still no log files are created !!!.What should I do to get the log messages get logged into the file .I tried manually adding log4j in my build path but it also didn't worked :( Nov 9, 2012 at 5:06
  • Make sure that the method testLog() is executed (use system.out.println to be sure). Set log4j.appender.FILE.File to use the full path /home/soumya/log4j/log.out The file may be created but in different folder. Nov 9, 2012 at 5:12
  • i am sure testLog() is executing I double checked it by using system.out.println . Edited log4j.properties to redirect logs to cosole also by adding log4j.rootLogger =DEBUG, S, R, FILE and use full path in g4j.appender.FILE . But no hope . Since the logs are not logged into console I might be missing some configurations Nov 9, 2012 at 5:31
0

so, I did have like this

import dependency in pom.xml

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>log4j-over-slf4j</artifactId>
        <version>1.7.30</version>
    </dependency>

after I import in class.java, have like this

 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;

After I did like this create the variable in class and use in your code

final Logger logger = (Logger) LogManager.getLogger(TechLearningv3Application.class);

logger.info("Clientes Encontrados com função findAll():");

Print Screen this project here enter image description here

Reference - https://www.sentinelone.com/blog/maven-log4j2-project/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.