How to Locate Java Trace Logs

Java applications can optionally generate a log file with a sequence of application-dependent entries. Each entry usually has a precise time stamp and a human-readable message describing a condition that was true at the time of the time stamp. For example, an application may generate a log entry every time a user logs in or out, or every time it fails to connect to another host over a network. Log-based traces allow system administrators to follow and troubleshoot the behavior of a system. You can locate the resulting Java log files at any point you want within the computer's folder hierarchy.

Instructions

    • 1

      Include the following line at the beginning in your Java code:

      import java.util.logging.*;

    • 2

      Open a file where the log records will be written by including the following line in your code:

      FileHandler myHandler = new Filehandler("mylog.log");

      Replace "mylog.log" by the file name you want for the log.

    • 3

      Create a Logger object, passing the name of the current class to the constructor method, as in the following sample code:

      Logger myLogger = Logger.getLogger(myClass.class.getName());

      Replace "myClass" with the name of the class that will write out log records.

    • 4

      Direct the output of your logger object to the file handle you created in Step 2 as in the following sample code:

      myLogger.addHandler(myHandler);

    • 5

      Generate log records whenever your application needs to as in the following sample code:

      myLogger.info("This is a log message");

      Replace "This is a log message" by the message your application wants to write to the log file. It will get appended at the end of the current contents of the file.

Tips & Warnings

  • By default, the log file will be located within folder "~/.java/deployment/log" in a Unix or Linux computer and within folder "%CSIDL_APPDATA%\Sun\Java\Deployment\log" in a Windows computer.

Related Searches:

References

Comments

Related Ads

Featured