001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package com.sun.jsfcl.util;
042:
043: import java.io.IOException;
044: import java.io.InputStream;
045: import java.util.HashMap;
046: import java.util.logging.Level;
047: import java.util.logging.LogManager;
048: import java.util.logging.Logger;
049:
050: /**
051: * @author eric
052: *
053: * TODO To change the template for this generated type comment go to
054: * Window - Preferences - Java - Code Generation - Code and Comments
055: */
056: public class LoggerUtil {
057:
058: protected static HashMap loggerUtils;
059:
060: protected Logger logger;
061:
062: static {
063: loggerUtils = new HashMap();
064: InputStream in = LoggerUtil.class
065: .getResourceAsStream("/local-logging.properties"); // NOI18N
066: if (in == null) {
067: in = LoggerUtil.class
068: .getResourceAsStream("/logging.properties"); // NOI18N
069: }
070: if (in != null) {
071: /*
072: * Commented out until I can figure out how to get the configuration I want without slamming everyone else
073: try {
074: LogManager.getLogManager().readConfiguration(in);
075: } catch (IOException e) {
076: } finally {
077: try {in.close();} catch (IOException e) {};
078: in= null;
079: }
080: */
081: }
082:
083: }
084:
085: public static LoggerUtil getLogger(String loggerName) {
086: LoggerUtil loggerUtil = (LoggerUtil) loggerUtils
087: .get(loggerName);
088: if (loggerUtil == null) {
089: loggerUtil = new LoggerUtil(Logger.getLogger(loggerName));
090: loggerUtils.put(loggerName, loggerUtil);
091: }
092: return loggerUtil;
093: }
094:
095: protected LoggerUtil(Logger logger) {
096:
097: this .logger = logger;
098: }
099:
100: public boolean config(String message) {
101:
102: log(Level.CONFIG, message, null);
103: return true;
104: }
105:
106: public boolean config(String message, Throwable throwable) {
107:
108: log(Level.CONFIG, message, throwable);
109: return true;
110: }
111:
112: protected String[] inferCaller() {
113: String lookFor = "com.sun.jsfcl.util.LoggerUtil";
114: // Get the stack trace.
115: StackTraceElement[] stack = (new Throwable()).getStackTrace();
116: // First, search back to a method in the Logger class.
117: int ix = 0;
118: while (ix < stack.length) {
119: StackTraceElement frame = stack[ix];
120: String cname = frame.getClassName();
121: if (cname.equals(lookFor)) {
122: break;
123: }
124: ix++;
125: }
126: // Now search for the first frame before the "Logger" class.
127: while (ix < stack.length) {
128: StackTraceElement frame = stack[ix];
129: String cname = frame.getClassName();
130: if (!cname.equals(lookFor)) {
131: // We've found the relevant frame.
132: return new String[] { cname, frame.getMethodName() };
133: }
134: ix++;
135: }
136: // We haven't found a suitable frame, so just punt. This is
137: // OK as we are only commited to making a "best effort" here.
138: return new String[2];
139: }
140:
141: public boolean info(String message) {
142:
143: log(Level.INFO, message, null);
144: return true;
145: }
146:
147: public boolean info(String message, Throwable throwable) {
148:
149: log(Level.INFO, message, throwable);
150: return true;
151: }
152:
153: public boolean log(Level level, String message, Throwable throwable) {
154: if (!logger.isLoggable(level)) {
155: return true;
156: }
157: String[] inferedCaller = inferCaller();
158: logger.logp(level, inferedCaller[0], inferedCaller[1], message,
159: throwable);
160: return true;
161: }
162:
163: public boolean severe(String message) {
164:
165: log(Level.SEVERE, message, null);
166: return true;
167: }
168:
169: public boolean severe(String message, Throwable throwable) {
170:
171: log(Level.SEVERE, message, throwable);
172: return true;
173: }
174:
175: public boolean warning(String message) {
176:
177: log(Level.WARNING, message, null);
178: return true;
179: }
180:
181: public boolean warning(String message, Throwable throwable) {
182:
183: log(Level.WARNING, message, throwable);
184: return true;
185: }
186:
187: }
|