通过多个类进行 Java 日志记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20737880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:57:40  来源:igfitidea点击:

Java logging through multiple classes

javaloggingjava.util.logging

提问by Jozsef Garab

I would like to log in my application which consist of several classes. I would like to have one .txt log file at the end. Therefore I make one static logger instance and I made a FileHandler for it in one class. Because I would like to have one file, I set the second argument for true in the FileHandler to be able to append the log file during logging.

我想登录我的应用程序,它由几个类组成。我希望最后有一个 .txt 日志文件。因此,我创建了一个静态记录器实例,并在一个类中为它创建了一个 FileHandler。因为我想要一个文件,所以我在 FileHandler 中将第二个参数设置为 true 以便能够在记录期间附加日志文件。

public class MyLogging {
    static Logger logger;
    public Handler fileHandler;
    Formatter plainText;

    public MyLogging() throws IOException{
        //instance the logger
        logger = Logger.getLogger(MyLogging.class.getName());
        //instance the filehandler
        fileHandler = new FileHandler("myLog.txt",true);
        //instance formatter, set formatting, and handler
        plainText = new SimpleFormatter();
        fileHandler.setFormatter(plainText);
        logger.addHandler(fileHandler);

    }

After that, I created the other loggers. I know that I have to instance one logger per class. Therefore I only make the logger (w/o FileHandler) for each class. But all of the loggers referencing for one class (not for the class, where I have created the logger). E.g:

之后,我创建了其他记录器。我知道我必须为每个类实例一个记录器。因此,我只为每个类制作记录器(不带 FileHandler)。但是所有记录器都引用了一个类(不是我创建记录器的那个类)。例如:

public class Test1 {
    static Logger logger;

    public Test1()throws IOException {

        logger = Logger.getLogger(MyLogging.class.getName());
    }

Although the logging was performed, I am not sure that this is the right solution. Can you give me some advises how to make logging through multiple the classes with the java.util.logging?

尽管执行了日志记录,但我不确定这是正确的解决方案。你能给我一些建议如何使用 java.util.logging 通过多个类进行日志记录吗?

采纳答案by Infinite Recursion

In MyLogging class, make the constructor privateinstead of public, and you need the following methods:

在 MyLogging 类中,创建构造函数private而不是public,并且您需要以下方法:

private static Logger getLogger(){
    if(logger == null){
        try {
            new MyLogging();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return logger;
}
public static void log(Level level, String msg){
    getLogger().log(level, msg);
    System.out.println(msg);
}

The logmethod is static, so it can be called from any class using the class name.

log方法是静态的,因此可以使用类名从任何类调用它。

So from all your classes, you can log just be invoking log method as below:

所以从你所有的类中,你可以记录调用 log 方法,如下所示:

public class Test1 {
    //static Logger logger; //no need to create an object for logging

    public Test1()throws IOException {

         MyLogging.log(Level.INFO, MyLogging.class.getName()); //call log method using classname
    }