Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing log into .log file using SLF4j/log4j

Tags:

slf4j

log4j

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

like image 398
Arvind Avatar asked Oct 19 '10 10:10

Arvind


2 Answers

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.

like image 67
icyrock.com Avatar answered Oct 04 '22 09:10

icyrock.com


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.

like image 26
rai.skumar Avatar answered Oct 04 '22 09:10

rai.skumar