001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.util;
018    
019    import org.apache.camel.LoggingLevel;
020    import org.slf4j.Logger;
021    import org.slf4j.LoggerFactory;
022    import org.slf4j.Marker;
023    import org.slf4j.MarkerFactory;
024    
025    /**
026     * A logger which logs to a slf4j {@link Logger}.
027     * <p/>
028     * The name <tt>CamelLogger</tt> has been chosen to avoid any name clash with log kits
029     * which has a <tt>Logger</tt> class.
030     *
031     * @version 
032     */
033    public class CamelLogger {
034        private Logger log;
035        private LoggingLevel level;
036        private Marker marker;
037    
038        public CamelLogger() {
039            this(LoggerFactory.getLogger(CamelLogger.class));
040        }
041    
042        public CamelLogger(Logger log) {
043            this(log, LoggingLevel.INFO);
044        }
045    
046        public CamelLogger(Logger log, LoggingLevel level) {
047            this(log, level, null);
048        }
049    
050        public CamelLogger(Logger log, LoggingLevel level, String marker) {
051            this.log = log;
052            setLevel(level);
053            setMarker(marker);
054        }
055    
056        public CamelLogger(String logName) {
057            this(LoggerFactory.getLogger(logName));
058        }
059    
060        public CamelLogger(String logName, LoggingLevel level) {
061            this(logName, level, null);
062        }
063    
064        public CamelLogger(String logName, LoggingLevel level, String marker) {
065            this(LoggerFactory.getLogger(logName), level, marker);
066        }
067    
068        @Override
069        public String toString() {
070            return "Logger[" + log + "]";
071        }
072    
073        public void log(String message, LoggingLevel loggingLevel) {
074            LoggingLevel oldLogLevel = getLevel();
075            setLevel(loggingLevel);
076            log(message);
077            setLevel(oldLogLevel);
078        }
079    
080        /**
081         * Logs the message <b>with</b> checking the {@link #shouldLog()} method first.
082         *
083         * @param message the message to log, if {@link #shouldLog()} returned <tt>true</tt>
084         */
085        public void log(String message) {
086            if (shouldLog(log, level)) {
087                log(log, level, marker, message);
088            }
089        }
090    
091        /**
092         * Logs the message <b>without</b> checking the {@link #shouldLog()} method first.
093         * 
094         * @param message the message to log
095         */
096        public void doLog(String message) {
097            log(log, level, marker, message);
098        }
099    
100        public void log(String message, Throwable exception, LoggingLevel loggingLevel) {
101            log(log, loggingLevel, marker, message, exception);
102        }   
103        
104        public void log(String message, Throwable exception) {
105            if (shouldLog(log, level)) {
106                log(log, level, marker, message, exception);
107            }
108        }
109    
110        public Logger getLog() {
111            return log;
112        }
113    
114        public void setLog(Logger log) {
115            this.log = log;
116        }
117    
118        public LoggingLevel getLevel() {
119            return level;
120        }
121    
122        public void setLevel(LoggingLevel level) {
123            if (level == null) {
124                throw new IllegalArgumentException("Log level may not be null");
125            }
126    
127            this.level = level;
128        }
129    
130        public void setLogName(String logName) {
131            this.log = LoggerFactory.getLogger(logName);
132        }
133    
134        public void setMarker(Marker marker) {
135            this.marker = marker;
136        }
137    
138        public void setMarker(String marker) {
139            if (ObjectHelper.isNotEmpty(marker)) {
140                this.marker = MarkerFactory.getMarker(marker);
141            } else {
142                this.marker = null;
143            }
144        }
145    
146        public static void log(Logger log, LoggingLevel level, String message) {
147            switch (level) {
148            case DEBUG:
149                log.debug(message);
150                break;
151            case ERROR:
152                log.error(message);
153                break;
154            case INFO:
155                log.info(message);
156                break;
157            case TRACE:
158                log.trace(message);
159                break;
160            case WARN:
161                log.warn(message);
162                break;
163            default:
164            }
165        }
166    
167        public static void log(Logger log, LoggingLevel level, Marker marker, String message) {
168            switch (level) {
169            case DEBUG:
170                log.debug(marker, message);
171                break;
172            case ERROR:
173                log.error(marker, message);
174                break;
175            case INFO:
176                log.info(marker, message);
177                break;
178            case TRACE:
179                log.trace(marker, message);
180                break;
181            case WARN:
182                log.warn(marker, message);
183                break;
184            default:
185            }
186        }
187    
188        public static void log(Logger log, LoggingLevel level, String message, Throwable th) {
189            switch (level) {
190            case DEBUG:
191                log.debug(message, th);
192                break;
193            case ERROR:
194                log.error(message, th);
195                break;
196            case INFO:
197                log.info(message, th);
198                break;
199            case TRACE:
200                log.trace(message, th);
201                break;
202            case WARN:
203                log.warn(message, th);
204                break;
205            default:
206            }
207        }
208    
209        public static void log(Logger log, LoggingLevel level, Marker marker, String message, Throwable th) {
210            switch (level) {
211            case DEBUG:
212                log.debug(marker, message, th);
213                break;
214            case ERROR:
215                log.error(marker, message, th);
216                break;
217            case INFO:
218                log.info(marker, message, th);
219                break;
220            case TRACE:
221                log.trace(marker, message, th);
222                break;
223            case WARN:
224                log.warn(marker, message, th);
225                break;
226            default:
227            }
228        }
229    
230        public boolean shouldLog() {
231            return CamelLogger.shouldLog(log, level);
232        }
233    
234        public static boolean shouldLog(Logger log, LoggingLevel level) {
235            switch (level) {
236            case DEBUG:
237                return log.isDebugEnabled(); 
238            case ERROR:
239                return log.isErrorEnabled(); 
240            case INFO:
241                return log.isInfoEnabled(); 
242            case TRACE:
243                return log.isTraceEnabled(); 
244            case WARN:
245                return log.isWarnEnabled(); 
246            default:
247            }
248            return false;
249        }
250    }