Vaadin Util Logger Exception

时间:2016-01-08 15:21:18

标签: java exception logging vaadin

首先,像这样的Logger可以在没有vaadin框架的其他项目中运行。

我在这样一个单独的类中实现了记录器,但是使用vaadin它不会起作用:

    Level cfgLevel = getConfigLevel();

    // create logger
    this.logger = Logger.getLogger(GZ_IOLogger.class.getName());
    this.logger.setLevel(cfgLevel);

    // create console handler for console outputs
    ConsoleHandler cHandler = new ConsoleHandler();
    cHandler.setLevel(cfgLevel);
    this.logger.addHandler(cHandler);

    // create filehandler for fileoutputs
    try {
        //logger path in web-inf
        String loggerPath = "logs/logs.txt";

        //get vaadin resource from web-inf
        File file = this.session.getResource(loggerPath);
        //!exception line!
        FileHandler handler = new FileHandler(file.getAbsolutePath());
        this.logger.addHandler(handler);
    } catch (SecurityException e) {
        System.err.println("Logger-Exception: " + e.getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        System.err.println("Logger-Exception: " + e.getMessage());
        e.printStackTrace();
    }

我总是得到以下异常:

Logger-Exception: C:\Users\XXX\IdeaProjects\Backend\out\artifacts\Backend_war_exploded\WEB-INF\log\logs.txt.lck
java.nio.file.NoSuchFileException: C:\Users\XXX\IdeaProjects\Backend\out\artifacts\Backend_war_exploded\WEB-INF\log\logs.txt.lck
    at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:79)
    at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
    at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
    at sun.nio.fs.WindowsFileSystemProvider.newFileChannel(WindowsFileSystemProvider.java:115)
    at java.nio.channels.FileChannel.open(FileChannel.java:287)
    at java.nio.channels.FileChannel.open(FileChannel.java:335)
    at java.util.logging.FileHandler.openFiles(FileHandler.java:459)
    at java.util.logging.FileHandler.<init>(FileHandler.java:292)
    at io.GZ_IOLogger.initLogger(GZ_IOLogger.java:57)
...

我认为可能无法创建文件而logger尝试创建锁定文件。记录器有解决方案吗?我也尝试使用像C:/ test这样的绝对路径,但它也不会起作用。

1 个答案:

答案 0 :(得分:2)

在创建FileHandler之前,请确保所有目录都存在。如果目录不存在,FileHandler将不会创建目录。

File file = this.session.getResource(loggerPath);
file.getParentFile().mkdirs();
//!exception line!
FileHandler handler = new FileHandler(file.getAbsolutePath());