Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用java.util.logging.FileHandler将日志存储在单个日志文件中?_Java_Exception_Apache Flink_Logfile_Single File - Fatal编程技术网

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

如何使用java.util.logging.FileHandler将日志存储在单个日志文件中?,java,exception,apache-flink,logfile,single-file,Java,Exception,Apache Flink,Logfile,Single File,我想在一个日志文件中添加异常,在我的源代码中,以下代码被多次使用。。。我想在单个文件中存储所有异常,但它会创建多个文件,如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 ne

我想在一个日志文件中添加异常,在我的源代码中,以下代码被多次使用。。。我想在单个文件中存储所有异常,但它会创建多个文件,如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();  
}  

这是因为每次调用该方法时都会创建一个新的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()
方法一样多的文件。

可能重复@Jean BaptisteYunès no,这不是问题所在,如果我想在单个日志中添加日志,该怎么办file@Gaurav你是认真的吗?读我在第一句中写的。改为将其作为类实例变量。@POrekhov,但它不起作用,仍在创建多个files@Gaurav到底是什么不起作用?您遇到了什么错误?@Gaurav
String newDir1=“C:\\Users\\pavel.orekhov\\Desktop”这可能是您的问题,这是我的计算机上的有效路径,不是您的