私の心が私をだますだけなのか、これが本当に機能していないのかはわかりません。
異なるタイプのLoggingクラスが必要なので、抽象クラスを作成しました。すべてのクラスが同じになる唯一の定義は、writeToLogの処理方法です。
public abstract class LoggerTemplate {
protected String filename ="log/";
protected File logfile;
protected FileWriter fw;
public void writeToLog(String message) {
if(fw != null) {
try {
message = new SimpleDateFormat("dd-MM-hh:mm").format(new Date()) + " " + message;
fw.write(message);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
具体的なサブクラスは、コンストラクターに残りのロジックを実装します。つまり、次のいずれかです。
public class JitterBufferLogger extends LoggerTemplate {
public JitterBufferLogger() {
super();
filename += new SimpleDateFormat("yyyyddMMhhmm'.log'").format(new Date());
if(!new File("log/").exists())
new File("log").mkdir();
logfile = new File(filename);
try {
logfile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
try {
fw = new FileWriter(logfile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
しかし、デバッグすると、特定のロガーに対してwriteToLogを呼び出すと、LoggerTemplateメソッドにジャンプするため、fwとlogfileがnullであることがわかります。だからそれは機能していません。
それはうまくいくはずではないのですか、それとも私は何かを少し台無しにして週末に行くべきですか;-)