0

我有一个奇怪的问题,可能很简单,但我已经用谷歌搜索了 20 分钟,但没有解决

我正在尝试在 Eclipse 中使用 log4j。我下载了最新的 zip 并添加到我的类路径中。我通过从教程中复制创建了属性文件。属性文件位于我项目的根文件夹中,名为“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
}

}
4

1 回答 1

1

You currently have PropertyConfigurator.configure in the class block so the compiler is complaining. Move the statement (along with the other non-declarative statements) into a method or static initializer block.

于 2013-03-07T18:16:09.070 回答