我已经设置了 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.属性 #定义控制台附加器 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);
}
}
最佳答案
这个Javadoc page模式 %-4r
r - Used to output the number of milliseconds elapsed from the construction of the layout until the creation of the logging event.
%-4
表示使用 4 个空格(填充)来写入 r
所需的信息。所以我猜测你的日志一个接一个地出现,速度如此之快,以至于它们之间仅相隔 1-2 毫秒(日志语句中的第一个数字)。我们还可以推断,当您实际开始记录内容时,模式布局是延迟构建的。
关于java - log4J 输出中跳过和重复的行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15279230/