001/*
002 * HA-JDBC: High-Availability JDBC
003 * Copyright (C) 2012  Paul Ferraro
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Lesser General Public License as published by
007 * the Free Software Foundation, either version 3 of the License, or
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Lesser General Public License for more details.
014 *
015 * You should have received a copy of the GNU Lesser General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018package net.sf.hajdbc.logging.jdk;
019
020import java.util.EnumMap;
021import java.util.Map;
022
023import net.sf.hajdbc.logging.AbstractLogger;
024import net.sf.hajdbc.logging.Level;
025
026/**
027 * java.util.logging-based {@link net.sf.hajdbc.logging.Logger} implementation.
028 * @author Paul Ferraro
029 */
030public class JDKLogger extends AbstractLogger
031{
032        private static final Map<Level, java.util.logging.Level> levels = new EnumMap<Level, java.util.logging.Level>(Level.class);
033        static
034        {
035                levels.put(Level.ERROR, java.util.logging.Level.SEVERE);
036                levels.put(Level.WARN, java.util.logging.Level.WARNING);
037                levels.put(Level.INFO, java.util.logging.Level.INFO);
038                levels.put(Level.DEBUG, java.util.logging.Level.FINE);
039                levels.put(Level.TRACE, java.util.logging.Level.FINEST);
040        }
041        
042        private final java.util.logging.Logger logger;
043        
044        public JDKLogger(Class<?> targetClass)
045        {
046                this.logger = java.util.logging.Logger.getLogger(targetClass.getName());
047        }
048
049        /**
050         * {@inheritDoc}
051         * @see net.sf.hajdbc.logging.Logger#log(net.sf.hajdbc.logging.Level, java.lang.Throwable, java.lang.String, java.lang.Object[])
052         */
053        @Override
054        public void log(Level level, Throwable e, String pattern, Object... args)
055        {
056                java.util.logging.Level realLevel = levels.get(level);
057                
058                if (this.logger.isLoggable(realLevel))
059                {
060                        String message = format(pattern, args);
061                        
062                        if (e != null)
063                        {
064                                this.logger.log(realLevel, message, e);
065                        }
066                        else
067                        {
068                                this.logger.log(realLevel, message);
069                        }
070                }
071        }
072}