001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.util;
019:
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022:
023: import java.util.*;
024:
025: /**
026: * A logger class that strives to make it as easy as possible for
027: * developers to write log calls, while simultaneously making those
028: * calls as cheap as possible by performing lazy evaluation of the log
029: * message.<p>
030: *
031: * @author Marc Johnson (mjohnson at apache dot org)
032: * @author Glen Stampoultzis (glens at apache.org)
033: * @author Nicola Ken Barozzi (nicolaken at apache.org)
034: */
035:
036: public class CommonsLogger extends POILogger {
037:
038: private static LogFactory _creator = LogFactory.getFactory();
039: private Log log = null;
040:
041: public void initialize(final String cat) {
042: this .log = _creator.getInstance(cat);
043: }
044:
045: /**
046: * Log a message
047: *
048: * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
049: * @param obj1 The object to log.
050: */
051:
052: public void log(final int level, final Object obj1) {
053: if (level == FATAL) {
054: if (log.isFatalEnabled()) {
055: log.fatal(obj1);
056: }
057: } else if (level == ERROR) {
058: if (log.isErrorEnabled()) {
059: log.error(obj1);
060: }
061: } else if (level == WARN) {
062: if (log.isWarnEnabled()) {
063: log.warn(obj1);
064: }
065: } else if (level == INFO) {
066: if (log.isInfoEnabled()) {
067: log.info(obj1);
068: }
069: } else if (level == DEBUG) {
070: if (log.isDebugEnabled()) {
071: log.debug(obj1);
072: }
073: } else {
074: if (log.isTraceEnabled()) {
075: log.trace(obj1);
076: }
077: }
078:
079: }
080:
081: /**
082: * Check if a logger is enabled to log at the specified level
083: *
084: * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
085: */
086:
087: public boolean check(final int level) {
088: if (level == FATAL) {
089: if (log.isFatalEnabled()) {
090: return true;
091: }
092: } else if (level == ERROR) {
093: if (log.isErrorEnabled()) {
094: return true;
095: }
096: } else if (level == WARN) {
097: if (log.isWarnEnabled()) {
098: return true;
099: }
100: } else if (level == INFO) {
101: if (log.isInfoEnabled()) {
102: return true;
103: }
104: } else if (level == DEBUG) {
105: if (log.isDebugEnabled()) {
106: return true;
107: }
108: }
109:
110: return false;
111:
112: }
113:
114: } // end package scope class POILogger
|