001: /**
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package org.apache.openejb.util;
018:
019: import java.util.logging.Level;
020: import java.util.logging.LogRecord;
021: import java.util.logging.Logger;
022:
023: public class JuliLogStream implements LogStream {
024: protected Logger logger;
025:
026: public JuliLogStream(LogCategory logCategory) {
027: logger = Logger.getLogger(logCategory.getName());
028: }
029:
030: public boolean isFatalEnabled() {
031: return logger.isLoggable(Level.SEVERE);
032: }
033:
034: public void fatal(String message) {
035: log(Level.SEVERE, message, null);
036: }
037:
038: public void fatal(String message, Throwable t) {
039: log(Level.SEVERE, message, t);
040: }
041:
042: public boolean isErrorEnabled() {
043: return logger.isLoggable(Level.SEVERE);
044: }
045:
046: public void error(String message) {
047: log(Level.SEVERE, message, null);
048: }
049:
050: public void error(String message, Throwable t) {
051: log(Level.SEVERE, message, t);
052: }
053:
054: public boolean isWarnEnabled() {
055: return logger.isLoggable(Level.WARNING);
056: }
057:
058: public void warn(String message) {
059: log(Level.WARNING, message, null);
060: }
061:
062: public void warn(String message, Throwable t) {
063: log(Level.WARNING, message, t);
064: }
065:
066: public boolean isInfoEnabled() {
067: return logger.isLoggable(Level.INFO);
068: }
069:
070: public void info(String message) {
071: log(Level.INFO, message, null);
072: }
073:
074: public void info(String message, Throwable t) {
075: log(Level.INFO, message, t);
076: }
077:
078: public boolean isDebugEnabled() {
079: return logger.isLoggable(Level.FINE);
080: }
081:
082: public void debug(String message) {
083: log(Level.FINE, message, null);
084: }
085:
086: public void debug(String message, Throwable t) {
087: log(Level.FINE, message, t);
088: }
089:
090: private void log(Level level, String message, Throwable t) {
091: if (logger.isLoggable(level)) {
092: LogRecord logRecord = new OpenEJBLogRecord(level, message);
093: if (t != null)
094: logRecord.setThrown(t);
095: logger.log(logRecord);
096: }
097: }
098:
099: private static class OpenEJBLogRecord extends LogRecord {
100: /**
101: * The name of the class that issued the logging call.
102: *
103: * @serial
104: */
105: private String sourceClassName;
106:
107: /**
108: * The name of the method that issued the logging call.
109: *
110: * @serial
111: */
112: private String sourceMethodName;
113:
114: // If the source method and source class has been inited
115: private transient boolean sourceInited;
116:
117: public OpenEJBLogRecord(Level level, String message) {
118: super (level, message);
119: sourceInited = false;
120: }
121:
122: /**
123: * Gets the name of the class that issued the logging call.
124: *
125: * @return the name of the class that issued the logging call
126: */
127: public String getSourceClassName() {
128: initSource();
129: return sourceClassName;
130: }
131:
132: /**
133: * Sets the name of the class that issued the logging call.
134: *
135: * @param sourceClassName
136: * the name of the class that issued the logging call
137: */
138: public void setSourceClassName(String sourceClassName) {
139: sourceInited = true;
140: this .sourceClassName = sourceClassName;
141: }
142:
143: /**
144: * Gets the name of the method that issued the logging call.
145: *
146: * @return the name of the method that issued the logging call
147: */
148: public String getSourceMethodName() {
149: initSource();
150: return sourceMethodName;
151: }
152:
153: /**
154: * Sets the name of the method that issued the logging call.
155: *
156: * @param sourceMethodName the name of the method that issued the logging call
157: */
158: public void setSourceMethodName(String sourceMethodName) {
159: sourceInited = true;
160: this .sourceMethodName = sourceMethodName;
161: }
162:
163: /**
164: * Init the sourceClass and sourceMethod fields.
165: */
166: private void initSource() {
167: if (!sourceInited) {
168: // search back up the stack for the first use of the OpenEJB Logger
169: StackTraceElement[] elements = (new Throwable())
170: .getStackTrace();
171: int i = 0;
172: String current = null;
173: for (; i < elements.length; i++) {
174: current = elements[i].getClassName();
175: if (current
176: .equals(org.apache.openejb.util.Logger.class
177: .getName())) {
178: break;
179: }
180: }
181:
182: // Skip any internal OpenEJB Logger call
183: while (++i < elements.length
184: && elements[i].getClassName().equals(current)) {
185: // do nothing
186: }
187:
188: // If we didn't run out of elements, set the source
189: if (i < elements.length) {
190: this .sourceClassName = elements[i].getClassName();
191: this .sourceMethodName = elements[i].getMethodName();
192: }
193: sourceInited = true;
194: }
195: }
196: }
197: }
|