001/* 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2026, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v2.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014 015package ch.qos.logback.core.status; 016 017import java.io.FileNotFoundException; 018import java.io.FileOutputStream; 019import java.io.PrintStream; 020 021public class OnFileStatusListener extends OnPrintStreamStatusListenerBase { 022 023 String filename; 024 PrintStream ps; 025 026 @Override 027 public void start() { 028 if (filename == null) { 029 addInfo("File option not set. Defaulting to \"status.txt\""); 030 filename = "status.txt"; 031 } 032 033 try { 034 FileOutputStream fos = new FileOutputStream(filename, true); 035 ps = new PrintStream(fos, true); 036 } catch (FileNotFoundException e) { 037 addError("Failed to open [" + filename + "]", e); 038 return; 039 } 040 041 super.start(); 042 043 } 044 045 @Override 046 public void stop() { 047 if (!isStarted) { 048 return; 049 } 050 if (ps != null) 051 ps.close(); 052 super.stop(); 053 } 054 055 public String getFilename() { 056 return filename; 057 } 058 059 public void setFilename(String filename) { 060 this.filename = filename; 061 } 062 063 @Override 064 protected PrintStream getPrintStream() { 065 return ps; 066 } 067 068}