• Post Reply Bookmark Topic Watch Topic
  • New Topic

No data written when trying to implement text file logger  RSS feed

 
Josh Herron
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good evening, I've been struggling along with my little test app and I'm now I'm trying to find a way to implement a logging class that I can call from my other classes/methods.

Below is the class I have and if I debug the class and call it with a string from another class I can see the string data is being received.  The C:/temp/FileCleaner.txt file is created  but no data is written.

I was initially just doing a try/catch block and had a filehandler there, but when I called the getLogger method many many files appeared in the C drive, so that clearly wasn't right.   But checking to see if a Filehandler has been initialized at some point doesn't seem to be working either.   Any suggestions?
 
 
Tehmina Yaseen
Greenhorn
Posts: 5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can create a file, append to a file, or write to a file by using the newOutputStream(Path, OpenOption...) method. This method opens or creates a file for writing bytes and returns an unbuffered output stream.
The method takes an optional OpenOption parameter. If no open options are specified, and the file does not exist, a new file is created. If the file exists, it is truncated. This option is equivalent to invoking the method with the CREATE and TRUNCATE_EXISTING options.

The following example opens a log file. If the file does not exist, it is created. If the file exists, it is opened for appending.

 
Josh Herron
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the feedback, just for leaning purposes do the filehandler and simpleformatter bot work with text files?  

I thought my logic was sound but since it's not writing data I would have to guess that I'm wrong.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch TY
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what's going wrong with your FileLogger class, as I can use it and it works fine for me.  Here's  how I called it:

Is this how you were intending to use it?
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing I changed in your FileLogger class that may have affected its functioning: I added imports that you left off of you code posting. 

I made a guess as to the Logger import.  This is why it's important to post a complete, compilable class.
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some notes on your FileLogger class that are unrelated to your question.  FYI.

* The naming convention with private static final variables is ALL_CAPS with underscores for spaces.
* Don't write if (boolean == false), write if (!boolean).  Similarly, don't write if (boolean == true), write if (boolean).
* Since Java 7, you've been able to write two or more catch exceptions that do the same thing like this:
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thinking more about the FileLogger class, if you want something done only once, a great way to do this without a boolean variable is to put the code in a static initializer.  Also, I don't see why any of the other variables need to be static; they can be local variables.
 
Josh Herron
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, many of the reading materials I have for learning Java are a bit dated so knowing the boolean argument enhancements are very helpful.

Also, from a static initializer perspective, I created my method as static.   I guess I don't follow what you mean by creating a static initializer?  Clearly I don't understand because I thought I was doing that. 
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A static initializer looks like this:

A static initializer executes once when the class is loaded.  Therefore it's a great place to put things you only want done once.
 
Josh Herron
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!  This simple example has helped immensely.    I can't wait to get home tonight and take another look at this.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!