Log4j:控制台上显示日志,但文件未更新
这里我在Netbeans的独立应用程序中使用log4j API.日志在控制台上打印出来,但在File.Log4j文件不在源文件夹中.请帮助我! 下面是我的log4j.properties文件
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.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
----编辑----
如果作为独立的Java应用程序运行,则需要在启动时配置log4j框架.您可以在文档.具体来说,由于您尝试使用属性文件,因此需要执行以下操作(直接从文档中借来的):
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.");
}
}