1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package de.bea.domingo.monitor;
24
25 import java.io.PrintWriter;
26 import java.text.SimpleDateFormat;
27 import java.util.Date;
28
29 /***
30 * Console monitor, logs everything to a <code>PrintWriter</code>.
31 *
32 * @author <a href=mailto:kriede@users.sourceforge.net>Kurt Riede</a>
33 */
34 public class WriterMonitor extends AbstractDefaultMonitor {
35
36 /*** Date format used to format dates in log output. */
37 private final SimpleDateFormat dateFormat;
38
39 /*** Reference to the output stream for monitoring. */
40 private PrintWriter writer;
41
42 /***
43 * Constructor.
44 *
45 * @param theWriter the output stream for monitoring
46 */
47 public WriterMonitor(final PrintWriter theWriter) {
48 writer = theWriter;
49 dateFormat = new SimpleDateFormat ("yyyy.MM.dd HH:mm:ss");
50 }
51
52 /***
53 * {@inheritDoc}
54 * @see de.bea.domingo.monitor.AbstractDefaultMonitor#monitor(java.lang.String)
55 */
56 protected final synchronized void monitor(final String message) {
57 final StringBuffer buffer = new StringBuffer();
58 buffer.append("[" + dateFormat.format(new Date()) + "] ");
59 buffer.append(Thread.currentThread().getName() + ": " + message);
60 writer.println(buffer.toString());
61 }
62
63 /***
64 * {@inheritDoc}
65 * @see de.bea.domingo.monitor.AbstractDefaultMonitor#monitor(java.lang.Throwable)
66 */
67 protected final synchronized void monitor(final Throwable throwable) {
68 if (throwable != null) {
69 throwable.printStackTrace(writer);
70 }
71 }
72 }