0

我已经设置了 log4j,但我注意到这样的奇怪输出(我希望看到 0 1 2 3 4 作为行号) -

0    [main] DEBUG org.dnsdojo.ryanhost.GA.MuPlusOne.Genome  - 0011110101101010110110111011101111110001010001110011100011110111011001011100100100101001100110100000011100010100
2    [main] DEBUG org.dnsdojo.ryanhost.GA.MuPlusOne.Genome  - 112
2    [main] DEBUG org.dnsdojo.ryanhost.GA.MuPlusOne.Genome  - 0010100111111010110110010001000100000110110111111011011100101010011100011001111111111011110011001111010100100101
3    [main] DEBUG org.dnsdojo.ryanhost.GA.MuPlusOne.Genome  - 112
3    [main] DEBUG org.dnsdojo.ryanhost.GA.MuPlusOne.Genome  - 0101101000001010000001101000101001001000111111111110001010011001111011010101011010010000111000100011110010101100
3    [main] DEBUG org.dnsdojo.ryanhost.GA.MuPlusOne.Genome  - 112

谁能解释为什么会发生这种情况?log4j.properties #定义控制台追加器 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

基因组类

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);
byte[] genome;
Random rng = new Random();
String genomeString = "";

public Genome ( int stringLength, int motorSet )
{
    PropertyConfigurator.configure("log4j.properties");
    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);
    logger.debug(genomeString.length());
}

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

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

}

司机班

package org.dnsdojo.ryanhost.GA.MuPlusOne;

import java.util.ArrayList;
public class Driver 
{
int populationSize = 0; //mu
int evaluationTime = 0; //tau measured in seconds
double reevaluationRate, mutationStep = 0;//rho = reevaluationRate, sigma = mutationStep
int numberOfSensors, numberOfMotors = 0;

ArrayList<Genome> population = new ArrayList<Genome>();

public Driver (int populationSize, int evaluationTime, double reevaluationRate, double mutationStep, int numberOfSensors, int numberOfMotors)
{
    this.populationSize = populationSize;
    this.evaluationTime = evaluationTime;
    this.reevaluationRate = reevaluationRate;
    this.mutationStep = mutationStep;
    this.numberOfSensors = numberOfSensors;
    this.numberOfMotors = numberOfMotors;

    for(int i = 0; i < populationSize; i++)
    {
        population.add (new Genome (numberOfSensors , numberOfMotors) );
    }
}

public static void main( String[] args)
{
    Driver gADriver = new Driver(100, 60 , 0.2, 0.2, 8, 2);
}

}
4

2 回答 2

1

Javadoc 页面为 Pattern 说明了以下内容%-4r

r - 用于输出从构建布局到创建日志事件所经过的毫秒数。

%-4就是说使用 4 个空格(填充)来写入r. 所以我猜你的日志一个接一个地来的太快了,它们之间只有 1-2 毫秒的间隔(你的日志语句中的第一个数字)。我们还可以推断,当您实际开始记录事物时,Pattern 布局是延迟构建的。

于 2013-03-07T18:51:27.597 回答
0

这与记录器无关(我使用 log4j 已经 15 年了)。它似乎正在准确记录发送给它的字符串。再次查看代码并与预期的内容进行比较。for的Random生成器nextInt(2)正在寻找 0-1 的值...因此记录了 0 和 1 的字符串。

for (int i = 0; i < genome.length; i++)
{
    genome[i] = (byte)rng.nextInt(2);
    genomeString += genome[i];
}
logger.debug(genomeString);
logger.debug(genomeString.length());
于 2013-03-07T18:55:25.860 回答