Log4j:Logs are displying on console but File is no

2019-08-03 10:42发布

Here i am using log4j API in standalone application in Netbeans.Logs are getting printed on console but NOT in File.Log4j file is present in the source folder .Please help me! Below is the my log4j.properties file

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.file.layout.ConversionPattern=%d %5p %c{1}\:%L - %m%n
log4j.appender.file.File=E:\\Final\\Testing123.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.DatePattern`enter code here`='.'yyyy-MM-dd
log4j.logger.testing=DEBUG, stdout, file
log4j.appender.stdout.layout.ConversionPattern=%d %5p %c{1}\:%L - %m%n
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.Target=System.out

and below is the class where i am using it.

public class Test {
     protected static  Logger logger = LoggerFactory.getLogger(Testing.class.getName());   

     public static void main(String[] args) {

         logger.debug("Test Log1");
         Test t = new Test();
         logger.debug("Test Log2");
         //t.setLogPropertyFile();
         logger.debug("Test Log3");
         t.testthis();
         logger.debug("Test Log4");
    }

    public void testthis(){
         this.logger.debug("Test Log");
         this.logger.info("Test Log");
         this.logger.warn("Test Log");
         this.logger.error("Test Log");
    }

标签: log4j
1条回答
Anthone
2楼-- · 2019-08-03 10:48

It looks like your problem is here:

log4j.appender.file.DatePattern`enter code here`='.'yyyy-MM-dd

Probably a copy/paste thing, but it should look like this:

log4j.appender.file.DatePattern='.'yyyy-MM-dd

Also, its a lot easier to see what is going on if you organize the config file:

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %5p %c{1}\:%L - %m%n

log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=E:\\Final\\Testing123.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %5p %c{1}\:%L - %m%n
log4j.appender.file.DatePattern='.'yyyy-MM-dd

log4j.logger.testing=DEBUG, stdout, file

---- Edit ----

If running as a standalone java application, you will need to Configure your log4j framework at startup. You can find information about this in the Configuration section of the documentation. Specifically, since you are trying to use a properties file, you will need to do something like this (borrowed directly from the documentation):

 import com.foo.Bar;

 import org.apache.log4j.Logger;
 import org.apache.log4j.PropertyConfigurator;

 public class MyApp {

   static Logger logger = Logger.getLogger(MyApp.class.getName());

   public static void main(String[] args) {


     // BasicConfigurator replaced with PropertyConfigurator.
     PropertyConfigurator.configure(args[0]);

     logger.info("Entering application.");
     Bar bar = new Bar();
     bar.doIt();
     logger.info("Exiting application.");
   }
 }
查看更多
登录 后发表回答