Log4j PropertyConfigurator.configure()不接受eclipse中的字符串

时间:2013-03-07 18:12:58

标签: java logging log4j

我有一个奇怪的问题,可能是一些简单的事情,但我现在用谷歌搜索20分钟而没有解决方案

我正在尝试在Eclipse中使用log4j。我下载了最新的zip并添加到我的classpath中。我通过复制教程创建了属性文件。属性文件位于项目的根文件夹中,名为“log4j.properties”。该文件包含

#define the console appender
log4j.appender.consoleAppender = org.apache.log4j.ConsoleAppender

# now define the layout for the appender
log4j.appender.consoleAppender.layout = org.apache.log4j.PatternLayout
log4j.appender.consoleAppender.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

# now map our console appender as a root logger, means all log messages will go to this     appender
log4j.rootLogger = DEBUG, consoleAppender

我的类如下,eclipse在PropertyConfigurator.configure()行中有以下错误。 此行有多个标记 - 令牌“”log4j.properties“”上的语法错误,删除此令牌 - 令牌上的语法错误,错位的构造

然而,log4j的api表明propertyconfigurator应该接受一个字符串。有什么建议?   -

package org.dnsdojo.ryanhost.GA.MuPlusOne;

import java.util.Random;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;


public class Genome
{
private static Logger logger = Logger.getLogger(Genome.class);
PropertyConfigurator.configure("log4j.properties");
byte[] genome;
Random rng = new Random();
String genomeString = "";

public Genome ( int stringLength, int motorSet )
{
    genome = new byte[ stringLength * 7 * motorSet]; // stringLength depends on how many bytes you wish to have. For the arpibot this is dependant on the number of sensor readings taken
    for (int i = 0; i < genome.length; i++)
    {
        genome[i] = (byte)rng.nextInt(2);
        genomeString += genome[i];
    }
    logger.debug(genomeString);     
}

public byte[] getGenome()
{
    return genome;
}

public byte[] mutate (float mutationStep)
{
    return genome;                                  //placeholder
}

}

1 个答案:

答案 0 :(得分:1)

您目前在类块中有PropertyConfigurator.configure,因此编译器抱怨。将语句(以及其他非声明性语句)移动到方法或static初始化程序块中。