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.commons;
019
020import org.apache.commons.logging.Log;
021import org.apache.commons.logging.LogFactory;
022
023import net.sf.hajdbc.logging.AbstractLogger;
024import net.sf.hajdbc.logging.Level;
025
026/**
027 * Apache Commons logging {@link net.sf.hajdbc.logging.Logger}.
028 * @author Paul Ferraro
029 */
030public class CommonsLogger extends AbstractLogger
031{
032        private final Log log;
033        
034        public CommonsLogger(Class<?> targetClass)
035        {
036                this.log = LogFactory.getLog(targetClass);
037        }
038        
039        /**
040         * {@inheritDoc}
041         * @see net.sf.hajdbc.logging.Logger#log(net.sf.hajdbc.logging.Level, java.lang.Throwable, java.lang.String, java.lang.Object[])
042         */
043        @Override
044        public void log(Level level, Throwable e, String pattern, Object... args)
045        {
046                switch (level)
047                {
048                        case ERROR:
049                        {
050                                if (this.log.isErrorEnabled())
051                                {
052                                        String message = format(pattern, args);
053                                        
054                                        if (e != null)
055                                        {
056                                                this.log.error(message, e);
057                                        }
058                                        else
059                                        {
060                                                this.log.error(message);
061                                        }
062                                }
063                                
064                                break;
065                        }
066                        case WARN:
067                        {
068                                if (this.log.isWarnEnabled())
069                                {
070                                        String message = format(pattern, args);
071                                        
072                                        if (e != null)
073                                        {
074                                                this.log.warn(message, e);
075                                        }
076                                        else
077                                        {
078                                                this.log.warn(message);
079                                        }
080                                }
081                                
082                                break;
083                        }
084                        case INFO:
085                        {
086                                if (this.log.isInfoEnabled())
087                                {
088                                        String message = format(pattern, args);
089                                        
090                                        if (e != null)
091                                        {
092                                                this.log.info(message, e);
093                                        }
094                                        else
095                                        {
096                                                this.log.info(message);
097                                        }
098                                }
099                                
100                                break;
101                        }
102                        case DEBUG:
103                        {
104                                if (this.log.isDebugEnabled())
105                                {
106                                        String message = format(pattern, args);
107                                        
108                                        if (e != null)
109                                        {
110                                                this.log.debug(message, e);
111                                        }
112                                        else
113                                        {
114                                                this.log.debug(message);
115                                        }
116                                }
117                                
118                                break;
119                        }
120                        case TRACE:
121                        {
122                                if (this.log.isTraceEnabled())
123                                {
124                                        String message = format(pattern, args);
125                                        
126                                        if (e != null)
127                                        {
128                                                this.log.trace(message, e);
129                                        }
130                                        else
131                                        {
132                                                this.log.trace(message);
133                                        }
134                                }
135                                
136                                break;
137                        }
138                }
139        }
140}