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 org.netbeans.modules.visualweb.dataconnectivity.naming;
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: * Abstract class to be used by any package to create a Log Utility
055: *
056: * @author John Kline
057: *
058: */
059: public abstract class LogBase {
060:
061: private static final String DEFAULT_NAME = "SEVERE"; //NOI18N
062: private static final Level DEFAULT = Level.SEVERE;
063: private static final String OFF_NAME = "OFF"; //NOI18N
064: private static final Level OFF_LEVEL = Level.OFF;
065: private static final String[] LEVEL_NAMES = { "OFF", "SEVERE",
066: "WARNING", "INFO", //NOI18N
067: "CONFIG", "FINE", "FINER", "FINEST" }; //NOI18N
068: private static final Level[] LEVELS = { Level.OFF, Level.SEVERE,
069: Level.WARNING, Level.INFO, Level.CONFIG, Level.FINE,
070: Level.FINER, Level.FINEST };
071:
072: private Logger logger = null;
073: private Level level = DEFAULT;
074: private String packageName;
075:
076: private static ResourceBundle rb = ResourceBundle
077: .getBundle(
078: "org.netbeans.modules.visualweb.dataconnectivity.naming.Bundle", // NOI18N
079: Locale.getDefault());
080:
081: protected LogBase(String packageName) {
082: this .packageName = packageName;
083: }
084:
085: protected Logger getPackageLogger() {
086: if (logger == null) {
087: String prop = System.getProperty(packageName, DEFAULT_NAME);
088: for (int i = 1; i < LEVELS.length; i++) {
089: if (prop.toLowerCase().equals(
090: LEVEL_NAMES[i].toLowerCase())) {
091: level = LEVELS[i];
092: break;
093: }
094: }
095: LogManager.getLogManager().addLogger(
096: new Logger(packageName, null) {
097: });
098: logger = LogManager.getLogManager().getLogger(packageName);
099: if (logger == null) {
100: System.out.println(packageName + ": "
101: + rb.getString("CANT_GET_LOGGER"));
102: return Logger.getLogger("global");
103: }
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.append(new java.util.Date(record.getMillis()));
112: s.append('|');
113: s.append(record.getSequenceNumber());
114: s.append('|');
115: s.append(record.getLevel().getLocalizedName());
116: s.append('|');
117: s.append(record.getThreadID());
118: if (record.getLoggerName() != null) {
119: s.append('|');
120: s.append(record.getLoggerName());
121: }
122: if (record.getSourceClassName() != null) {
123: s.append('|');
124: s.append(record.getSourceClassName());
125: }
126: if (record.getSourceMethodName() != null) {
127: s.append('|');
128: s.append(record.getSourceMethodName());
129: s.append('(');
130: Object[] parms = record.getParameters();
131: if (parms != null) {
132: for (int i = 0; i < parms.length; i++) {
133: if (i != 0) {
134: s.append(',');
135: }
136: s.append(parms[i]);
137: }
138: }
139: s.append(')');
140: }
141: if (record.getThrown() != null) {
142: s.append('|');
143: s.append(record.getThrown());
144: }
145: if (record.getMessage() != null) {
146: s.append('|');
147: s.append(record.getMessage());
148: }
149: s.append("|#]\n");
150: return s.toString();
151: }
152: };
153: handler.setFormatter(formatter);
154: logger.addHandler(handler);
155: }
156: return logger;
157: }
158: }
|