e401. limiting the size of a log file.txt

来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 18 行

TXT
18
字号
By default, a log file can grow without bound. It is possible to limit the size of the file by specifying the desired limit when creating a file handler. When a record is about to be logged and the file size is greater than the limit, the file is emptied before the record is logged. This example creates a file handler with a limit. 
See also e402 Limiting the Size of a Log by Using a Rotating Sequence of Files. 

    try {
        // Create a file handler with a limit of 1 megabytes
        String pattern = "my.log";
        int limit = 1000000; // 1 Mb
        FileHandler fh = new FileHandler("my.log", limit, 1);
    
        // Add to logger
        Logger logger = Logger.getLogger("com.mycompany");
        logger.addHandler(fh);
    } catch (IOException e) {
    }

 Related Examples 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?