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-2006 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 org.netbeans.modules.projectimport;
043:
044: import java.io.PrintWriter;
045: import java.io.StringWriter;
046: import java.text.DateFormat;
047: import java.text.SimpleDateFormat;
048: import java.util.Date;
049: import java.util.logging.ConsoleHandler;
050: import java.util.logging.Formatter;
051: import java.util.logging.Level;
052: import java.util.logging.LogRecord;
053: import java.util.logging.Logger;
054:
055: /**
056: * Logger factory.
057: *
058: * @author mkrauskopf
059: */
060: public class LoggerFactory {
061:
062: private static LoggerFactory factory = new LoggerFactory();
063:
064: // used logging level adjustable with "projectimport.logging.level" system
065: // property
066: private static final Level LEVEL = readLevel();
067:
068: // TOTO: mkrauskopf - enhance this
069: private static final int MAX_LEVEL_LENGTH = Level.WARNING.getName()
070: .length();
071:
072: /** Returns factory instance. */
073: public static LoggerFactory getDefault() {
074: return factory;
075: }
076:
077: private static Level readLevel() {
078: Level level = Level.INFO;
079: String levelName = System
080: .getProperty("projectimport.logging.level"); // NOI18N
081: if (levelName != null) {
082: try {
083: level = Level.parse(levelName);
084: } catch (IllegalArgumentException iae) {
085: System.err
086: .println("Unable to parse \"projectimport.logging.level\": "
087: + levelName); // NOI18N
088: // use default level - INFO
089: }
090: }
091: return level;
092: }
093:
094: /**
095: * Creates logger for the given class.
096: */
097: public Logger createLogger(Class clazz) {
098: Logger logger = Logger.getLogger(clazz.getName());
099: ConsoleHandler ch = new ConsoleHandler();
100: ch.setLevel(LEVEL);
101: ch.setFormatter(new BasicFormatter());
102: logger.addHandler(ch);
103: logger.setUseParentHandlers(false);
104: logger.setLevel(LEVEL);
105: return logger;
106: }
107:
108: private static class BasicFormatter extends Formatter {
109:
110: private final Date dat = new Date();
111: private final DateFormat formatter = new SimpleDateFormat(
112: "yyyyMMdd_HHmmss z"); // NOI18N
113:
114: public String format(LogRecord record) {
115: StringBuffer sb = new StringBuffer();
116: String name = record.getLevel().getName();
117: // indent on the base of severity to have nice and readable output
118: for (int i = name.length(); i < MAX_LEVEL_LENGTH; i++) {
119: sb.append(' ');
120: }
121: // append severity
122: sb.append('[' + name + "]: "); // NOI18N
123: // append date and time (minimize memory allocations here)
124: dat.setTime(record.getMillis());
125: sb.append(formatter.format(dat));
126: sb.append(" - "); // NOI18N
127: // append class and method names
128: if (record.getSourceClassName() != null) {
129: sb.append(record.getSourceClassName());
130: } else {
131: sb.append(record.getLoggerName());
132: }
133: if (record.getSourceMethodName() != null) {
134: sb.append('.');
135: sb.append(record.getSourceMethodName());
136: sb.append("()"); // NOI18N
137: }
138: // append stacktrace if there is any
139: sb.append(": "); // NOI18N
140: sb.append(record.getMessage());
141: if (record.getThrown() != null) {
142: sb.append("\n "); // NOI18N
143: StringWriter sw = new StringWriter();
144: PrintWriter pw = new PrintWriter(sw);
145: record.getThrown().printStackTrace(pw);
146: pw.close();
147: sb.append(sw.toString());
148: } else {
149: sb.append('\n');
150: }
151: return sb.toString();
152: }
153:
154: }
155:
156: }
|