001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with 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,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.common.logging;
019:
020: import java.text.MessageFormat;
021: import java.util.ResourceBundle;
022: import java.util.logging.Level;
023: import java.util.logging.LogRecord;
024: import java.util.logging.Logger;
025:
026: import org.apache.cxf.common.i18n.BundleUtils;
027:
028: /**
029: * A container for static utility methods related to logging.
030: */
031: public final class LogUtils {
032:
033: private static final Object[] NO_PARAMETERS = new Object[0];
034:
035: /**
036: * Prevents instantiation.
037: */
038: private LogUtils() {
039: }
040:
041: /**
042: * Get a Logger with the associated default resource bundle for the class.
043: *
044: * @param cls the Class to contain the Logger
045: * @return an appropriate Logger
046: */
047: public static Logger getL7dLogger(Class<?> cls) {
048: return Logger.getLogger(cls.getName(), BundleUtils
049: .getBundleName(cls));
050: }
051:
052: /**
053: * Get a Logger with an associated resource bundle.
054: *
055: * @param cls the Class to contain the Logger
056: * @param name the resource name
057: * @return an appropriate Logger
058: */
059: public static Logger getL7dLogger(Class<?> cls, String name) {
060: return Logger.getLogger(cls.getName(), BundleUtils
061: .getBundleName(cls, name));
062: }
063:
064: /**
065: * Allows both parameter substitution and a typed Throwable to be logged.
066: *
067: * @param logger the Logger the log to
068: * @param level the severity level
069: * @param message the log message
070: * @param throwable the Throwable to log
071: * @param parameter the parameter to substitute into message
072: */
073: public static void log(Logger logger, Level level, String message,
074: Throwable throwable, Object parameter) {
075: if (logger.isLoggable(level)) {
076: final String formattedMessage = MessageFormat.format(
077: localize(logger, message), parameter);
078: doLog(logger, level, formattedMessage, throwable);
079: }
080: }
081:
082: /**
083: * Allows both parameter substitution and a typed Throwable to be logged.
084: *
085: * @param logger the Logger the log to
086: * @param level the severity level
087: * @param message the log message
088: * @param throwable the Throwable to log
089: * @param parameters the parameters to substitute into message
090: */
091: public static void log(Logger logger, Level level, String message,
092: Throwable throwable, Object... parameters) {
093: if (logger.isLoggable(level)) {
094: final String formattedMessage = MessageFormat.format(
095: localize(logger, message), parameters);
096: doLog(logger, level, formattedMessage, throwable);
097: }
098: }
099:
100: /**
101: * Checks log level and logs
102: *
103: * @param logger the Logger the log to
104: * @param level the severity level
105: * @param message the log message
106: */
107: public static void log(Logger logger, Level level, String message) {
108: if (logger.isLoggable(level)) {
109: final String formattedMessage = MessageFormat.format(
110: localize(logger, message), NO_PARAMETERS);
111: doLog(logger, level, formattedMessage, null);
112: }
113:
114: }
115:
116: /**
117: * Checks log level and logs
118: *
119: * @param logger the Logger the log to
120: * @param level the severity level
121: * @param message the log message
122: * @param throwable the Throwable to log
123: */
124: public static void log(Logger logger, Level level, String message,
125: Throwable throwable) {
126: log(logger, level, message, throwable, NO_PARAMETERS);
127: }
128:
129: /**
130: * Checks log level and logs
131: *
132: * @param logger the Logger the log to
133: * @param level the severity level
134: * @param message the log message
135: * @param parameter the parameter to substitute into message
136: */
137: public static void log(Logger logger, Level level, String message,
138: Object parameter) {
139: log(logger, level, message, new Object[] { parameter });
140: }
141:
142: /**
143: * Checks log level and logs
144: *
145: * @param logger the Logger the log to
146: * @param level the severity level
147: * @param message the log message
148: * @param parameters the parameters to substitute into message
149: */
150: public static void log(Logger logger, Level level, String message,
151: Object[] parameters) {
152: if (logger.isLoggable(level)) {
153: final String formattedMessage = MessageFormat.format(
154: localize(logger, message), parameters);
155: doLog(logger, level, formattedMessage, null);
156: }
157:
158: }
159:
160: private static void doLog(Logger log, Level level, String msg,
161: Throwable t) {
162: LogRecord record = new LogRecord(level, msg);
163:
164: record.setLoggerName(log.getName());
165: record.setResourceBundleName(log.getResourceBundleName());
166: record.setResourceBundle(log.getResourceBundle());
167:
168: if (t != null) {
169: record.setThrown(t);
170: }
171:
172: //try to get the right class name/method name - just trace
173: //back the stack till we get out of this class
174: StackTraceElement stack[] = (new Throwable()).getStackTrace();
175: String cname = LogUtils.class.getName();
176: for (int x = 0; x < stack.length; x++) {
177: StackTraceElement frame = stack[x];
178: if (!frame.getClassName().equals(cname)) {
179: record.setSourceClassName(frame.getClassName());
180: record.setSourceMethodName(frame.getMethodName());
181: break;
182: }
183: }
184: log.log(record);
185: }
186:
187: /**
188: * Retreive localized message retreived from a logger's resource
189: * bundle.
190: *
191: * @param logger the Logger
192: * @param message the message to be localized
193: */
194: private static String localize(Logger logger, String message) {
195: ResourceBundle bundle = logger.getResourceBundle();
196: return bundle != null ? bundle.getString(message) : message;
197: }
198:
199: }
|