1 /*
2 * jBoss, the OpenSource EJB server
3 *
4 * Distributable under GPL license.
5 * See terms of license at gnu.org.
6 */
7
8 // $Id: JBossLogSink.java,v 1.1.2.1 2003/10/03 15:17:41 gregwilkins Exp $
9
10 package org.jboss.jetty.log;
11
12 //------------------------------------------------------------------------------
13
14 import java.util.HashMap;
15 import org.jboss.logging.Logger;
16 import org.mortbay.util.Code;
17 import org.mortbay.util.Frame;
18 import org.mortbay.util.Log;
19 import org.mortbay.util.LogSink;
20
21 //------------------------------------------------------------------------------
22
23 /* ------------------------------------------------------------ */
24
25 /**
26 * This class bidges the API between Jetty and Log4J.
27 *
28 * @author <a href="mailto:">Jules Gosnell</a>
29 * @version $Id: JBossLogSink.java,v 1.1.2.1 2003/10/03 15:17:41 gregwilkins Exp $
30 * @since 1.0
31 * @see org.mortbay.util.LogSink
32 */
33 public class JBossLogSink
34 implements LogSink
35 {
36 Logger _log;
37 boolean _started = false;
38 HashMap _dispatch = new HashMap();
39
40 interface MyLogger {void log(String s);}
41
42 public
43 JBossLogSink()
44 {
45 // populate the dispatch map...
46
47 // don't necessarily use just _log.debug() for Jetty debug:
48 // Jetty guards it's debug output to it's log sinks, so it will
49 // only get here if Jetty has debugging enabled. However,
50 // enabling debugging in Jetty (eg via the CodeMBean) may not
51 // result in log output because JBoss has it's log level set to
52 // INFO.To reduce confusion, if Jetty debug is enabled, but not
53 // on JBoss, we will still output it to the JBoss log sink as
54 // category INFO.
55
56 _dispatch.put(Log.DEBUG,
57 new MyLogger()
58 {
59 public void log(String s)
60 {
61 if (_log.isDebugEnabled())
62 _log.debug(s);
63 else
64 _log.info("DEBUG: "+s);
65 }
66 });
67 _dispatch.put(Log.EVENT, new MyLogger(){public void log(String s){_log.info(s);}});
68 _dispatch.put(Log.WARN, new MyLogger(){public void log(String s){_log.warn("WARNING: "+s);}});
69 _dispatch.put(Log.ASSERT, new MyLogger(){public void log(String s){_log.error(s);}});
70 _dispatch.put(Log.FAIL, new MyLogger(){public void log(String s){_log.error(s);}});
71 }
72
73 // 'LifeCycle' interface
74 public void
75 initialize(Object log)
76 throws InterruptedException
77 {
78 _log = (Logger) log;
79 }
80
81 public void
82 start()
83 {
84 _started = true;
85 }
86
87 public void
88 stop()
89 throws InterruptedException
90 {
91 _started = false;
92 //_log=null;
93 }
94
95 public void
96 destroy()
97 {
98 _log = null;
99 }
100
101 public boolean
102 isStarted()
103 {
104 return _started;
105 }
106
107 public boolean
108 isDestroyed()
109 {
110 return (_log==null);
111 }
112
113 //----------------------------------------------------------------------
114 // Options interface - NYI - probably never will be...
115 //----------------------------------------------------------------------
116
117 public void
118 setOptions(String dateFormat,
119 String timezone,
120 boolean logTimeStamps,
121 boolean logLabels,
122 boolean logTags,
123 boolean logStackSize,
124 boolean logStackTrace,
125 boolean logOneLine)
126 {
127 // is it possible to translate these into JBoss logging options...?
128 }
129
130 public void
131 setOptions(String logOptions)
132 {
133 // setOptions((logOptions.indexOf(OPT_TIMESTAMP) >= 0),
134 // (logOptions.indexOf(OPT_LABEL) >= 0),
135 // (logOptions.indexOf(OPT_TAG) >= 0),
136 // (logOptions.indexOf(OPT_STACKSIZE) >= 0),
137 // (logOptions.indexOf(OPT_STACKTRACE) >= 0),
138 // (logOptions.indexOf(OPT_ONELINE) >= 0));
139 }
140
141 public String
142 getOptions()
143 {
144 // return
145 // (_logTimeStamps?"t":"")+
146 // (_logLabels?"L":"")+
147 // (_logTags?"T":"")+
148 // (_logStackSize?"s":"")+
149 // (_logStackTrace?"S":"")+
150 // (_logOneLine?"O":"");
151 return "";
152 }
153
154 /* ------------------------------------------------------------ */
155 /** Log a message.
156 * This method formats the log information as a string and calls
157 * log(String). It should only be specialized by a derived
158 * implementation if the format of the logged messages is to be changed.
159 *
160 * @param tag Tag for type of log
161 * @param msg The message
162 * @param frame The frame that generated the message.
163 * @param time The time stamp of the message.
164 */
165 public void
166 log(String tag, Object msg, Frame frame, long time)
167 {
168 boolean debugging=Code.debug();
169
170 MyLogger logger=(MyLogger)_dispatch.get(tag);
171 if (logger!=null)
172 {
173 logger.log(msg+(debugging?", "+frame:""));
174 }
175 else
176 {
177 log(msg+" - "+tag+(debugging?", "+frame:""));
178 _log.warn("JBossLogSink doesn't understand tag: '"+tag+"'");
179 }
180 }
181
182 /* ------------------------------------------------------------ */
183 /** Log a message.
184 * The formatted log string is written to the log sink. The default
185 * implementation writes the message to a PrintWriter.
186 * @param formattedLog
187 */
188 public synchronized void
189 log(String formattedLog)
190 {
191 _log.info(formattedLog);
192 }
193 }