KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > cli > framework > CLILogger


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * LoggerManager.java
26  *
27  * Created on July 29, 2003, 5:11 PM
28  */

29
30 package com.sun.enterprise.cli.framework;
31
32 //imports
33
import java.util.logging.Logger JavaDoc;
34 import java.util.logging.Level JavaDoc;
35 import java.util.logging.Handler JavaDoc;
36 import java.io.ByteArrayOutputStream JavaDoc;
37
38 /**
39  *
40  * @author pa100654
41  */

42 public class CLILogger
43 {
44     
45     private static CLILogger logger;
46     private Logger JavaDoc s1asLogger;
47     private final static String JavaDoc DEBUG_FLAG = "Debug";
48     private final static int kDefaultBufferSize = 512;
49     private final static String JavaDoc PACKAGE_NAME = "com.sun.enterprise.cli.framework";
50     
51     /** Creates a new instance of CLILogger */
52     protected CLILogger()
53     {
54         s1asLogger = Logger.getLogger(PACKAGE_NAME, null);
55         if (System.getProperty(DEBUG_FLAG) != null)
56             s1asLogger.setLevel(Level.FINEST);
57         else
58     {
59             s1asLogger.setLevel(Level.INFO);
60             //s1asLogger.setLevel(Level.SEVERE);
61
}
62         s1asLogger.addHandler(new CLILoggerHandler());
63         s1asLogger.setUseParentHandlers(false);
64     }
65     
66     public static boolean isDebug()
67     {
68         if (System.getProperty(DEBUG_FLAG) != null) {
69             return true;
70         } else {
71             return false;
72         }
73     }
74     
75     /**
76      * returns the instance of the logger
77      */

78     public static CLILogger getInstance()
79     {
80         if (logger == null)
81         {
82             logger = new CLILogger();
83         }
84         return logger;
85     }
86     
87     /**
88      * returns the current output Level
89      * @return Level the java.util.logging.Level
90      */

91     public Level JavaDoc getOutputLevel()
92     {
93         return s1asLogger.getLevel();
94     }
95     
96     /**
97      * Sets the output Level
98      * @param level the java.util.logging.Level
99      */

100     public void setOutputLevel(Level JavaDoc level)
101     {
102         if (System.getProperty(DEBUG_FLAG) == null)
103             s1asLogger.setLevel(level);
104     }
105     
106     /**
107      * prints the message with level as INFO
108      * @param message the message to be written on the output stream
109      */

110     public void printMessage(String JavaDoc message)
111     {
112         s1asLogger.log(Level.INFO, message);
113     }
114     
115     /**
116      * prints the message with level as FINE
117      * @param message the message to be written on the output stream
118      */

119     public void printDetailMessage(String JavaDoc message)
120     {
121         s1asLogger.log(Level.FINE, message);
122     }
123     
124     /**
125      * prints the message with level as WARNING
126      * @param message the message to be written on the output stream
127      */

128     public void printWarning(String JavaDoc message)
129     {
130         s1asLogger.log(Level.WARNING, message);
131     }
132     
133     /**
134      * prints the message with level as SEVERE
135      * @param message the message to be written on the output stream
136      */

137     public void printError(String JavaDoc message)
138     {
139         s1asLogger.log(Level.SEVERE, message);
140     }
141     
142     /**
143      * prints the message with level as FINEST
144      * @param message the message to be written on the output stream
145      */

146     public void printDebugMessage(String JavaDoc message)
147     {
148         s1asLogger.log(Level.FINEST, message);
149     }
150
151
152     /**
153      * prints the exception message with level as FINEST
154      * @param e - the exception object to print
155      */

156     public void printExceptionStackTrace(java.lang.Throwable JavaDoc e)
157     {
158     /*
159     java.lang.StackTraceElement[] ste = e.getStackTrace();
160     for (int ii=0; ii<ste.length; ii++)
161         {
162         printDebugMessage(ste[ii].toString());
163     }
164     */

165         final ByteArrayOutputStream JavaDoc output = new ByteArrayOutputStream JavaDoc( kDefaultBufferSize );
166         e.printStackTrace( new java.io.PrintStream JavaDoc(output));
167         printDebugMessage(output.toString());
168     }
169
170     
171     public class CLILoggerHandler extends Handler JavaDoc
172     {
173
174         /** Creates a new instance of CLILoggerHandler */
175         public CLILoggerHandler()
176         {
177         }
178
179         public void publish(java.util.logging.LogRecord JavaDoc logRecord)
180         {
181             if (logRecord.getLevel() == Level.SEVERE)
182             {
183         InputsAndOutputs.getInstance().getErrorOutput().println(logRecord.getMessage());
184             }
185             else
186             {
187                 InputsAndOutputs.getInstance().getUserOutput().println(logRecord.getMessage());
188             }
189             //for now prints to System.out, fix me
190
}
191
192         public void close() throws java.lang.SecurityException JavaDoc
193         {
194         }
195
196         public void flush()
197         {
198         }
199     }
200
201     public static void main(String JavaDoc[] args)
202     {
203         CLILogger logger = new CLILogger();
204     try
205     {
206         String JavaDoc sLevel = null;
207         // LocalStringsManager lsm = LocalStringsManagerFactory.getLocalStringsManager("com.sun.enterprise.cli.framework", "LocalStrings" );
208
LocalStringsManager lsm = LocalStringsManagerFactory.getFrameworkLocalStringsManager();
209
210         InputsAndOutputs.getInstance().getUserOutput().print(lsm.getString("PROMPT"));
211         sLevel = InputsAndOutputs.getInstance().getUserInput().getLine();
212         logger.setOutputLevel(java.util.logging.Level.parse(sLevel));
213
214         System.out.println("Logger level = " + logger.getOutputLevel());
215         logger.printDetailMessage("Fine");
216         logger.printMessage("Info");
217         logger.printError("Error");
218         logger.printWarning("Warning");
219         logger.printDebugMessage("Debug");
220
221         // test from file
222
InputsAndOutputs.getInstance().setUserOutputFile("UserOutput.txt");
223         InputsAndOutputs.getInstance().setErrorOutputFile("ErrorOutput.txt");
224         InputsAndOutputs.getInstance().setUserInputFile("test_input.txt");
225         InputsAndOutputs.getInstance().getUserOutput().print(lsm.getString("PROMPT"));
226         sLevel = InputsAndOutputs.getInstance().getUserInput().getLine();
227         logger.setOutputLevel(java.util.logging.Level.parse(sLevel));
228         logger.printDetailMessage("Fine");
229         logger.printMessage("Info");
230         logger.printError("Error");
231         logger.printWarning("Warning");
232         logger.printDebugMessage("Debug");
233
234     }
235     catch (Exception JavaDoc e)
236     {
237         logger.printExceptionStackTrace(e);
238         //e.printStackTrace();
239
}
240     }
241 }
242
Popular Tags