001    /*
002     * Copyright (C) 2006-2010 Enrique Lara (k957@68k.org)
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006      * You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package net.sf.jmorse.util.logging;
018    
019    import java.util.logging.Level;
020    
021    public class Logger {
022        private static final Level TRACE = Level.FINEST;
023        private static final Level DEBUG = Level.FINER;
024        private static final Level INFO = Level.INFO;
025        private static final Level WARN = Level.WARNING;
026        private static final Level ERROR = Level.SEVERE;
027        private static final Level FATAL = Level.SEVERE;
028    
029        public static Logger getLogger(Class clazz) {
030            String name = clazz.getName();
031    
032            java.util.logging.Logger l = java.util.logging.Logger.getLogger(name);
033    
034            return new Logger(l);
035        }
036    
037        private final java.util.logging.Logger jlog;
038    
039        public Logger(java.util.logging.Logger jlog) {
040            this.jlog = jlog;
041        }
042    
043        public boolean isTraceEnabled() {
044            return jlog.isLoggable(TRACE);
045        }
046    
047        public boolean isDebugEnabled() {
048            return jlog.isLoggable(DEBUG);
049        }
050    
051        public boolean isInfoEnabled() {
052            return jlog.isLoggable(INFO);
053        }
054    
055        public boolean isWarnEnabled() {
056            return jlog.isLoggable(WARN);
057        }
058    
059        public boolean isErrorEnabled() {
060            return jlog.isLoggable(ERROR);
061        }
062    
063        public boolean isFatalEnabled() {
064            return jlog.isLoggable(FATAL);
065        }
066    
067        private void log(Level level, Object msg, Throwable t) {
068            if (msg == null) {
069                throw new IllegalArgumentException("Log message may not be null");
070            }
071    
072            if (t != null) {
073                jlog.log(level, msg.toString(), t);
074            } else {
075                jlog.log(level, msg.toString());
076            }
077        }
078    
079        public void trace(Object msg) {
080            log(TRACE, msg, null);
081        }
082    
083        public void trace(Object msg, Throwable t) {
084            log(TRACE, msg, t);
085        }
086    
087        public void debug(Object msg) {
088            log(DEBUG, msg, null);
089        }
090    
091        public void debug(Object msg, Throwable t) {
092            log(DEBUG, msg, t);
093        }
094    
095        public void info(Object msg) {
096            log(INFO, msg, null);
097        }
098    
099        public void info(Object msg, Throwable t) {
100            log(INFO, msg, t);
101        }
102    
103        public void warn(Object msg) {
104            log(WARN, msg, null);
105        }
106    
107        public void warn(Object msg, Throwable t) {
108            log(WARN, msg, t);
109        }
110    
111        public void error(Object msg) {
112            log(ERROR, msg, null);
113        }
114    
115        public void error(Object msg, Throwable t) {
116            log(ERROR, msg, t);
117        }
118    
119        public void fatal(Object msg) {
120            log(FATAL, msg, null);
121        }
122    
123        public void fatal(Object msg, Throwable t) {
124            log(FATAL, msg, t);
125        }
126    }