1

I don't know if my mind just fools me or this is really not working.

I need different type of Logging-classes so I created a abstract-class, the only definition that all classes will have the same is the way the writeToLog is handled:

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();
            }
        }
    }
}

The concrete sub-classes will implement rest of the logic in their constructor, ie one of them:

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();
        }
    }
}

But when I debug I can see that when calling the writeToLog for a specific logger, it jumps into the LoggerTemplate method, and therefore fw and logfile are null. So it's not working.

Isn't it supposed to work or do I just mess something a bit up and should go into weekend ;-)

4
  • 2
    This "should" work (regarding assignment of super-classes member variables). Did you debug into the constructor of JitterBufferLogger? Probably stdout goes to an unexpected location and you missed the stacktraces. Also, 'log/.20122012.log' (the slash) is not a valid filename - see logfile.createNewFile(filename). Commented Jun 22, 2012 at 10:29
  • Did you check for the stack trace on opening the file writer? Per @MartinK's comment, the invalid file name is probably your issue. Commented Jun 22, 2012 at 10:37
  • Consider having a File parentDir in your base class and have the base class constructor populate it and create the dir. Then in the concrete class do new File(super.parentDir, fileName) or super.parentDir.createNewFile(fileName) Commented Jun 22, 2012 at 10:40
  • new ErrorLogger().writeToLog("test my derp skills"); But the main problem just seemed that fw did not write properly to the file. I capsulated it with a printwriter and it is working as expected. Don't know what messed my brain but somehow something did.. Commented Jun 22, 2012 at 10:41

2 Answers 2

1

It should work, it is normal, that the debugger stepped into the LoggerTemplate class upon entering the writeToLog() method. What is strange that the attributes in the base class have null values.

I have tested your code with the following short test program:

public class Test {
    public static void main(String[] args) {
        LoggerTemplate lt = new JitterBufferLogger();
        lt.writeToLog("Hello");
    }
}

After adding fw.flush() to the LoggerTemplate.writeToLog() method just after the fw.write() call, it worked for me, the log file had been created and it contained the log message.

Maybe the new File("log").mkdir() or some other calls throw an exception which you cannot see, because stderr had been redirected somewhere.

Sign up to request clarification or add additional context in comments.

1 Comment

yap, the stepp to LoggerTemplate must be there, how else should it work. Maybe the flush just failed, I will update my post with the new wrapping with PrintWriter which now works.
1

So what may be missing? - filewriter flushing could have helped. - I can't reproduce the null values with the original code, don't know what happened. - but as everybody, including me, said: it should work and it does.

Why was nothing in the logfile? - maybe the flush of fw was missing..

anyhow I wrapped it with a Printwriter:

public abstract class LoggerTemplate {

    protected String filename ="log/";
    protected File logfile;

    protected PrintWriter pw;

    public void writeToLog(String message) {
            try {
                pw = new PrintWriter(new FileWriter(logfile,true));
                message = new SimpleDateFormat("dd-MM-hh:mm").format(new Date()) + " " + message + "\n";
                pw.write(message);
                pw.flush();
                pw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
}

and now it's working like it should and was expected to be. Note that the fw instantiation in the concrete sub-classes is not needed anymore.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.