java中的日志轮换,日志限制

时间:2018-01-24 13:31:48

标签: java logging java.util.logging

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

然后我想添加限制,例如我只能添加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

1 个答案:

答案 0 :(得分:1)

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