log4j没有写入文件

时间:2015-10-03 06:10:52

标签: java log4j

已在指定位置创建日志文件,但未在文件中输入内容。它总是空的。

import org.apache.log4j.Logger;
 public class LogFile {
public static void log(String msg){
    System.out.println("logfileee==="+msg);
    Logger logger = Logger.getLogger(LogFile.class);
    logger.info(msg);
}
 }


 log4j.rootLogger=INFO, R
 log4j.appender.A1=org.apache.log4j.ConsoleAppender
 log4j.appender.A1.layout=org.apache.log4j.PatternLayout
 log4j.appender.A1.layout.ConversionPattern=%d{HH:mm:ss,SSS} %-5p [%c] - %m%n
 log4j.appender.R = org.apache.log4j.DailyRollingFileAppender
 log4j.appender.R.Append = true
 log4j.appender.R.DatePatter    n = '.'yyy-MM-dd
 log4j.appender.R.File = D:/logs/testing.log
 log4j.appender.R.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n
 log4j.category.DataNucleus.JDO=WARN, A1
 log4j.category.DataNucleus.Persistence=WARN, A1
 log4j.category.DataNucleus.Connection=WARN, A1
 log4j.category.DataNucleus.Cache=WARN, A1
 log4j.category.DataNucleus.MetaData=WARN, A1
 log4j.category.DataNucleus.General=WARN, A1
 log4j.category.DataNucleus.Utility=WARN, A1
 log4j.category.DataNucleus.Transaction=WARN, A1
 log4j.category.DataNucleus.Datastore=WARN, A1
 log4j.category.DataNucleus.ClassLoading=WARN, A1
 log4j.category.DataNucleus.Plugin=WARN, A1
 log4j.category.DataNucleus.ValueGeneration=WARN, A1
 log4j.category.DataNucleus.Enhancer=WARN, A1
 log4j.category.DataNucleus.SchemaTool=WARN, A1
 log4j.category.DataNucleus.Query=WARN, A1
 log4j.category.DataNucleus.Reachability=WARN, A1
 log4j.category.DataNucleus.Lifecycle=WARN, A1
 log4j.logger.org.springframework=INFO, A1

我获取在指定位置创建的文件testing.log 有谁知道我做错了什么????

1 个答案:

答案 0 :(得分:0)

您的属性文件包含太多条目。我建议创建一个只有两个appender的干净文件:文件和控制台,看看发生了什么。尝试关闭文件appender,看看是否在控制台上打印了消息。并进一步打开文件appender。确保属性文件位于类路径中。

这是我尝试过的,并且正在为控制台和日志文件appender工作。

Log4j属性文件:

# Root logger option
log4j.rootLogger=INFO, file, stdout

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/tmp/logging.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Direct log messages to stdout
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{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

示例程序:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

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

public class Log4JTest {
static final String LOG_PROPERTIES_FILE = "config/log4J.properties";
private final static Logger LOGGER = Logger.getLogger(Log4JTest.class);

public static void main(String[] args)
  {
    // call our constructor
    Log4JTest log4JTest = new Log4JTest();
    log4JTest.initializeLogger();

    // Log4J is now loaded; try it
    LOGGER.info("leaving the main method of Log4JDemo");
  }

private void initializeLogger() {
    Properties logProperties = new Properties();

    try {
        ClassLoader classLoader = getClass().getClassLoader();
        File file = new File(classLoader.getResource(LOG_PROPERTIES_FILE).getFile());

        // load our log4j properties / configuration file
        logProperties.load(new FileInputStream(file));
        PropertyConfigurator.configure(logProperties);
        LOGGER.info("Logging initialized.");
    } catch (IOException e) {
        throw new RuntimeException("Unable to load logging property "
                + LOG_PROPERTIES_FILE);
    }
}

}

Pom文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mysample</groupId>
<artifactId>java-samples</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>java-samples</name>
<description>java-samples</description>

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

<dependencies>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
</dependencies>