Java Log Rotate rotateLog()

Here you can find the source of rotateLog()

Description

rotate Log

License

Open Source License

Declaration

public static void rotateLog() 

Method Source Code

//package com.java2s;
/*/*ww w .jav a 2 s  .  c  o m*/
 * This file is part of the Origin-World game client.
 * Copyright (C) 2013 Arkadiy Fattakhov <ark@ark.su>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;

public class Main {
    public static void rotateLog() {
        int maxLogs = 2;

        File last = new File("client-" + maxLogs + ".log");
        if (last.exists()) {
            if (!last.delete()) {
                System.err.println("Error removing old log file:" + last);
            }
        }
        for (int i = maxLogs - 1; i > 0; i--) {
            File current = new File("client-" + i + ".log");
            if (current.exists()) {
                if (!current.renameTo(last)) {
                    System.err.println("Error renaming:" + current + " to:"
                            + last);
                }
            }
            last = current;
        }

        File current = new File("client.log");
        if (current.exists()) {
            if (!current.renameTo(last)) {
                System.err.println("Error renaming:" + current + " to:"
                        + last);
            }
        }
    }
}

Related

  1. rotateLogs(File where, String basename, int max)