001: package com.jat.core.log;
002:
003: import java.text.SimpleDateFormat;
004: import java.util.Date;
005: import java.util.TimeZone;
006:
007: import com.jat.core.config.Config;
008:
009: /**
010: * <p>Title: JAT</p>
011: * <p>Description: This class is responsible for logging features management.</p>
012: * <p>There are 4 types of log files:
013: * <ul>
014: * <li><i>error</i> log: contains information about errors and warnings</li>
015: * <li>log</i> log: contains information about user accesses and resources initialization</li>
016: * <li><i>time</i> log: contains information about response time (ex. database query response time)</li>
017: * <li><i>debug</i> log: contains information about debug information</li>
018: * </ul>
019: * Every log could be turn off as explained in the configuration (see below).
020: * </p>
021: * <p><b>Log Configuration</b>: in the <i>log</i> section of the main configuration file, the following information must be present:
022: * <ul>
023: * <li><b>debug</b> indicates if debug log is active (possible values: <i>active</i> or <i>inactive</i>)</li>
024: * <li><b>log</b> indicates if debug log is active (possible values: <i>active</i> or <i>inactive</i>)</li>
025: * <li><b>error</b> indicates if debug log is active (possible values: <i>active</i> or <i>inactive</i>)</li>
026: * <li><b>time</b> indicates if debug log is active (possible values: <i>active</i> or <i>inactive</i>)</li>
027: * <li><b>timezone</b> the timezone (ex. <i>ECT</i>)</li>
028: * <li><b>date_format</b> date format into log files (ex. <i>yyyy-MM-dd HH:mm:ss</i>)</li>
029: * <li><b>path</b> the absolute or relative path where log files will be created</li>
030: * </ul>
031: * </p>
032: * <p>Copyright: Copyright (c) 2004 -2005 Stefano Fratini (stefano.fratini@gmail.com)</p>
033: * <p>Distributed under the terms of the GNU Lesser General Public License, v2.1 or later</p>
034: * @author stf
035: * @version 1.1
036: */
037:
038: public class LogManager {
039:
040: /**
041: * Instance of CGMessage
042: */
043: public static LogManager specificMessage_;
044:
045: public static Log getLog() {
046: return log_;
047: }
048:
049: public static Log getDebug() {
050: return debug_;
051: }
052:
053: public static Log getError() {
054: return error_;
055: }
056:
057: public static Log getTime() {
058: return time_;
059: }
060:
061: /**
062: * Send a debug message
063: * @param _message text of the trace
064: */
065: static public void sendDebug(String _message) {
066: try {
067: /*
068: * format message
069: */
070: if (debug_.isActivated()) {
071: StringBuffer messageString = specificMessage_
072: .getSpecificInfo();
073: messageString.append("DEBUG ");
074: messageString.append(_message);
075:
076: /*
077: * send
078: */
079: debug_.send(messageString);
080: }
081: } catch (Exception e) {
082: String dt = "";
083: try {
084: dt = specificMessage_.getSpecificInfo().toString();
085: } catch (Exception ignored) {
086: }
087: //if (Project.DEBUG) System.out.println (dt+"CGMessage::sendDebug for message : '" + _message +"'; exception : " + e);
088: }
089: }
090:
091: /**
092: * Send a log message
093: * @param _message text of the trace
094: */
095: static public void sendLog(String _message) {
096: try {
097: /*
098: * format message
099: */
100: if (log_.isActivated()) {
101: StringBuffer messageString = specificMessage_
102: .getSpecificInfo();
103: messageString.append("LOG ");
104: messageString.append(_message);
105:
106: /*
107: * send
108: */
109: log_.send(messageString);
110: debug_.send(messageString);
111: }
112: } catch (Exception e) {
113: String dt = "";
114: try {
115: dt = specificMessage_.getSpecificInfo().toString();
116: } catch (Exception ignored) {
117: }
118: }
119: }
120:
121: /**
122: * Send an error message
123: * @param _message text of the trace
124: */
125: static public void sendError(String _message) {
126: sendError(_message, "ERROR");
127: com.jat.core.Project.notifyError();
128: }
129:
130: /**
131: * Send a warning message
132: * @param _message text of the trace
133: */
134: static public void sendWarning(String _message) {
135: sendError(_message, "WARNING");
136: }
137:
138: /**
139: * Send an error message
140: * @param _message text of the trace
141: * @param _type the type of message (i.e. warning or error)
142: */
143: static private void sendError(String _message, String _type) {
144: try {
145: /*
146: * format message
147: */
148: if (error_.isActivated()) {
149: StringBuffer messageString = specificMessage_
150: .getSpecificInfo();
151: // messageString.append(_id + " ");
152: messageString.append(_type + " ");
153: messageString.append(_message);
154: /*
155: * send
156: */
157: error_.send(messageString);
158: debug_.send(messageString);
159: //if (Project.DEBUG) System.out.println(messageString);
160: }
161:
162: } catch (Exception e) {
163: String dt = "";
164: try {
165: dt = specificMessage_.getSpecificInfo().toString();
166: } catch (Exception ignored) {
167: }
168: //if (Project.DEBUG) System.out.println (dt+"CGMessage::sendError for message : '" + _message +"': exception : " + e.getMessage());
169: }
170: }
171:
172: /**
173: * Send a time message
174: * @param _procedure procedure name
175: * @param _time time in milliseconds
176: */
177: static public void sendTime(String _procedure, long _time) {
178: sendTime(_procedure, _time, false);
179: }
180:
181: /**
182: * Send a time message
183: * @param _procedure procedure name
184: * @param _time time in milliseconds
185: * @param _error true if an error in the procedure occours
186: */
187: static public void sendTime(String _procedure, long _time,
188: boolean _error) {
189: try {
190: /*
191: * format message
192: */
193: if (time_.isActivated()) {
194: StringBuffer messageString = specificMessage_
195: .getSpecificInfo();
196: messageString.append("|" + _procedure + "|" + _time
197: + "|" + (_error ? "Error" : "Ok"));
198: time_.send(messageString);
199: //debug_.send(messageString);
200: //if (Project.DEBUG) System.out.println(messageString);
201: }
202:
203: } catch (Exception e) {
204: String dt = "";
205: try {
206: dt = specificMessage_.getSpecificInfo().toString();
207: } catch (Exception ignored) {
208: }
209: //if (Project.DEBUG) System.out.println (dt+" CGMessage::sendTime for message : " + _procedure +"|"+_time+ "; exception : " + e.getMessage());
210: }
211: }
212:
213: StringBuffer getSpecificInfo() throws Exception {
214: try {
215: return new StringBuffer(messageDateFormat_
216: .format(new Date())
217: + " " + getHostApplication() + " ");
218: } catch (Exception ex) {
219: throw new java.io.IOException(
220: "LogManager::getSpecificInfo:: exception : " + ex);
221: }
222: }
223:
224: private static String hostAddress;
225:
226: public static String getHostApplication() {
227: if (hostAddress == null) {
228: try {
229: hostAddress = java.net.InetAddress.getLocalHost()
230: .getHostAddress();
231: } catch (Exception e) {
232: System.out
233: .println("Project::getHostApplication:: exception: "
234: + e);
235: hostAddress = "N.D.";
236: }
237: }
238: return hostAddress;
239: }
240:
241: static private Log initMessageOutput(String _path, String _name)
242: throws java.io.IOException, Exception {
243: String path = _path + (_path.endsWith("/") ? "" : "/") + _name
244: + "/";
245: Log messageOutput = new Log(path, _name);
246: return messageOutput;
247: }
248:
249: static public void init() throws Exception {
250: try {
251: /*
252: * Set specific Instance of CGMessage
253: */
254: Object[] params = {};
255:
256: specificMessage_ = new LogManager();
257: /*
258: * Set TimeZone to "ECT"
259: */
260: TimeZone ectTimeZone = TimeZone.getTimeZone(Config
261: .getCurrent().getValue("log", "time_zone"));
262: TimeZone.setDefault(ectTimeZone);
263:
264: String dateFormat = Config.getCurrent().getValue("log",
265: "date_format");
266: messageDateFormat_ = new SimpleDateFormat(dateFormat);
267:
268: String path = Config.getCurrent().getValue("log", "path");
269: /*
270: * Initializes Log and messageDateFormat_
271: */
272: Log.init(ectTimeZone);
273: messageDateFormat_.setTimeZone(ectTimeZone);
274:
275: /*
276: * Set debug options
277: */
278: debug_ = initMessageOutput(path, "debug"); //,Config.getCurrent());
279:
280: /*
281: * Set log options
282: */
283: log_ = initMessageOutput(path, "log"); //,Config.getCurrent());
284:
285: /*
286: * Set error options
287: */
288: error_ = initMessageOutput(path, "error"); //,Config.getCurrent());
289: /*
290: * Set error options
291: */
292: time_ = initMessageOutput(path, "time"); //,Config.getCurrent());
293: } catch (Exception ex) {
294: throw new Exception(
295: "LogManager:: init: exception initialiting log manager: "
296: + ex);
297: }
298:
299: }
300:
301: private LogManager() {
302: }
303:
304: /**
305: * Format of dates used in messages
306: */
307: private static SimpleDateFormat messageDateFormat_ = new SimpleDateFormat(
308: "yyyy/MM/dd HH:mm:ss z : ");
309:
310: /**
311: * Log management object
312: */
313: private static Log log_;
314:
315: /**
316: * Debug management object
317: */
318: private static Log debug_;
319:
320: /**
321: * Error management object
322: */
323: private static Log error_;
324:
325: /**
326: * Time management object
327: */
328: private static Log time_;
329: }
|