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

查看:830
本文介绍了使用SLF4j/log4j将日志存储到.log文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

班级:

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.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..!!

任何人都可以帮助我将日志存储到checkLog.log文件中吗??

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:

  • 检查是否可以写入C的根:-改为写入:

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

log4j.appender.file.File=checkLog.log 

登录到当前文件夹

添加控制台记录器以查看其是否可以在控制台中工作:

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.

检查所有sl4j库都在路径中-您将在类路径上需要slf4j-apislf4j-log4j12 jars

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

确保log4j.properties在类路径上

希望这会有所帮助.

这篇关于使用SLF4j/log4j将日志存储到.log文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆