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 Jun 23, 2005
014: * @author Marc Batchelor
015: *
016: */
017:
018: package org.pentaho.core.system;
019:
020: import java.io.Serializable;
021: import java.text.MessageFormat;
022:
023: import org.apache.commons.logging.Log;
024: import org.pentaho.core.session.IPentahoSession;
025: import org.pentaho.util.IVersionHelper;
026: import org.pentaho.util.logging.ILogger;
027:
028: public abstract class PentahoBase implements ILogger, Serializable {
029:
030: protected int loggingLevel = UNKNOWN;
031:
032: public static final String LOGID_MASK1 = "{0}:{1}:{2}: "; //$NON-NLS-1$
033:
034: public static final String LOGID_MASK2 = "{0}:{1}:{2}:{3} "; //$NON-NLS-1$
035:
036: public static final String LOGID_SEPARATOR = ":"; //$NON-NLS-1$
037:
038: public String EMPTYLOGID = "::: "; //$NON-NLS-1$
039:
040: private String logId = EMPTYLOGID;
041:
042: public abstract Log getLogger();
043:
044: public String getLogId() {
045: return logId;
046: }
047:
048: public void setLogId(String lId) {
049: logId = lId;
050: }
051:
052: public void genLogIdFromSession(IPentahoSession sess) {
053: genLogIdFromInfo(sess.getId(), sess.getProcessId(), sess
054: .getActionName());
055: }
056:
057: public void genLogIdFromInfo(String sessId, String procId,
058: String actName) {
059: Object[] args = { sessId, procId, actName };
060: setLogId(MessageFormat.format(LOGID_MASK1, noNulls(args)));
061: }
062:
063: public void genLogIdFromInfo(String sessId, String procId,
064: String actName, String instId) {
065: Object[] args = { sessId, procId, actName, instId };
066: setLogId(MessageFormat.format(LOGID_MASK2, noNulls(args)));
067: }
068:
069: private Object[] noNulls(Object[] inStr) {
070: for (int i = 0; i < inStr.length; ++i) {
071: if (inStr[i] == null) {
072: inStr[i] = ""; //$NON-NLS-1$
073: }
074: }
075: return (inStr);
076: }
077:
078: /* ILogger Implementation */
079:
080: public String getObjectName() {
081: return this .getClass().getName();
082: }
083:
084: public int getLoggingLevel() {
085: return loggingLevel;
086: }
087:
088: public void setLoggingLevel(int logLevel) {
089: this .loggingLevel = logLevel;
090: }
091:
092: public void trace(String message) {
093: if (loggingLevel <= TRACE) {
094: getLogger().trace(getLogId() + message);
095: }
096: }
097:
098: public void debug(String message) {
099: if (loggingLevel <= DEBUG) {
100: getLogger().debug(getLogId() + message);
101: }
102: }
103:
104: public void info(String message) {
105: if (loggingLevel <= INFO) {
106: getLogger().info(getLogId() + message);
107: }
108: }
109:
110: public void warn(String message) {
111: if (loggingLevel <= WARN) {
112: getLogger().warn(getLogId() + message);
113: }
114: }
115:
116: public void error(String message) {
117: if (loggingLevel <= ERROR) {
118: getLogger().error(getLogId() + message);
119: }
120: }
121:
122: public void fatal(String message) {
123: if (loggingLevel <= FATAL) {
124: getLogger().fatal(getLogId() + message);
125: }
126: }
127:
128: public void trace(String message, Throwable error) {
129: if (loggingLevel <= TRACE) {
130: getLogger().trace(getLogId() + message, error);
131: }
132: }
133:
134: public void debug(String message, Throwable error) {
135: if (loggingLevel <= DEBUG) {
136: getLogger().debug(getLogId() + message, error);
137: }
138: }
139:
140: public void info(String message, Throwable error) {
141: if (loggingLevel <= INFO) {
142: getLogger().info(getLogId() + message, error);
143: }
144: }
145:
146: public void warn(String message, Throwable error) {
147: if (loggingLevel <= WARN) {
148: getLogger().warn(getLogId() + message, error);
149: }
150: }
151:
152: public void error(String message, Throwable error) {
153: if (loggingLevel <= ERROR) {
154: IVersionHelper helper = PentahoSystem
155: .getVersionHelper(null);
156: getLogger()
157: .error(
158: "Error Start: Pentaho " + helper.getVersionInformation()); //$NON-NLS-1$
159: getLogger().error(getLogId() + message, error);
160: getLogger().error("Error end:"); //$NON-NLS-1$
161: }
162: }
163:
164: public void fatal(String message, Throwable error) {
165: if (loggingLevel <= FATAL) {
166: IVersionHelper helper = PentahoSystem
167: .getVersionHelper(null);
168: getLogger().error(
169: "Error: Pentaho " + helper.getVersionInformation()); //$NON-NLS-1$
170: getLogger().fatal(getLogId() + message, error);
171: getLogger().error("Error end:"); //$NON-NLS-1$
172: }
173: }
174:
175: }
|