KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > projectimport > j2seimport > 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.j2seimport;
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 Level JavaDoc LEVEL = Level.INFO;
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     static {
55         String JavaDoc levelName = System.getProperty("projectimport.logging.level"); // NOI18N
56
if (levelName != null) {
57             try {
58                 LEVEL = Level.parse(levelName);
59             } catch (IllegalArgumentException JavaDoc iae) {
60                 // use default level - INFO
61
}
62         }
63     }
64     
65     /**
66      * Creates logger for the given class.
67      */

68     public Logger JavaDoc createLogger(Class JavaDoc clazz) {
69         Logger JavaDoc logger = Logger.getLogger(clazz.getName());
70         ConsoleHandler JavaDoc ch = new ConsoleHandler JavaDoc();
71         ch.setLevel(LEVEL);
72         ch.setFormatter(new BasicFormatter());
73         logger.addHandler(ch);
74         logger.setUseParentHandlers(false);
75         logger.setLevel(LEVEL);
76         return logger;
77     }
78     
79     
80     private static class BasicFormatter extends Formatter JavaDoc {
81         private Date JavaDoc dat = new Date JavaDoc();
82         private final static String JavaDoc format = "yyyyMMdd_HHmmss z"; // NOI18N
83
private DateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc(format);
84         
85         public String JavaDoc format(LogRecord JavaDoc record) {
86             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
87             String JavaDoc name = record.getLevel().getName();
88             // indent on the base of severity to have nice and readable output
89
for (int i = name.length(); i < MAX_LEVEL_LENGTH; i++) {
90                 sb.append(" "); // NOI18N
91
}
92             // append severity
93
sb.append("[" + name + "]: "); // NOI18N
94
// append date and time (minimize memory allocations here)
95
dat.setTime(record.getMillis());
96             sb.append(formatter.format(dat));
97             sb.append(" - "); // NOI18N
98
// append class and method names
99
if (record.getSourceClassName() != null) {
100                 sb.append(record.getSourceClassName());
101             } else {
102                 sb.append(record.getLoggerName());
103             }
104             if (record.getSourceMethodName() != null) {
105                 sb.append("."); // NOI18N
106
sb.append(record.getSourceMethodName());
107                 sb.append("()"); // NOI18N
108
}
109             // append stacktrace if there is any
110
sb.append(": "); // NOI18N
111
sb.append(record.getMessage());
112             if (record.getThrown() != null) {
113                 try {
114                     sb.append("\n "); // NOI18N
115
StringWriter JavaDoc sw = new StringWriter JavaDoc();
116                     PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
117                     record.getThrown().printStackTrace(pw);
118                     pw.close();
119                     sb.append(sw.toString());
120                 } catch (Exception JavaDoc ex) {
121                 }
122             } else {
123                 sb.append("\n"); // NOI18N
124
}
125             return sb.toString();
126         }
127     }
128     
129 }
130
Popular Tags