gpt4 book ai didi

java - java中的日志轮转,日志限制

转载 作者:行者123 更新时间:2023-12-02 11:42:49 25 4
gpt4 key购买 nike

我有一个类游戏,它会添加到我的游戏的日志分数中。

然后我想添加限制,例如我只能添加 2 个日志,而不添加日志。但是我应该在 Game.class 中添加什么以使该类与文件 log.properties 进行通信?我应该使用 log4j 吗?

这是我的 Game.class:

import java.util.Random;
import java.util.logging.Logger;

public class Game {
public static final int numOfFutMarbles = 3; // przyszle kulki
private int score;

Field fieldProcessor = new Field();
Wave wave = new Wave();

Logger logger = Logger.getLogger(Game.class.getName());
Random random = new Random();

public final static int width = 9; // rozmiar planszy
public final static int height = 9;

public Bin[][] field;

public static final int not_sel = -1;
int activeBinX = not_sel;
int activeBinY = not_sel;

public Game() {
this.field = new Bin[width][height];
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
field[i][j] = new Bin(i, j);
}
}
placeMarbles(5, Marble.new_mar); // liczba kulek na poczatku
placeMarbles(3, Marble.fut_mar); // liczba przyszlych kulek

score = 0;
}

public void BinSelected(int i, int j) {
if (isBinContainsMarble(i, j)) {
activeBinchange(i, j);
}
}

boolean isBinContainsMarble(int i, int j) {
return field[i][j].isBinContainsMarble();
}

public void makeMoveOrSelect(int i, int j) {
if (field[i][j].isBinContainsMarble()) {
activeBinchange(i, j);
return;
}

if (activeBinX == not_sel) {
return;
}

if (wave.isMoveAvailable(field, activeBinX, activeBinY, i, j)) {
makeMove(i, j);
}
}

void makeMove(int x, int y) {
field[x][y].marble = field[activeBinX][activeBinY].marble;
field[x][y].setMarbleState(Marble.inac_mar);

field[activeBinX][activeBinY].marble = null;

activeBinX = not_sel;
activeBinY = not_sel;

boolean isLineRemoved = fieldProcessor.checkField(field);
if (!isLineRemoved) {
placeNewMarbles();
fieldProcessor.checkField(field);
}
calcScores();
wave.createWaveArrayFrom(field);
}

void activeBinchange(int i, int j) {
if (isActiveBinSelected()) {
field[activeBinX][activeBinY].setMarbleState(Marble.inac_mar);
}
field[i][j].setMarbleState(Marble.act_mar);
activeBinX = i;
activeBinY = j;
}

private boolean isActiveBinSelected() {
return activeBinX > not_sel;
}

void placeNewMarbles() {
int remaningFutureMarbles = calcRemaningFutureMarblesAndMakeThemNEW();
placeMarbles(numOfFutMarbles, Marble.fut_mar);
}

int calcRemaningFutureMarblesAndMakeThemNEW() {
int remainingFutureMarblesAmount = 0;
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
if (field[i][j].getMarbleState() == Marble.fut_mar) {
remainingFutureMarblesAmount++;
field[i][j].setMarbleState(Marble.new_mar);
}
}
}
return remainingFutureMarblesAmount;
}

void placeMarbles(int n, int state) {
for (int i = 0; i < n; i++) {
placeMarbleOnEmptySpace(state);
}
}

void placeMarbleOnEmptySpace(int state) { // umieszamy kulke na wolnym miejscu
boolean isPlaced = false;
do {
int i = random.nextInt(width);
int j = random.nextInt(height);

if (field[i][j].getMarbleState() == 0) {
field[i][j].marble = new Marble(state);
isPlaced = true;
}
} while (!isPlaced);
}

public void updateBins() {
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
if (field[i][j].marble != null) {
field[i][j].marble.porcessMarbles();
}
}
}
}

public int calcScores() {
int markedForDel = 0;
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
if (field[i][j].marble != null) {
if (field[i][j].marble.MarbleState == Marble.mark_for_rem) {
markedForDel++;
}
}
}
}
if (markedForDel > 0) {
int bon = (int) (4 + Math.pow(markedForDel - 4, 3)); // 6 kulek = 12 pkt, 7 kulek - 31 pkt
score += bon;
logger.info(score + "");
System.out.println("your score: " + score); // 1 pkt za kazda kulke
}
return score;
}
}

这是我的日志配置:

handlers = java.util.logging.FileHandler, java.util.logging.ConsoleHandler
.level = ALL

# File Logging
java.util.logging.FileHandler.pattern=./logs/Marbles_game.log
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.FileHandler.level=ALL
java.util.logging.FileHandler.limit=2
java.util.logging.FileHandler.count=100

# Console Logging
java.util.logging.ConsoleHandler.level = OFF

最佳答案

java.util.logging.FileHandler.append=true 添加到您的属性文件中。这会将数据追加到日志文件中,直到达到限制。

关于java - java中的日志轮转,日志限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48423727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com