001: /*
002: * Log.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.executequery.util;
023:
024: import java.io.IOException;
025: import java.io.Serializable;
026: import org.apache.log4j.Appender;
027: import org.apache.log4j.Level;
028: import org.apache.log4j.Logger;
029: import org.apache.log4j.PatternLayout;
030: import org.apache.log4j.RollingFileAppender;
031: import org.executequery.SystemUtilities;
032: import org.underworldlabs.util.SystemProperties;
033:
034: /* ----------------------------------------------------------
035: * CVS NOTE: Changes to the CVS repository prior to the
036: * release of version 3.0.0beta1 has meant a
037: * resetting of CVS revision numbers.
038: * ----------------------------------------------------------
039: */
040:
041: /**
042: * <p>Logger wrapper class.<br>
043: * Provides static methods to the Log4J logging methods.
044: * This is a convenience class only and can not be instantiated.
045: *
046: * @author Takis Diakoumis
047: * @version $Revision: 1.9 $
048: * @date $Date: 2006/09/24 16:24:01 $
049: */
050: public class Log implements Serializable {
051:
052: /** The Log4J Logger object */
053: private static Logger logger;
054:
055: /** the logger name */
056: public static final String LOGGER_NAME = "system-logger";
057:
058: /** the log pattern string */
059: public static final String PATTERN = "[%d{HH:mm:ss}] %m%n";
060:
061: /** the max number of log files rolled over */
062: public static final int MAX_BACKUP_INDEX = 5;
063:
064: /** <p><code>private<code> constructor to prevent instantiation. */
065: private Log() {
066: }
067:
068: /**
069: * Initialises the logger instance with the default level.
070: */
071: public static void init() {
072: init("INFO");
073: }
074:
075: /**
076: * Initialises the logger instance with the specified level.
077: *
078: * @param level - the log level
079: */
080: public static void init(String level) {
081: // if we are already running - bail
082: if (isLogEnabled()) {
083: return;
084: }
085:
086: logger = Logger.getLogger(LOGGER_NAME);
087: try {
088: if (level != null) {
089: setLevel(level);
090: } else { // default to INFO
091: setLevel("INFO");
092: }
093:
094: // the log file path
095: String path = SystemUtilities.getSystemLogPath();
096:
097: // init the patter layout
098: PatternLayout patternLayout = new PatternLayout(PATTERN);
099: RollingFileAppender appender = new RollingFileAppender(
100: patternLayout, path, true);
101: appender.setMaxBackupIndex(MAX_BACKUP_INDEX);
102: appender.setMaxFileSize("1MB");
103: logger.addAppender(appender);
104: } catch (IOException ioExc) {
105: ioExc.printStackTrace();
106: }
107: }
108:
109: /**
110: * Adds the specified appender to the logger.
111: *
112: * @param appender - the appender to be added
113: */
114: public static void addAppender(Appender appender) {
115: if (logger == null) {
116: throw new RuntimeException("Logger not initialised.");
117: }
118: logger.addAppender(appender);
119: }
120:
121: /**
122: * Returns whether the log level is set to DEBUG.
123: */
124: public static boolean isDebugEnabled() {
125: if (logger != null) {
126: return logger.isDebugEnabled();
127: }
128: return false;
129: }
130:
131: /**
132: * Sets the logger level to that specified.
133: *
134: * @param level - the logger level to be set.<br>
135: * Valid values are: ERROR, DEBUG, INFO, WARN, ALL, FATAL, TRACE
136: */
137: public static void setLevel(String level) {
138: if (level == null) {
139: return;
140: }
141:
142: level = level.toUpperCase();
143: if (level.equals("INFO")) {
144: logger.setLevel((Level) Level.INFO);
145: } else if (level.equals("WARN")) {
146: logger.setLevel((Level) Level.WARN);
147: } else if (level.equals("DEBUG")) {
148: logger.setLevel((Level) Level.DEBUG);
149: } else if (level.equals("ERROR")) {
150: logger.setLevel((Level) Level.ERROR);
151: } else if (level.equals("FATAL")) {
152: logger.setLevel((Level) Level.FATAL);
153: } else if (level.equals("TRACE")) {
154: logger.setLevel((Level) Level.TRACE);
155: } else if (level.equals("ALL")) {
156: logger.setLevel((Level) Level.ALL);
157: }
158:
159: }
160:
161: /**
162: * Logs a message at log level INFO.
163: *
164: * @param message the log message.
165: * @param throwable the throwable.
166: */
167: public static void info(Object message, Throwable throwable) {
168: if (logger == null) {
169: return;
170: }
171: logger.info(message, throwable);
172: }
173:
174: /**
175: * Logs a message at log level WARN.
176: *
177: * @param message the log message.
178: * @param throwable the throwable.
179: */
180: public static void warning(Object message, Throwable throwable) {
181: if (logger == null) {
182: return;
183: }
184: logger.warn(message, throwable);
185: }
186:
187: /**
188: * Logs a message at log level DEBUG.
189: *
190: * @param message the log message.
191: */
192: public static void debug(Object message) {
193: if (logger == null || !logger.isDebugEnabled()) {
194: return;
195: }
196: logger.debug(message);
197: }
198:
199: /**
200: * Logs a message at log level DEBUG.
201: *
202: * @param message the log message.
203: * @param throwable the throwable.
204: */
205: public static void debug(Object message, Throwable throwable) {
206: if (logger == null || !logger.isDebugEnabled()) {
207: return;
208: }
209: logger.debug(message, throwable);
210: }
211:
212: /**
213: * Logs a message at log level ERROR.
214: *
215: * @param message the log message.
216: * @param throwable the throwable.
217: */
218: public static void error(Object message, Throwable throwable) {
219: if (logger == null) {
220: System.out.println("ERROR: " + message);
221: throwable.printStackTrace();
222: return;
223: }
224: logger.error(message, throwable);
225: }
226:
227: /**
228: * Logs a message at log level INFO.
229: *
230: * @param message the log message.
231: */
232: public static void info(Object message) {
233: if (logger == null) {
234: return;
235: }
236: logger.info(message);
237: }
238:
239: /**
240: * Logs a message at log level WARN.
241: *
242: * @param message the log message.
243: */
244: public static void warning(Object message) {
245: if (logger == null) {
246: return;
247: }
248: logger.warn(message);
249: }
250:
251: /**
252: * Logs a message at log level ERROR.
253: *
254: * @param message the log message.
255: */
256: public static void error(Object message) {
257: if (logger == null) {
258: System.err.println("ERROR: " + message);
259: return;
260: }
261: logger.error(message);
262: }
263:
264: /**
265: * Returns whether a logger exists and
266: * has been initialised.
267: *
268: * @return <code>true</code> if initialised |
269: * <code>false</code> otherwise
270: */
271: public static boolean isLogEnabled() {
272: return logger != null;
273: }
274:
275: }
|