使用SLF4j/log4j将日志存储到.log文件中[英] Storing log into .log file using SLF4j/log4j

本文是小编为大家收集整理的关于使用SLF4j/log4j将日志存储到.log文件中的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在使用SLF4J,根据需要,我必须将日志存储到.log文件中.但是,当我运行程序时,日志未写入thelog文件.

类:

import org.slf4j.Logger;   
import org.slf4j.LoggerFactory;   


public class TestSLF4J {   

//  private static Logger _logger = LoggerFactory.getLogger(TestSLF4J.class);   
    private static Logger _logger = LoggerFactory.getLogger(TestSLF4J.class);   



    public static void main(String[] args) {   
        logger .debug("Sample debug message"); 
logger .info("Sample info message"); 
logger .warn("Sample warn message"); 
logger .error("Sample error message");   
    }   
}   

log4j.properties

log4j.appender.file=org.apache.log4j.RollingFileAppender   
log4j.appender.file.maxFileSize=100KB   
log4j.appender.file.maxBackupIndex=5  
log4j.appender.file.File=C:/checkLog.log   
log4j.appender.file.threshold=DEBUG
log4j.appender.file.layout=org.apache.log4j.PatternLayout   
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n   
log4j.rootLogger=DEBUG,file

我可以看到信息,警告,在控制台上的错误,但没有调试值.!!

任何人都可以帮助我将登录存储到Checklog.log文件中.??

推荐答案

我刚刚尝试了您给出的示例,对我来说很好.我会检查/尝试几件事:

  • 检查是否可以写入C的根: - 代替写:

    log4j.appender.file.File=checkLog.log 
    

    登录当前文件夹

  • 添加控制台记录器,以查看它是否在控制台中工作:

    log4j.appender.console=org.apache.log4j.ConsoleAppender
    log4j.appender.console.layout=org.apache.log4j.PatternLayout   
    log4j.appender.console.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
    
    log4j.appender.file=org.apache.log4j.RollingFileAppender   
    log4j.appender.file.maxFileSize=100KB   
    log4j.appender.file.maxBackupIndex=5  
    log4j.appender.file.File=checkLog.log   
    log4j.appender.file.threshold=DEBUG
    log4j.appender.file.layout=org.apache.log4j.PatternLayout   
    log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
    
    log4j.rootLogger=DEBUG,console,file
    

    运行应用程序时,应该在控制台和文件中看到登录.

  • 检查所有SL4J库是否都在路径中 - 您需要slf4j-api和slf4j-log4j12 jars classPath上的jars

  • 确保log4j.properties在classPath上

希望这会有所帮助.

其他推荐答案

确保仅使用一个记录框架具有绑定.如果您有一个以上的记录框架罐,则可能 请参阅输出.

我也有类似的问题,然后发现,班级路径具有slf4j-simple-1.7.5.jar以及log4j.jar.因此,日志输出仅在控制台上写入.从班级路径中删除第一条帮助.

Also check console, you should be getting a warning/error message.

本文地址:https://www.itbaoku.cn/post/1574761.html

问题描述

I am using SLF4J and as per requirement i have to store the logs into the .log file. But when i run the program the log are not written into thelog file.

Class :

import org.slf4j.Logger;   
import org.slf4j.LoggerFactory;   


public class TestSLF4J {   

//  private static Logger _logger = LoggerFactory.getLogger(TestSLF4J.class);   
    private static Logger _logger = LoggerFactory.getLogger(TestSLF4J.class);   



    public static void main(String[] args) {   
        logger .debug("Sample debug message"); 
logger .info("Sample info message"); 
logger .warn("Sample warn message"); 
logger .error("Sample error message");   
    }   
}   

log4j.properties

log4j.appender.file=org.apache.log4j.RollingFileAppender   
log4j.appender.file.maxFileSize=100KB   
log4j.appender.file.maxBackupIndex=5  
log4j.appender.file.File=C:/checkLog.log   
log4j.appender.file.threshold=DEBUG
log4j.appender.file.layout=org.apache.log4j.PatternLayout   
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n   
log4j.rootLogger=DEBUG,file

i can see info,warn,error on console but not debug value..!!

Can anyone help me store log into the checkLog.log file.??

推荐答案

I just tried the example you gave and it worked fine for me. There are several things that I'd check/try:

  • Check if you can write to the root of C: - write this instead:

    log4j.appender.file.File=checkLog.log 
    

    to log to the current folder

  • Add a console logger to see whether it works in console:

    log4j.appender.console=org.apache.log4j.ConsoleAppender
    log4j.appender.console.layout=org.apache.log4j.PatternLayout   
    log4j.appender.console.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
    
    log4j.appender.file=org.apache.log4j.RollingFileAppender   
    log4j.appender.file.maxFileSize=100KB   
    log4j.appender.file.maxBackupIndex=5  
    log4j.appender.file.File=checkLog.log   
    log4j.appender.file.threshold=DEBUG
    log4j.appender.file.layout=org.apache.log4j.PatternLayout   
    log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
    
    log4j.rootLogger=DEBUG,console,file
    

    When you run the application, you should see logging in the console and in the file.

  • Check that all sl4j libraries are in the path - you will need slf4j-api and slf4j-log4j12 jars on your classpath

  • Ensure that log4j.properties is on the classpath

Hope this helps.

其他推荐答案

Make sure that you have binding with only one logging framework. If you have more than one logging framework jar then you might NOT see output.

I had similar problem then found that, class path had slf4j-simple-1.7.5.jar as well as log4j.jar. So log output was getting written only on console. Removing first one from class path helped.

Also check console, you should be getting a warning/error message.