PageRenderTime 69ms CodeModel.GetById 65ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

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