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.sql.rowset;
042:
043: import java.util.logging.ConsoleHandler;
044: import java.util.logging.Formatter;
045: import java.util.logging.Handler;
046: import java.util.logging.Level;
047: import java.util.logging.Logger;
048: import java.util.logging.LogManager;
049: import java.util.logging.LogRecord;
050: import java.util.Locale;
051: import java.util.ResourceBundle;
052:
053: /**
054: * Log utility for Sun's rowset extensions
055: *
056: */
057: public class Log {
058:
059: private static final String DEFAULT_NAME = "SEVERE"; //NOI18N
060: private static final Level DEFAULT = Level.SEVERE;
061: private static final String OFF_NAME = "OFF"; //NOI18N
062: private static final Level OFF_LEVEL = Level.OFF;
063: private static final String[] LEVEL_NAMES = { "OFF", "SEVERE",
064: "WARNING", "INFO", //NOI18N
065: "CONFIG", "FINE", "FINER", "FINEST" }; //NOI18N
066: private static final Level[] LEVELS = { Level.OFF, Level.SEVERE,
067: Level.WARNING, Level.INFO, Level.CONFIG, Level.FINE,
068: Level.FINER, Level.FINEST };
069:
070: private static Log log = null;
071:
072: private Logger logger = null;
073: private Level level = DEFAULT;
074: private String packageName;
075:
076: private static ResourceBundle rb = ResourceBundle.getBundle(
077: "com.sun.sql.rowset.Bundle", // NOI18N
078: Locale.getDefault());
079:
080: Log(String packageName) {
081: this .packageName = packageName;
082: }
083:
084: private Logger getPackageLogger() {
085: if (logger == null) {
086: String prop = System.getProperty(packageName, DEFAULT_NAME);
087: for (int i = 1; i < LEVELS.length; i++) {
088: if (prop.toLowerCase().equals(
089: LEVEL_NAMES[i].toLowerCase())) {
090: level = LEVELS[i];
091: break;
092: }
093: }
094: LogManager.getLogManager().addLogger(
095: new Logger(packageName, null) {
096: });
097: logger = LogManager.getLogManager().getLogger(packageName);
098: if (logger == null) {
099: System.out.println(packageName + ": "
100: + rb.getString("CANT_GET_LOGGER"));
101: return Logger.getLogger("global");
102: }
103: try {
104: logger.setLevel(level);
105: Handler handler = new ConsoleHandler();
106: handler.setLevel(level);
107: Formatter formatter = new Formatter() {
108: public String format(LogRecord record) {
109: StringBuffer s = new StringBuffer();
110: s.append("[#|");
111: s
112: .append(new java.util.Date(record
113: .getMillis()));
114: s.append('|');
115: s.append(record.getSequenceNumber());
116: s.append('|');
117: s.append(record.getLevel().getLocalizedName());
118: s.append('|');
119: s.append(record.getThreadID());
120: if (record.getLoggerName() != null) {
121: s.append('|');
122: s.append(record.getLoggerName());
123: }
124: if (record.getSourceClassName() != null) {
125: s.append('|');
126: s.append(record.getSourceClassName());
127: }
128: if (record.getSourceMethodName() != null) {
129: s.append('|');
130: s.append(record.getSourceMethodName());
131: s.append('(');
132: Object[] parms = record.getParameters();
133: if (parms != null) {
134: for (int i = 0; i < parms.length; i++) {
135: if (i != 0) {
136: s.append(',');
137: }
138: s.append(parms[i]);
139: }
140: }
141: s.append(')');
142: }
143: if (record.getThrown() != null) {
144: s.append('|');
145: s.append(record.getThrown());
146: }
147: if (record.getMessage() != null) {
148: s.append('|');
149: s.append(record.getMessage());
150: }
151: s.append("|#]\n");
152: return s.toString();
153: }
154: };
155: handler.setFormatter(formatter);
156: logger.addHandler(handler);
157: } catch (SecurityException e) {
158: }
159: }
160:
161: return logger;
162: }
163:
164: static Logger getLogger() {
165: if (log == null) {
166: log = new Log(Log.class.getPackage().getName());
167: }
168: return log.getPackageLogger();
169: }
170:
171: }
|