KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > web > logger > IASLogger


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 package com.sun.enterprise.web.logger;
25
26 import java.util.logging.Logger JavaDoc;
27 import java.util.logging.Level JavaDoc;
28 import com.sun.enterprise.util.logging.IASLevel;
29
30 /**
31  * An implementation of <b>Logger</b> that writes log messages
32  * using JDK 1.4's logging API.
33  *
34  * @author Arvind Srinivasan
35  * @author Neelam Vaidya
36  * @version $Revision: 1.4 $
37  */

38
39 public final class IASLogger extends LoggerBase {
40
41     // ----------------------------------------------------- Instance Variables
42

43     /**
44      * The server wide log message handler.
45      */

46     Logger JavaDoc _logger = null;
47
48     /**
49      * Classname of the object invoking the log method.
50      */

51     private String JavaDoc _classname;
52
53     /**
54      * Name of the method invoking the log method.
55      */

56     private String JavaDoc _methodname;
57     
58     /**
59      * The descriptive information about this implementation.
60      */

61     protected static final String JavaDoc info =
62         "com.sun.enterprise.web.logger.IASLogger/1.0";
63
64
65     // ----------------------------------------------------------- Constructors
66

67     /**
68      * Deny void construction.
69      */

70     private IASLogger() {
71         super();
72     }
73
74     /**
75      * Construct a new instance of this class, that uses the specified
76      * logger instance.
77      *
78      * @param logger The logger to send log messages to
79      */

80     public IASLogger(Logger JavaDoc logger) {
81         _logger = logger;
82     }
83
84     // ------------------------------------------------------ Protected Methods
85

86     /**
87      * Logs the message to the JDK 1.4 logger that handles all log
88      * messages for the iPlanet Application Server.
89      */

90     protected void write(String JavaDoc msg, int verbosity) {
91
92         if (_logger == null)
93             return;
94
95         Level JavaDoc level = Level.INFO;
96
97         if (verbosity == FATAL)
98             level = (Level JavaDoc)IASLevel.FATAL;
99         else if (verbosity == ERROR)
100             level = Level.SEVERE;
101         else if (verbosity == WARNING)
102             level = Level.WARNING;
103         else if (verbosity == INFORMATION)
104             level = Level.INFO;
105         else if (verbosity == DEBUG)
106             level = Level.FINER;
107
108         inferCaller();
109         _logger.logp(level, _classname, _methodname, msg);
110     }
111
112     // ------------------------------------------------------ Private Methods
113

114     /**
115      * Examine the call stack and determine the name of the method and the
116      * name of the class logging the message.
117      */

118     private void inferCaller() {
119         // Get the stack trace.
120
StackTraceElement JavaDoc stack[] = (new Throwable JavaDoc()).getStackTrace();
121         _classname = "";
122         _methodname = "";
123         for (int ix=0; ix < stack.length; ix++) {
124         StackTraceElement JavaDoc frame = stack[ix];
125         _classname = frame.getClassName();
126         if (!_classname.startsWith("com.sun.enterprise.web.logger")) {
127         // We've found the relevant frame. Get Method Name.
128
_methodname = frame.getMethodName();
129         return;
130         }
131         }
132     }
133 }
134
Popular Tags