KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jnlp > sample > servlet > Logger


1 /*
2  * @(#)Logger.java 1.6 05/11/17
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 package jnlp.sample.servlet;
38 import java.text.MessageFormat JavaDoc;
39 import java.util.*;
40 import java.io.*;
41 import javax.servlet.*;
42
43 /* A loging object used by the servlets */
44 public class Logger {
45     // Logging levels
46
public final static int NONE = 0;
47     public static final String JavaDoc NONE_KEY = "NONE";
48     public final static int FATAL = 1;
49     public static final String JavaDoc FATAL_KEY = "FATAL";
50     public final static int WARNING = 2;
51     public static final String JavaDoc WARNING_KEY = "WARNING";
52     public final static int INFORMATIONAL = 3;
53     public static final String JavaDoc INFORMATIONAL_KEY = "INFORMATIONAL";
54     public final static int DEBUG = 4;
55     public static final String JavaDoc DEBUG_KEY = "DEBUG";
56     
57     // Configuration parameters
58
private final static String JavaDoc LOG_LEVEL = "logLevel";
59     private final static String JavaDoc LOG_PATH = "logPath";
60     
61     private int _loggingLevel = FATAL;
62     private ServletContext _servletContext = null;
63     private String JavaDoc _logFile = null;
64     private String JavaDoc _servletName = null;
65     
66     // Localization
67
ResourceBundle _resources = null;
68     
69     
70     /** Initialize logging object. It reads the logLevel and pathLevel init parameters.
71      * Default is logging level FATAL, and logging using the ServletContext.log
72      */

73     public Logger(ServletConfig config, ResourceBundle resources) {
74     _resources = resources;
75     _servletContext = config.getServletContext();
76     _servletName = config.getServletName();
77     _logFile = config.getInitParameter(LOG_PATH);
78     if (_logFile != null) {
79         _logFile = _logFile.trim();
80         if (_logFile.length() == 0) _logFile = null;
81     }
82         String JavaDoc level = config.getInitParameter(LOG_LEVEL);
83     if (level != null) {
84         level = level.trim().toUpperCase();
85         if (level.equals(NONE_KEY)) _loggingLevel = NONE;
86         if (level.equals(FATAL_KEY)) _loggingLevel = FATAL;
87         if (level.equals(WARNING_KEY)) _loggingLevel = WARNING;
88         if (level.equals(INFORMATIONAL_KEY)) _loggingLevel = INFORMATIONAL;
89         if (level.equals(DEBUG_KEY)) _loggingLevel = DEBUG;
90     }
91     }
92     
93     // Logging API. Fatal, Warning, and Informational are localized
94
public void addFatal(String JavaDoc key, Throwable JavaDoc throwable) {
95     logEvent(FATAL, getString(key), throwable);
96     }
97     
98     public void addWarning(String JavaDoc key, String JavaDoc arg) {
99     logL10N(WARNING, key, arg, (Throwable JavaDoc)null);
100     }
101     
102     public void addWarning(String JavaDoc key, String JavaDoc arg, Throwable JavaDoc t) {
103     logL10N(WARNING, key, arg, t);
104     }
105     
106     public void addWarning(String JavaDoc key, String JavaDoc arg1, String JavaDoc arg2) {
107     logL10N(WARNING, key, arg1, arg2);
108     }
109     
110     public void addWarning(String JavaDoc key, String JavaDoc arg1, String JavaDoc arg2, String JavaDoc arg3) {
111     logL10N(WARNING, key, arg1, arg2, arg3);
112     }
113     
114     public void addInformational(String JavaDoc key) {
115     logEvent(INFORMATIONAL, getString(key), (Throwable JavaDoc)null);
116     }
117     
118     public void addInformational(String JavaDoc key, String JavaDoc arg) {
119     logL10N(INFORMATIONAL, key, arg, (Throwable JavaDoc)null);
120     }
121     
122     public void addInformational(String JavaDoc key, String JavaDoc arg1, String JavaDoc arg2, String JavaDoc arg3) {
123     logL10N(INFORMATIONAL, key, arg1, arg2, arg3);
124     }
125     
126     // Debug messages are not localized
127
public void addDebug(String JavaDoc msg) { logEvent(DEBUG, msg, null); }
128     
129     public void addDebug(String JavaDoc msg, Throwable JavaDoc throwable) {
130     logEvent(DEBUG, msg, throwable);
131     }
132     
133     // Query to test for level
134
boolean isNoneLevel() { return _loggingLevel >= NONE; }
135     boolean isFatalevel() { return _loggingLevel >= FATAL; }
136     boolean isWarningLevel() { return _loggingLevel >= WARNING; }
137     boolean isInformationalLevel() { return _loggingLevel >= INFORMATIONAL; }
138     boolean isDebugLevel() { return _loggingLevel >= DEBUG; }
139     
140     // Returns a string from the resources
141
private String JavaDoc getString(String JavaDoc key) {
142         try {
143         return _resources.getString(key);
144         } catch (MissingResourceException mre) {
145         return "Missing resource for: " + key;
146         }
147     }
148     
149     private void logL10N(int level, String JavaDoc key, String JavaDoc arg, Throwable JavaDoc e) {
150     Object JavaDoc[] messageArguments = { arg };
151         logEvent(level, applyPattern(key, messageArguments), e);
152     }
153     
154     private void logL10N(int level, String JavaDoc key, String JavaDoc arg1, String JavaDoc arg2) {
155     Object JavaDoc[] messageArguments = { arg1, arg2 };
156         logEvent(level, applyPattern(key, messageArguments), null);
157     }
158     
159     private void logL10N(int level, String JavaDoc key, String JavaDoc arg1, String JavaDoc arg2, String JavaDoc arg3) {
160     Object JavaDoc[] messageArguments = { arg1, arg2, arg3 };
161         logEvent(level, applyPattern(key, messageArguments), null);
162     }
163     
164     /** Helper function that applies the messageArguments to a message from the resource object */
165     private String JavaDoc applyPattern(String JavaDoc key, Object JavaDoc[] messageArguments) {
166         String JavaDoc message = getString(key);
167         MessageFormat JavaDoc formatter = new MessageFormat JavaDoc(message);
168         String JavaDoc output = formatter.format(message, messageArguments);
169         return output;
170     }
171     
172     // The method that actually does the logging */
173
private synchronized void logEvent(int level, String JavaDoc string, Throwable JavaDoc throwable) {
174     // Check if the event should be logged
175
if (level > _loggingLevel) return;
176     
177     if (_logFile != null) {
178         // No logfile specified, log using servlet context
179
PrintWriter pw = null;
180         try {
181         pw = new PrintWriter(new FileWriter(_logFile, true));
182         pw.println(_servletName + "(" + level + "): " + string);
183         if (throwable != null) {
184             throwable.printStackTrace(pw);
185         }
186         pw.close();
187         // Do a return here. An exception will cause a fall through to
188
// do _servletContex logging API
189
return;
190         } catch (IOException ioe) {
191         /* just ignore */
192         }
193     }
194     
195     // Otherwise, write to servlet context log
196
if (throwable == null) {
197         _servletContext.log(string);
198     } else {
199         _servletContext.log(string, throwable);
200     }
201     }
202 }
203
204
Popular Tags