001: /*
002: * hgcommons 7
003: * Hammurapi Group Common Library
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.logging;
024:
025: /**
026: * @author Pavel Vlasov
027: * @version $Revision: 1.2 $
028: */
029: public class ConsoleLogger implements Logger {
030: public static final int DEBUG = 0;
031: public static final int VERBOSE = 1;
032: public static final int INFO = 2;
033: public static final int WARN = 3;
034: public static final int ERROR = 4;
035:
036: private int level;
037:
038: /**
039: * Default constructor with INFO level.
040: */
041: public ConsoleLogger() {
042: level = INFO;
043: }
044:
045: public ConsoleLogger(String level) {
046: if ("DEBUG".equalsIgnoreCase(level)) {
047: this .level = DEBUG;
048: } else if ("VERBOSE".equalsIgnoreCase(level)) {
049: this .level = VERBOSE;
050: } else if ("INFO".equalsIgnoreCase(level)) {
051: this .level = INFO;
052: } else if ("WARN".equalsIgnoreCase(level)) {
053: this .level = WARN;
054: } else if ("ERROR".equalsIgnoreCase(level)) {
055: this .level = ERROR;
056: } else {
057: throw new IllegalArgumentException("Invalid level: "
058: + level);
059: }
060:
061: }
062:
063: public ConsoleLogger(int level) {
064: if (level < DEBUG || level > ERROR) {
065: throw new IllegalArgumentException("Invalid level: "
066: + level);
067: }
068: this .level = level;
069: }
070:
071: public void debug(Object source, String message) {
072: if (level <= DEBUG) {
073: System.out.println("[DEBUG] " + source + " " + message);
074: }
075: }
076:
077: public void verbose(Object source, String message) {
078: if (level <= VERBOSE) {
079: System.out.println("[VERBOSE] " + source + " " + message);
080: }
081: }
082:
083: public void info(Object source, String message) {
084: if (level <= INFO) {
085: System.out.println("[INFO] " + source + " " + message);
086: }
087: }
088:
089: public void warn(Object source, String message) {
090: if (level <= WARN) {
091: System.out.println("[WARN] " + source + " " + message);
092: if (source instanceof Throwable) {
093: ((Throwable) source).printStackTrace();
094: }
095: }
096: }
097:
098: public void error(Object source, String message) {
099: if (level <= ERROR) {
100: System.out.println("[ERROR] " + source + " " + message);
101: if (source instanceof Throwable) {
102: ((Throwable) source).printStackTrace();
103: }
104: }
105: }
106: }
|