aboutsummaryrefslogtreecommitdiffstats
path: root/vespalog/src/main/java/com/yahoo/log/FileLogTarget.java
blob: 1fe00fbd6f5c02e2b34744b7c7d0fd4eef5ef237 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.log;

import java.io.*;

/**
 * @author Ulf Lilleengen
 * @since 5.1
 * Should only be used internally in the log library
 */
class FileLogTarget implements LogTarget {
    private final File file;
    private FileOutputStream fileOutputStream;

    public FileLogTarget(File target) throws FileNotFoundException {
        this.file = target;
        this.fileOutputStream = null;
    }

    public synchronized OutputStream open() {
        try {
            close();
            fileOutputStream = new FileOutputStream(file, true);
        } catch (FileNotFoundException e) {
            throw new RuntimeException("Unable to open output stream", e);
        }
        return fileOutputStream;
    }

    public synchronized void close() {
        try {
            if (fileOutputStream != null) {
                fileOutputStream.close();
                fileOutputStream = null;
            }
        } catch (IOException e) {
            throw new RuntimeException("Unable to close output stream", e);
        }
    }

    @Override
    public String toString() {
        return file.getAbsolutePath();
    }
}