001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * @created May 4, 2005
014: * @author James Dixon
015: *
016: */
017: package org.pentaho.util.logging;
018:
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
021: import org.pentaho.core.system.PentahoSystem;
022: import org.pentaho.messages.Messages;
023: import org.pentaho.util.IVersionHelper;
024:
025: public class Logger {
026:
027: private static int logLevel = ILogger.ERROR;
028:
029: private static String MISC_LOG = "misc-"; //$NON-NLS-1$
030:
031: private static Log logger;
032:
033: private static String logName = "org.pentaho.util.logging.Logger"; //$NON-NLS-1$
034:
035: static {
036: logger = LogFactory.getLog(logName);
037: }
038:
039: public static String getLogLevelName(int localLogLevel) {
040: switch (localLogLevel) {
041: case ILogger.TRACE:
042: return "TRACE"; //$NON-NLS-1$
043: case ILogger.DEBUG:
044: return "DEBUG"; //$NON-NLS-1$
045: case ILogger.INFO:
046: return "INFO"; //$NON-NLS-1$
047: case ILogger.WARN:
048: return "WARN"; //$NON-NLS-1$
049: case ILogger.ERROR:
050: return "ERROR"; //$NON-NLS-1$
051: case ILogger.FATAL:
052: return "FATAL"; //$NON-NLS-1$
053: default:
054: return Messages.getString("Logger.CODE_LOG_UNKNOWN"); //$NON-NLS-1$
055: }
056: }
057:
058: public static int getLogLevel(String localLogLevel) {
059: if ("TRACE".equalsIgnoreCase(localLogLevel)) { //$NON-NLS-1$
060: return ILogger.TRACE;
061: }
062: if ("DEBUG".equalsIgnoreCase(localLogLevel)) { //$NON-NLS-1$
063: return ILogger.DEBUG;
064: }
065: if ("INFO".equalsIgnoreCase(localLogLevel)) { //$NON-NLS-1$
066: return ILogger.INFO;
067: }
068: if ("WARN".equalsIgnoreCase(localLogLevel)) { //$NON-NLS-1$
069: return ILogger.WARN;
070: }
071: if ("ERROR".equalsIgnoreCase(localLogLevel)) { //$NON-NLS-1$
072: return ILogger.ERROR;
073: }
074: if ("FATAL".equalsIgnoreCase(localLogLevel)) { //$NON-NLS-1$
075: return ILogger.FATAL;
076: }
077: return ILogger.ERROR;
078: }
079:
080: public static int getLogLevel() {
081: return logLevel;
082: }
083:
084: public static void setLogLevel(int newLogLevel) {
085: logLevel = newLogLevel;
086: }
087:
088: public static void trace(Object caller, String message) {
089: String id = (caller == null) ? Messages
090: .getString("Logger.DEBUG_LOG_UNKNOWN") : caller.getClass().getName(); //$NON-NLS-1$
091: if (logLevel <= ILogger.TRACE) {
092: logger.trace(MISC_LOG + id + ": " + message); //$NON-NLS-1$
093: }
094: }
095:
096: public static void debug(Object caller, String message) {
097: String id = (caller == null) ? Messages
098: .getString("Logger.DEBUG_LOG_UNKNOWN") : caller.getClass().getName(); //$NON-NLS-1$
099: if (logLevel <= ILogger.DEBUG) {
100: logger.debug(MISC_LOG + id + ": " + message); //$NON-NLS-1$
101: }
102: }
103:
104: public static void info(Object caller, String message) {
105: String id = (caller == null) ? Messages
106: .getString("Logger.DEBUG_LOG_UNKNOWN") : caller.getClass().getName(); //$NON-NLS-1$
107: if (logLevel <= ILogger.INFO) {
108: logger.info(MISC_LOG + id + ": " + message); //$NON-NLS-1$
109: }
110: }
111:
112: public static void warn(Object caller, String message) {
113: String id = (caller == null) ? Messages
114: .getString("Logger.DEBUG_LOG_UNKNOWN") : caller.getClass().getName(); //$NON-NLS-1$
115: if (logLevel <= ILogger.WARN) {
116: logger.warn(MISC_LOG + id + ": " + message); //$NON-NLS-1$
117: }
118: }
119:
120: public static void error(Object caller, String message) {
121: String id = (caller == null) ? Messages
122: .getString("Logger.DEBUG_LOG_UNKNOWN") : caller.getClass().getName(); //$NON-NLS-1$
123: if (logLevel <= ILogger.ERROR) {
124: logger.error(MISC_LOG + id + ": " + message); //$NON-NLS-1$
125: }
126: }
127:
128: public static void fatal(Object caller, String message) {
129: String id = (caller == null) ? Messages
130: .getString("Logger.DEBUG_LOG_UNKNOWN") : caller.getClass().getName(); //$NON-NLS-1$
131: if (logLevel <= ILogger.FATAL) {
132: logger.fatal(MISC_LOG + id + ": " + message); //$NON-NLS-1$
133: }
134: }
135:
136: public static void trace(Object caller, String message,
137: Throwable error) {
138: String id = (caller == null) ? Messages
139: .getString("Logger.DEBUG_LOG_UNKNOWN") : caller.getClass().getName(); //$NON-NLS-1$
140: if (logLevel <= ILogger.TRACE) {
141: logger.trace(MISC_LOG + id + ": " + message, error); //$NON-NLS-1$
142: }
143: }
144:
145: public static void debug(Object caller, String message,
146: Throwable error) {
147: String id = (caller == null) ? Messages
148: .getString("Logger.DEBUG_LOG_UNKNOWN") : caller.getClass().getName(); //$NON-NLS-1$
149: if (logLevel <= ILogger.DEBUG) {
150: logger.debug(MISC_LOG + id + ": " + message, error); //$NON-NLS-1$
151: }
152: }
153:
154: public static void info(Object caller, String message,
155: Throwable error) {
156: String id = (caller == null) ? Messages
157: .getString("Logger.DEBUG_LOG_UNKNOWN") : caller.getClass().getName(); //$NON-NLS-1$
158: if (logLevel <= ILogger.INFO) {
159: logger.info(MISC_LOG + id + ": " + message, error); //$NON-NLS-1$
160: }
161: }
162:
163: public static void warn(Object caller, String message,
164: Throwable error) {
165: String id = (caller == null) ? Messages
166: .getString("Logger.DEBUG_LOG_UNKNOWN") : caller.getClass().getName(); //$NON-NLS-1$
167: if (logLevel <= ILogger.WARN) {
168: logger.warn(MISC_LOG + id + ": " + message, error); //$NON-NLS-1$
169: }
170: }
171:
172: public static void error(Object caller, String message,
173: Throwable error) {
174: String id = (caller == null) ? Messages
175: .getString("Logger.DEBUG_LOG_UNKNOWN") : caller.getClass().getName(); //$NON-NLS-1$
176: if (logLevel <= ILogger.ERROR) {
177: logger.error(MISC_LOG + id + ": " + message, error); //$NON-NLS-1$
178: }
179: }
180:
181: public static void fatal(Object caller, String message,
182: Throwable error) {
183: String id = (caller == null) ? Messages
184: .getString("Logger.DEBUG_LOG_UNKNOWN") : caller.getClass().getName(); //$NON-NLS-1$
185: if (logLevel <= ILogger.FATAL) {
186: logger.fatal(MISC_LOG + id + ": " + message, error); //$NON-NLS-1$
187: }
188: }
189:
190: public static void debug(String caller, String message) {
191: String id = (caller == null) ? Messages
192: .getString("Logger.DEBUG_LOG_UNKNOWN") : caller; //$NON-NLS-1$
193: if (logLevel <= ILogger.DEBUG) {
194: logger.debug(MISC_LOG + id + ": " + message); //$NON-NLS-1$
195: }
196: }
197:
198: public static void info(String caller, String message) {
199: String id = (caller == null) ? Messages
200: .getString("Logger.DEBUG_LOG_UNKNOWN") : caller; //$NON-NLS-1$
201: if (logLevel <= ILogger.INFO) {
202: logger.info(MISC_LOG + id + ": " + message); //$NON-NLS-1$
203: }
204: }
205:
206: public static void warn(String caller, String message) {
207: String id = (caller == null) ? Messages
208: .getString("Logger.DEBUG_LOG_UNKNOWN") : caller; //$NON-NLS-1$
209: if (logLevel <= ILogger.WARN) {
210: logger.warn(MISC_LOG + id + ": " + message); //$NON-NLS-1$
211: }
212: }
213:
214: public static void error(String caller, String message) {
215: String id = (caller == null) ? Messages
216: .getString("Logger.DEBUG_LOG_UNKNOWN") : caller; //$NON-NLS-1$
217: if (logLevel <= ILogger.ERROR) {
218: logger.error(MISC_LOG + id + ": " + message); //$NON-NLS-1$
219: }
220: }
221:
222: public static void fatal(String caller, String message) {
223: String id = (caller == null) ? Messages
224: .getString("Logger.DEBUG_LOG_UNKNOWN") : caller; //$NON-NLS-1$
225: if (logLevel <= ILogger.FATAL) {
226: logger.fatal(MISC_LOG + id + ": " + message); //$NON-NLS-1$
227: }
228: }
229:
230: public static void debug(String caller, String message,
231: Throwable error) {
232: String id = (caller == null) ? Messages
233: .getString("Logger.DEBUG_LOG_UNKNOWN") : caller; //$NON-NLS-1$
234: if (logLevel <= ILogger.DEBUG) {
235: logger.debug(MISC_LOG + id + ": " + message, error); //$NON-NLS-1$
236: }
237: }
238:
239: public static void info(String caller, String message,
240: Throwable error) {
241: String id = (caller == null) ? Messages
242: .getString("Logger.DEBUG_LOG_UNKNOWN") : caller; //$NON-NLS-1$
243: if (logLevel <= ILogger.INFO) {
244: logger.info(MISC_LOG + id + ": " + message, error); //$NON-NLS-1$
245: }
246: }
247:
248: public static void warn(String caller, String message,
249: Throwable error) {
250: String id = (caller == null) ? Messages
251: .getString("Logger.DEBUG_LOG_UNKNOWN") : caller; //$NON-NLS-1$
252: if (logLevel <= ILogger.WARN) {
253: logger.warn(MISC_LOG + id + ": " + message, error); //$NON-NLS-1$
254: }
255: }
256:
257: public static void error(String caller, String message,
258: Throwable error) {
259: String id = (caller == null) ? Messages
260: .getString("Logger.DEBUG_LOG_UNKNOWN") : caller; //$NON-NLS-1$
261: if (logLevel <= ILogger.ERROR) {
262: IVersionHelper versionHelper = PentahoSystem
263: .getVersionHelper(null);
264: logger
265: .error("Error: Pentaho " + versionHelper.getVersionInformation(Logger.class)); //$NON-NLS-1$
266: logger.error(MISC_LOG + id + ": " + message, error); //$NON-NLS-1$
267: logger.error("Error end:"); //$NON-NLS-1$
268: }
269: }
270:
271: public static void fatal(String caller, String message,
272: Throwable error) {
273: String id = (caller == null) ? Messages
274: .getString("Logger.DEBUG_LOG_UNKNOWN") : caller; //$NON-NLS-1$
275: if (logLevel <= ILogger.FATAL) {
276: IVersionHelper versionHelper = PentahoSystem
277: .getVersionHelper(null);
278: logger
279: .error("Error: Pentaho " + versionHelper.getVersionInformation(Logger.class)); //$NON-NLS-1$
280: logger.fatal(MISC_LOG + id + ": " + message, error); //$NON-NLS-1$
281: logger.error("Error end:"); //$NON-NLS-1$
282: }
283: }
284:
285: }
|