001: /*******************************************************************************
002: * Portions created by Sebastian Thomschke are copyright (c) 2005-2007 Sebastian
003: * Thomschke.
004: *
005: * All Rights Reserved. This program and the accompanying materials
006: * are made available under the terms of the Eclipse Public License v1.0
007: * which accompanies this distribution, and is available at
008: * http://www.eclipse.org/legal/epl-v10.html
009: *
010: * Contributors:
011: * Sebastian Thomschke - initial implementation.
012: *******************************************************************************/package net.sf.oval.logging;
013:
014: import java.util.logging.Level;
015: import java.util.logging.LogRecord;
016:
017: /**
018: * JDK Logging Wrapper
019: * @author Sebastian Thomschke
020: */
021: public class LoggerJDKImpl implements Logger {
022: private final java.util.logging.Logger log;
023: private final String name;
024:
025: public LoggerJDKImpl(final String name) {
026: if (name == null)
027: throw new IllegalArgumentException("name cannot be null");
028: this .name = name;
029: log = java.util.logging.Logger.getLogger(name);
030: }
031:
032: public void debug(final String msg) {
033: log(Level.FINE, msg, null);
034: }
035:
036: public void debug(final String msg, final Throwable t) {
037: log(Level.FINE, msg, t);
038: }
039:
040: public void error(final String msg) {
041: log(Level.SEVERE, msg, null);
042: }
043:
044: public void error(final String msg, final Throwable t) {
045: log(Level.SEVERE, msg, t);
046: }
047:
048: public void info(final String msg) {
049: log(Level.INFO, msg, null);
050: }
051:
052: public void info(final String msg, final Throwable t) {
053: log(Level.INFO, msg, t);
054: }
055:
056: public boolean isDebug() {
057: return log.isLoggable(Level.FINE);
058: }
059:
060: public boolean isError() {
061: return log.isLoggable(Level.SEVERE);
062: }
063:
064: public boolean isInfo() {
065: return log.isLoggable(Level.INFO);
066: }
067:
068: public boolean isTrace() {
069: return log.isLoggable(Level.FINEST);
070: }
071:
072: public boolean isWarn() {
073: return log.isLoggable(Level.WARNING);
074: }
075:
076: private void log(final Level level, final String msg,
077: final Throwable t) {
078: final LogRecord record = new LogRecord(level, msg);
079: record.setLoggerName(name);
080: record.setThrown(t);
081:
082: /* java.lang.Throwable
083: * at net.sf.oval.logging.LoggerJDKImpl.log(LoggerJDKImpl.java:123)
084: * at net.sf.oval.logging.LoggerJDKImpl.warn(LoggerJDKImpl.java:136)
085: * at net.sf.oval.internal.Log.warn(Log.java:180)
086: */
087: final int offset = 3;
088: final StackTraceElement[] steArray = new Throwable()
089: .getStackTrace();
090: record.setSourceClassName(steArray[offset].getClassName());
091: record.setSourceMethodName(steArray[offset].getMethodName());
092:
093: log.log(record);
094: }
095:
096: public void trace(final String msg) {
097: log(Level.FINEST, msg, null);
098: }
099:
100: public void trace(final String msg, final Throwable t) {
101: log(Level.FINEST, msg, t);
102: }
103:
104: public void warn(final String msg) {
105: log(Level.WARNING, msg, null);
106: }
107:
108: public void warn(final String msg, final Throwable t) {
109: log(Level.WARNING, msg, t);
110: }
111: }
|