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:
042: package com.sun.rave.faces.util;
043:
044: import java.io.IOException;
045: import java.io.InputStream;
046: import java.util.HashMap;
047: import java.util.logging.Level;
048: import java.util.logging.LogManager;
049: import java.util.logging.Logger;
050:
051: /**
052: * @author eric
053: *
054: * TODO To change the template for this generated type comment go to
055: * Window - Preferences - Java - Code Generation - Code and Comments
056: */
057: public class LoggerUtil {
058:
059: protected static HashMap loggerUtils;
060:
061: protected Logger logger;
062:
063: static {
064: loggerUtils = new HashMap();
065: InputStream in = LoggerUtil.class
066: .getResourceAsStream("/local-logging.properties"); // NOI18N
067: if (in == null) {
068: in = LoggerUtil.class
069: .getResourceAsStream("/logging.properties"); // NOI18N
070: }
071: if (in != null) {
072: /*
073: * Commented out until I can figure out how to get the configuration I want without slamming everyone else
074: try {
075: LogManager.getLogManager().readConfiguration(in);
076: } catch (IOException e) {
077: } finally {
078: try {in.close();} catch (IOException e) {};
079: in= null;
080: }
081: */
082: }
083:
084: }
085:
086: public static LoggerUtil getLogger(String loggerName) {
087: LoggerUtil loggerUtil = (LoggerUtil) loggerUtils
088: .get(loggerName);
089: if (loggerUtil == null) {
090: loggerUtil = new LoggerUtil(Logger.getLogger(loggerName));
091: loggerUtils.put(loggerName, loggerUtil);
092: }
093: return loggerUtil;
094: }
095:
096: protected LoggerUtil(Logger logger) {
097:
098: this .logger = logger;
099: }
100:
101: public boolean config(String message) {
102:
103: log(Level.CONFIG, message, null);
104: return true;
105: }
106:
107: public boolean config(String message, Throwable throwable) {
108:
109: log(Level.CONFIG, message, throwable);
110: return true;
111: }
112:
113: protected String[] inferCaller() {
114: String lookFor = "com.sun.jsfcl.util.LoggerUtil";
115: // Get the stack trace.
116: StackTraceElement[] stack = (new Throwable()).getStackTrace();
117: // First, search back to a method in the Logger class.
118: int ix = 0;
119: while (ix < stack.length) {
120: StackTraceElement frame = stack[ix];
121: String cname = frame.getClassName();
122: if (cname.equals(lookFor)) {
123: break;
124: }
125: ix++;
126: }
127: // Now search for the first frame before the "Logger" class.
128: while (ix < stack.length) {
129: StackTraceElement frame = stack[ix];
130: String cname = frame.getClassName();
131: if (!cname.equals(lookFor)) {
132: // We've found the relevant frame.
133: return new String[] { cname, frame.getMethodName() };
134: }
135: ix++;
136: }
137: // We haven't found a suitable frame, so just punt. This is
138: // OK as we are only commited to making a "best effort" here.
139: return new String[2];
140: }
141:
142: public boolean info(String message) {
143:
144: log(Level.INFO, message, null);
145: return true;
146: }
147:
148: public boolean info(String message, Throwable throwable) {
149:
150: log(Level.INFO, message, throwable);
151: return true;
152: }
153:
154: public boolean log(Level level, String message, Throwable throwable) {
155: if (!logger.isLoggable(level)) {
156: return true;
157: }
158: String[] inferedCaller = inferCaller();
159: logger.logp(level, inferedCaller[0], inferedCaller[1], message,
160: throwable);
161: return true;
162: }
163:
164: public boolean severe(String message) {
165:
166: log(Level.SEVERE, message, null);
167: return true;
168: }
169:
170: public boolean severe(String message, Throwable throwable) {
171:
172: log(Level.SEVERE, message, throwable);
173: return true;
174: }
175:
176: public boolean warning(String message) {
177:
178: log(Level.WARNING, message, null);
179: return true;
180: }
181:
182: public boolean warning(String message, Throwable throwable) {
183:
184: log(Level.WARNING, message, throwable);
185: return true;
186: }
187:
188: }
|