001package ch.qos.logback.core.status;
002
003import java.io.FileNotFoundException;
004import java.io.FileOutputStream;
005import java.io.PrintStream;
006
007public class OnFileStatusListener extends OnPrintStreamStatusListenerBase {
008
009    String filename;
010    PrintStream ps;
011
012    @Override
013    public void start() {
014        if (filename == null) {
015            addInfo("File option not set. Defaulting to \"status.txt\"");
016            filename = "status.txt";
017        }
018
019        try {
020            FileOutputStream fos = new FileOutputStream(filename, true);
021            ps = new PrintStream(fos, true);
022        } catch (FileNotFoundException e) {
023            addError("Failed to open [" + filename + "]", e);
024            return;
025        }
026
027        super.start();
028
029    }
030
031    @Override
032    public void stop() {
033        if (!isStarted) {
034            return;
035        }
036        if (ps != null)
037            ps.close();
038        super.stop();
039    }
040
041    public String getFilename() {
042        return filename;
043    }
044
045    public void setFilename(String filename) {
046        this.filename = filename;
047    }
048
049    @Override
050    protected PrintStream getPrintStream() {
051        return ps;
052    }
053
054}