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.j2seimport;
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 Level LEVEL = Level.INFO;
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: static {
078: String levelName = System
079: .getProperty("projectimport.logging.level"); // NOI18N
080: if (levelName != null) {
081: try {
082: LEVEL = Level.parse(levelName);
083: } catch (IllegalArgumentException iae) {
084: // use default level - INFO
085: }
086: }
087: }
088:
089: /**
090: * Creates logger for the given class.
091: */
092: public Logger createLogger(Class clazz) {
093: Logger logger = Logger.getLogger(clazz.getName());
094: ConsoleHandler ch = new ConsoleHandler();
095: ch.setLevel(LEVEL);
096: ch.setFormatter(new BasicFormatter());
097: logger.addHandler(ch);
098: logger.setUseParentHandlers(false);
099: logger.setLevel(LEVEL);
100: return logger;
101: }
102:
103: private static class BasicFormatter extends Formatter {
104: private Date dat = new Date();
105: private final static String format = "yyyyMMdd_HHmmss z"; // NOI18N
106: private DateFormat formatter = new SimpleDateFormat(format);
107:
108: public String format(LogRecord record) {
109: StringBuffer sb = new StringBuffer();
110: String name = record.getLevel().getName();
111: // indent on the base of severity to have nice and readable output
112: for (int i = name.length(); i < MAX_LEVEL_LENGTH; i++) {
113: sb.append(" "); // NOI18N
114: }
115: // append severity
116: sb.append("[" + name + "]: "); // NOI18N
117: // append date and time (minimize memory allocations here)
118: dat.setTime(record.getMillis());
119: sb.append(formatter.format(dat));
120: sb.append(" - "); // NOI18N
121: // append class and method names
122: if (record.getSourceClassName() != null) {
123: sb.append(record.getSourceClassName());
124: } else {
125: sb.append(record.getLoggerName());
126: }
127: if (record.getSourceMethodName() != null) {
128: sb.append("."); // NOI18N
129: sb.append(record.getSourceMethodName());
130: sb.append("()"); // NOI18N
131: }
132: // append stacktrace if there is any
133: sb.append(": "); // NOI18N
134: sb.append(record.getMessage());
135: if (record.getThrown() != null) {
136: try {
137: sb.append("\n "); // NOI18N
138: StringWriter sw = new StringWriter();
139: PrintWriter pw = new PrintWriter(sw);
140: record.getThrown().printStackTrace(pw);
141: pw.close();
142: sb.append(sw.toString());
143: } catch (Exception ex) {
144: }
145: } else {
146: sb.append("\n"); // NOI18N
147: }
148: return sb.toString();
149: }
150: }
151:
152: }
|