1

We are using Slf4j and implementing slf4j with log4j to print the logs in console as well as in the file.The code which we are trying to printing the logs will be created as a jar and placed it in the other place in the tomcat but when running the application i am not able to see printing the logs in the console.

Please look into the follwoing configuration:

log4j.category.sample.sso.application=Debug, file, C
log4j.additivity.sample.sso.application=true

log4j.appender.C=org.apache.log4j.ConsoleAppender
log4j.appender.C.Target=System.out
log4j.appender.C.ImmediateFlush=true
log4j.appender.C.layout=org.apache.log4j.PatternLayout
log4j.appender.C.layout.ConversionPattern=%-5p %d [%t] %m%n


### direct messages to file ###
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=${catalina.home}/var/ipau/logs/ssoapplication.log
log4j.appender.file.Append=true
log4j.appender.file.MaxFileSize=10MB
# mylog.log.10 \u307e\u3067\u4fdd\u6301
log4j.appender.file.MaxBackupIndex=50
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %5p [%t] %c{1} - %m%n

log4j.rootLogger=INFO, C, file

My project structure is:

Sample-- src-- sample-- sso-- application log4j.properties.

I am preparing the jar file from the above structure and placed in the other war file in the tomcat. As suggested in the Stackover flow, i tried by reading the log4j.properties file externally which is below.

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.util.Properties;
import java.util.regex.Matcher;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.binary.Base64;
import org.apache.log4j.PropertyConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class PreProcessServlet extends HttpServlet {

    private static final long serialVersionUID = -572799841125956990L;

    PreLoginServlet() {

    }



    protected void service(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        try {
          FileInputStream fis = new FileInputStream(new File("/log4j.properties"));
//          InputStream input = this.class.getClassLoader().getResourceAsStream("log4j.properties");
          Properties prop = new Properties();
          prop.load(fis);
          PropertyConfigurator.configure(prop);
      } catch (IOException e) {
          e.printStackTrace();
          System.out.println("ERROR: Unable to load log4j.properties");
      }
        final Logger log = LoggerFactory
                .getLogger(PreProcessServlet .class); 
        log.debug("Pre login process is started");

        System.out.println("sys printing");
        Boolean parametersAreValid = true;
        RegularExpression regularExpression = RegularExpression.getInstance();


        String sessionId = request.getParameter(GlobalStrings.sessionId);
        if (null != sessionId){
            Matcher sessionIdMatcher = regularExpression.getSessionPattern().matcher(sessionId);
            if ((sessionId.equals(GlobalStrings.nullString)) ||
                    (!sessionId.equals(GlobalStrings.nullString) && sessionIdMatcher.find())){
                log.error("PreLoginServlet "+ "Invalid SessionID " + sessionId);
                parametersAreValid = false;
            }
        }else{
            log.info("sessionId field is empty");
        }

}

Update: I got the following error even i have added dependency

libs(slf4j-log4j12-1.7.5, log4j along with slf4j):
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
1
  • Please let me know if the provided info is not sufficient
    – M.S.Naidu
    May 29, 2015 at 11:15

1 Answer 1

1

This error is reported when the org.slf4j.impl.StaticLoggerBinder class could not be loaded into memory.

This happens when no appropriate SLF4J binding could be found on the class path.

Placing one (and only one) of slf4j-nop.jar, slf4j-simple.jar, slf4j-log4j12.jar, slf4j-jdk14.jar or logback-classic.jar on the class path should solve the problem.

please add slf4j-simple-1.7.jar refer this link for more information

In case you are using maven add

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.7.12</version>
</dependency>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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