无法从jar文件打印日志

时间:2015-05-29 04:21:59

标签: tomcat log4j slf4j

我们正在使用Slf4j并使用log4j实现slf4j以在控制台和文件中打印日志。我们尝试打印日志的代码将被创建为jar并将其放置在另一个位置。 tomcat但是在运行应用程序时,我无法看到在控制台中打印日志。

请查看以下配置:

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

我的项目结构是:

Sample--     src--        样品 -           sso--             应用        log4j.properties。

我正在从上面的结构中准备jar文件并放在tomcat中的另一个war文件中。 正如Stackover流程中所建议的那样,我尝试通过外部读取log4j.properties文件,该文件位于下方。

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");
        }

}

更新:即使我添加了依赖

,我也收到了以下错误
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 个答案:

答案 0 :(得分:0)

当无法将org.slf4j.impl.StaticLoggerBinder类加载到内存中时会报告此错误。

如果在类路径上找不到合适的SLF4J绑定,则会发生这种情况。

在类路径上放置一个(且只有一个)slf4j-nop.jar, slf4j-simple.jar, slf4j-log4j12.jar, slf4j-jdk14.jar or logback-classic.jar应该可以解决问题。

请添加slf4j-simple-1.7.jar,请参阅此link以获取更多信息

  

如果您使用maven add

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