如何使用java.util.logging.filehandler将日志存储在单个日志文件中?

8xiog9wr  于 2021-06-21  发布在  Flink
关注(0)|答案(1)|浏览(431)

我想在一个日志文件中添加异常,在我的源代码中下列代码被多次使用。。。我想将所有异常存储在一个文件中,但它会创建多个文件,如exception.log、exception.log.1、exception.log.1.lck、exception.log.2等等。。。

Date dir1 = new java.util.Date(System.currentTimeMillis());
String baseDir1 = "/home/gaurav/usr/logs/ESBegin/";
String newDir1 = createDateBasedDirectory(baseDir1, dir1);
System.out.println("Exception :: " + e.getMessage());
Logger logger = Logger.getLogger("MyLog");  
FileHandler fh;  
try {  
    // This block configure the logger with handler and formatter  
    fh = new FileHandler(newDir1+"/exception.log");  
    logger.addHandler(fh);
    SimpleFormatter formatter = new SimpleFormatter();
    fh.setFormatter(formatter);  
    // the following statement is used to log any messages  
    logger.info(e.getMessage()); 
} catch (SecurityException ex) {  
    ex.printStackTrace();  
} catch (IOException ex) {  
    ex.printStackTrace();  
}
eeq64g8w

eeq64g8w1#

这是因为每次调用该方法时都会创建一个新的filehandler,而不是将其作为类示例变量。我测试了它,它工作了:

import java.io.IOException;
import java.util.Date;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class Main {

    private static String newDir1 = "C:\\Users\\pavel.orekhov\\Desktop";
    private static FileHandler fh;

    static {
        try {
            fh = new FileHandler(newDir1 + "\\exception.log", 0, 1, true);
        } catch (IOException | SecurityException e) {
        }
    }

    static void test() {
        Date dir1 = new java.util.Date(System.currentTimeMillis());
        Logger logger = Logger.getLogger("MyLog");
        logger.addHandler(fh);
        SimpleFormatter formatter = new SimpleFormatter();
        fh.setFormatter(formatter);
        logger.info("test");

    }

    public static void main(String[] args) {
        // all these write to the same file
        test();
        test();
        test();
        test();
    }
}

你的代码是这样的:

import java.io.IOException;
import java.util.Date;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class Main {

    static void test() {
        Date dir1 = new java.util.Date(System.currentTimeMillis());
        String newDir1 = "C:\\Users\\pavel.orekhov\\Desktop";
        Logger logger = Logger.getLogger("MyLog");
        FileHandler fh;
        try {
            fh = new FileHandler(newDir1 + "\\exception.log", 0, 1, true);
            logger.addHandler(fh);
            SimpleFormatter formatter = new SimpleFormatter();
            fh.setFormatter(formatter);
            logger.info("test");
        } catch (SecurityException | IOException ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        test(); // creates file 1
        test(); // creates file 2
        test(); // creates file 3
        test(); // creates file 4
    }
}

这会创建尽可能多的文件 test() 方法。

相关问题