Simple Logger
Home Up Jar War Ear Simple Logger Eclipse

 

Back Home Next

Up

ourmission
theweb.gif (1103 bytes)
booksandbibles16
thenewsroom
governmentrm.gif (1147 bytes)
searchpage
tutorials
webtools
websecurity

What is the Web?

Privacy & Disclaimer
copyrights
notices
HOME

Visitors Since
Aug - 2004

Hit Counter

 

Simple Logger

Well I started with a final static string pointing to a directory on the machine of the application. Which, at the time hade a D drive with a directory called Kron.

Then, I decided to do some updating and move the application to my machine to create a properties file.

When I went to log, the status of the properties file, I discovered that my logger crashed?

So, I went looking, and those hard coded things were sneaking up on me. But, I did not do a graceful recovery.

My first thought was to just do a check to see, if the actual directory existed. This was easy enough:
File parentDir = filePath.getParentFile( );
boolean exists = parentDir.exists( );
 

You just create a file object and find out if it exists. Right?

Wrong!!

You want to know, if the directory exists, beucase you are going to create the file yourself. So, we need to create a File object, for the full path, then we want to get the directory structure that points to the location of the file at the end of the path.

Ok. So, we do the File.getParrentFile and every thing is OK.

We do a file exists on the directory and BINGO! We know that on my machine, there is no such thing.

And, if they decide to move things around on the server, or someone is cleaning up and deltes the directory. The application will know that also and still not crash.

Problem solved.

No?

I found out, that I could not write to the requested directory. So, what do I do. Exit and skip logging.

No!!

What we need to do in the real world, is find a new home for the log file. So, we need to use the following code:

filePath.getName( ));

We need to use getName to get the actual file name, and then use the System properties to find out where we are and place the log file there.

Another interesting thing is this.

I wanted to look at the properties file, during the processing, but discovered that the application (or Eclipse) would not let it go unitl the program completely existed.

So, I added to additional calls to the properties method, and keep the idea here:

fw.flush( );
fw.close( );
Because of the strange mannerisms of Java and the friendly operating systems that it runs on, it is possible that it might take awhile before all of the data is actually written to the file on the physical harddisk.
This prompted me to add the flush and close methods every where I use that combination. The flush will ensure that your data is actually on the harddisk, when your callers return from your method.

The final code snippet if below, and I hope that it will help others out there in internet land.

    */

    static  String ftpLog = "D:\\Kron\\logs\\ftpLog.log";

    int count = 0;

     /*

     * Routine to delete requested file from FTP server. 2 Aug 2005 cez

     *

     */

    public static void doFTPLog( final String thisMessage )

        throws IOException {

        StringBuffer realOutBuffer = new StringBuffer( 1000 );

        final Date Now = new Date(  );

        /*

         *  First, verify that the log file directory is valid

         *

         *  We create a file handle for the log file location

         */

        File filePath = new File( ftpLog );

         /*

         *   Now, we extract the directory path from the log file location

         */

        File parentDir = filePath.getParentFile(  );

        boolean exists = parentDir.exists(  );

         /*

         * if the directory does not exist, then use the application's directory.

         */

        final File finalPathLocation;

        if ( exists ) {

            finalPathLocation = filePath;

        } else {

            finalPathLocation = new File (System.getProperty( "user.dir" ) +

                filePath.getName(  ));

            realOutBuffer.append( "\n??? Log File Path Does Not Exist: " + ftpLog );

            realOutBuffer.append( "\n??? Log File Saved to: " + finalPathLocation.getAbsolutePath() );

            ftpLog = finalPathLocation.getCanonicalPath();

        }

         FileWriter fw = new FileWriter( finalPathLocation, true );

         try {

            realOutBuffer.append( "\n*** " + Now );

            realOutBuffer.append( "\n*** " + thisMessage );

            fw.write( realOutBuffer.toString(  ) );

        } catch ( IOException e ) {

        } finally {

            // please, always write out the general log, even if an exception

            // occurs .....

            fw.close(  );

        }

    }