我已经设置了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 #define 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
基因组课程
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);
}
}
答案 0 :(得分:1)
此Javadoc page声明了模式%-4r
r - 用于输出从中经过的毫秒数 构建布局直到创建日志记录事件。
%-4
说使用4个空格(填充)来写r
所需的信息。所以我猜测的是你的日志一个接一个地快速到达它们只有1-2毫秒(日志声明中的第一个数字)。我们还可以推断,当你真正开始记录事物时,模式布局是懒惰的。
答案 1 :(得分:0)
这与记录器无关(我已经使用log4j 15年了)。
它似乎正在完全记录发送给它的String。
再次查看代码并与预期进行比较。
Random
的{{1}}生成器正在寻找0-1的值...
因此记录了一个0和1的字符串。
nextInt(2)