/tests/Fest/src/org/gjt/sp/jedit/testframework/Log.java

# · Java · 64 lines · 27 code · 7 blank · 30 comment · 1 complexity · 0da07f957e3f267b3edd94e78d419936 MD5 · raw file

  1. package org.gjt.sp.jedit.testframework;
  2. import java.io.*;
  3. import java.util.*;
  4. /**
  5. * Simple logger for debugging, useful for web app components that don't have
  6. * access to the servlet container log. All exceptions that could possibly be
  7. * caught are caught and silently ignored so as to not cause an application
  8. * failure.
  9. * <p>
  10. * The specific log file may be set by setting a System property named "LOG_FILE".
  11. * Of course, the file must be writable, or there will be a problem! If this
  12. * property is not set, the log file will be written to $user.home/debug.txt.
  13. * <p>
  14. * The log file will always be appended to, never overwritten.
  15. *
  16. * @author Dale Anson
  17. */
  18. public class Log {
  19. private static File logfile = System.getProperty("LOG_FILE") != null ?
  20. new File(System.getProperty("LOG_FILE")) :
  21. new File(System.getProperty("user.home"), "jedit_debug.txt");
  22. /**
  23. * Write a message to the log file.
  24. *
  25. * @param msg the message to write.
  26. */
  27. public static void log(String msg) {
  28. try {
  29. FileWriter fw = new FileWriter(logfile, true);
  30. fw.write(new Date().toString() + ": " + msg + "\n");
  31. fw.flush();
  32. fw.close();
  33. }
  34. catch (Exception e) {
  35. }
  36. }
  37. /**
  38. * Write an exception to the log file. This will write out the full stack
  39. * trace, not just the message.
  40. *
  41. * @param e The exception to log.
  42. */
  43. public static void log(Exception e) {
  44. StringWriter sw = new StringWriter();
  45. PrintWriter pw = new PrintWriter(sw);
  46. e.printStackTrace(pw);
  47. log(sw.toString());
  48. }
  49. /**
  50. * Convenience method to log a StringBuffer to the log.
  51. *
  52. * @param sb The StringBuffer containing the message to write to the log.
  53. */
  54. public static void log(StringBuffer sb) {
  55. log(sb.toString());
  56. }
  57. }