001: /*
002: * @(#)Log4jLog.java
003: *
004: * Copyright (C) 2002-2003 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Part of the GroboUtils package at:
009: * http://groboutils.sourceforge.net
010: *
011: * Permission is hereby granted, free of charge, to any person obtaining a
012: * copy of this software and associated documentation files (the "Software"),
013: * to deal in the Software without restriction, including without limitation
014: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
015: * and/or sell copies of the Software, and to permit persons to whom the
016: * Software is furnished to do so, subject to the following conditions:
017: *
018: * The above copyright notice and this permission notice shall be included in
019: * all copies or substantial portions of the Software.
020: *
021: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
022: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
023: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
024: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
025: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
026: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
027: * DEALINGS IN THE SOFTWARE.
028: */
029: package net.sourceforge.groboutils.autodoc.v1.log4j;
030:
031: import net.sourceforge.groboutils.autodoc.v1.AutoDocLog;
032:
033: import org.apache.log4j.Logger;
034: import org.apache.log4j.Priority;
035:
036: /**
037: * An interface for logging. This allows for an abstraction between the
038: * owning class and any underlying logging mechanism desired.
039: * <P>
040: * The actual meaning of the logging levels is implementation independent.
041: *
042: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
043: * @version $Date: 2003/02/10 22:52:12 $
044: * @since March 16, 2002
045: */
046: public class Log4jLog implements AutoDocLog {
047: private Logger log;
048:
049: /**
050: * Standard constructor - uses the Logger based on the given class.
051: *
052: * @param owner the owning class to assign log responsibility to.
053: * Must not be <tt>null</tt>.
054: * @exception IllegalArgumentException if <tt>owner</tt> is <tt>null</tt>.
055: */
056: public Log4jLog(Class owner) {
057: if (owner == null) {
058: throw new IllegalArgumentException("no null arguments");
059: }
060: setLog(Logger.getLogger(owner));
061: }
062:
063: /**
064: * Create a log interface based on the given Apache Logger.
065: *
066: * @param log the underlying log instance to use for logging. Must not
067: * be <tt>null</tt>.
068: * @exception IllegalArgumentException if <tt>owner</tt> is <tt>null</tt>.
069: */
070: public Log4jLog(Logger log) {
071: setLog(log);
072: }
073:
074: public void debug(Object message) {
075: this .log.debug(message);
076: }
077:
078: public void debug(Object message[]) {
079: if (this .log.isDebugEnabled()) {
080: this .log.debug(concatMessage(message));
081: }
082: }
083:
084: public void debug(Object message, Throwable error) {
085: this .log.debug(message, error);
086: }
087:
088: public void debug(Object message[], Throwable error) {
089: if (this .log.isDebugEnabled()) {
090: this .log.debug(concatMessage(message), error);
091: }
092: }
093:
094: public void info(Object message) {
095: this .log.info(message);
096: }
097:
098: public void info(Object message[]) {
099: if (this .log.isInfoEnabled()) {
100: this .log.info(concatMessage(message));
101: }
102: }
103:
104: public void info(Object message, Throwable error) {
105: this .log.info(message, error);
106: }
107:
108: public void info(Object message[], Throwable error) {
109: if (this .log.isInfoEnabled()) {
110: Object msg = concatMessage(message);
111: this .log.info(msg, error);
112: }
113: }
114:
115: public void warn(Object message) {
116: this .log.warn(message);
117: }
118:
119: public void warn(Object message[]) {
120: if (this .log.isEnabledFor(Priority.WARN)) {
121: this .log.warn(concatMessage(message));
122: }
123: }
124:
125: public void warn(Object message, Throwable error) {
126: this .log.warn(message, error);
127: }
128:
129: public void warn(Object message[], Throwable error) {
130: if (this .log.isEnabledFor(Priority.WARN)) {
131: this .log.warn(concatMessage(message), error);
132: }
133: }
134:
135: /**
136: * Concatenate the given array into a single string. The returned object
137: * will be of type <tt>java.lang.StringBuffer</tt> or
138: * <tt>java.lang.String</tt>; use <tt>toString()</tt> on the resulting
139: * object.
140: *
141: * @param o an array (possibly null) of objects (possibly null) to
142: * concatenate their <tt>toString()</tt> results together.
143: * @return the concatenated message.
144: * @see java.lang.StringBuffer
145: */
146: protected Object concatMessage(Object o[]) {
147: if (o == null) {
148: return "null";
149: }
150: if (o.length <= 0) {
151: return "";
152: }
153: StringBuffer sb = new StringBuffer();
154: for (int i = 0; i < o.length; ++i) {
155: sb.append(o[i]);
156: }
157: return sb;
158: }
159:
160: /**
161: * Sets the internal log instance.
162: *
163: * @param log the log to set. It must not be <tt>null</tt>.
164: * @exception IllegalArgumentException if <tt>log</tt> is <tt>null</tt>.
165: */
166: protected void setLog(Logger log) {
167: if (log == null) {
168: throw new IllegalArgumentException("no null arguments");
169: }
170: this.log = log;
171: }
172: }
|