KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > projectimport > LoggerFactory


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.projectimport;
21
22 import java.io.PrintWriter JavaDoc;
23 import java.io.StringWriter JavaDoc;
24 import java.text.DateFormat JavaDoc;
25 import java.text.SimpleDateFormat JavaDoc;
26 import java.util.Date JavaDoc;
27 import java.util.logging.ConsoleHandler JavaDoc;
28 import java.util.logging.Formatter JavaDoc;
29 import java.util.logging.Level JavaDoc;
30 import java.util.logging.LogRecord JavaDoc;
31 import java.util.logging.Logger JavaDoc;
32
33 /**
34  * Logger factory.
35  *
36  * @author mkrauskopf
37  */

38 public class LoggerFactory {
39
40     private static LoggerFactory factory = new LoggerFactory();
41
42     // used logging level adjustable with "projectimport.logging.level" system
43
// property
44
private static final Level JavaDoc LEVEL = readLevel();
45     
46     // TOTO: mkrauskopf - enhance this
47
private static final int MAX_LEVEL_LENGTH = Level.WARNING.getName().length();
48     
49     /** Returns factory instance. */
50     public static LoggerFactory getDefault() {
51         return factory;
52     }
53     
54     private static Level JavaDoc readLevel() {
55         Level JavaDoc level = Level.INFO;
56         String JavaDoc levelName = System.getProperty("projectimport.logging.level"); // NOI18N
57
if (levelName != null) {
58             try {
59                 level = Level.parse(levelName);
60             } catch (IllegalArgumentException JavaDoc iae) {
61                 System.err.println("Unable to parse \"projectimport.logging.level\": " + levelName); // NOI18N
62
// use default level - INFO
63
}
64         }
65         return level;
66     }
67     
68     /**
69      * Creates logger for the given class.
70      */

71     public Logger JavaDoc createLogger(Class JavaDoc clazz) {
72         Logger JavaDoc logger = Logger.getLogger(clazz.getName());
73         ConsoleHandler JavaDoc ch = new ConsoleHandler JavaDoc();
74         ch.setLevel(LEVEL);
75         ch.setFormatter(new BasicFormatter());
76         logger.addHandler(ch);
77         logger.setUseParentHandlers(false);
78         logger.setLevel(LEVEL);
79         return logger;
80     }
81     
82     private static class BasicFormatter extends Formatter JavaDoc {
83         
84         private final Date JavaDoc dat = new Date JavaDoc();
85         private final DateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc("yyyyMMdd_HHmmss z"); // NOI18N
86

87         public String JavaDoc format(LogRecord JavaDoc record) {
88             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
89             String JavaDoc name = record.getLevel().getName();
90             // indent on the base of severity to have nice and readable output
91
for (int i = name.length(); i < MAX_LEVEL_LENGTH; i++) {
92                 sb.append(' ');
93             }
94             // append severity
95
sb.append('[' + name + "]: "); // NOI18N
96
// append date and time (minimize memory allocations here)
97
dat.setTime(record.getMillis());
98             sb.append(formatter.format(dat));
99             sb.append(" - "); // NOI18N
100
// append class and method names
101
if (record.getSourceClassName() != null) {
102                 sb.append(record.getSourceClassName());
103             } else {
104                 sb.append(record.getLoggerName());
105             }
106             if (record.getSourceMethodName() != null) {
107                 sb.append('.');
108                 sb.append(record.getSourceMethodName());
109                 sb.append("()"); // NOI18N
110
}
111             // append stacktrace if there is any
112
sb.append(": "); // NOI18N
113
sb.append(record.getMessage());
114             if (record.getThrown() != null) {
115                 sb.append("\n "); // NOI18N
116
StringWriter JavaDoc sw = new StringWriter JavaDoc();
117                 PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
118                 record.getThrown().printStackTrace(pw);
119                 pw.close();
120                 sb.append(sw.toString());
121             } else {
122                 sb.append('\n');
123             }
124             return sb.toString();
125         }
126         
127     }
128     
129 }
130
Popular Tags