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 */
017package org.apache.camel.spi;
018
019import org.apache.camel.LoggingLevel;
020import org.apache.camel.util.ObjectHelper;
021import org.slf4j.Logger;
022import org.slf4j.LoggerFactory;
023import org.slf4j.Marker;
024import org.slf4j.MarkerFactory;
025
026/**
027 * A logger which logs to a slf4j {@link Logger}.
028 * <p/>
029 * The name <tt>CamelLogger</tt> has been chosen to avoid any name clash with log kits
030 * which has a <tt>Logger</tt> class.
031 */
032public class CamelLogger {
033    private Logger log;
034    private LoggingLevel level;
035    private Marker marker;
036
037    public CamelLogger() {
038        this(LoggerFactory.getLogger(CamelLogger.class));
039    }
040
041    public CamelLogger(Logger log) {
042        this(log, LoggingLevel.INFO);
043    }
044
045    public CamelLogger(Logger log, LoggingLevel level) {
046        this(log, level, null);
047    }
048
049    public CamelLogger(Logger log, LoggingLevel level, String marker) {
050        this.log = log;
051        setLevel(level);
052        setMarker(marker);
053    }
054
055    public CamelLogger(String logName) {
056        this(LoggerFactory.getLogger(logName));
057    }
058
059    public CamelLogger(String logName, LoggingLevel level) {
060        this(logName, level, null);
061    }
062
063    public CamelLogger(String logName, LoggingLevel level, String marker) {
064        this(LoggerFactory.getLogger(logName), level, marker);
065    }
066
067    @Override
068    public String toString() {
069        return "Logger[" + log + "]";
070    }
071
072    public void log(String message, LoggingLevel loggingLevel) {
073        LoggingLevel oldLogLevel = getLevel();
074        setLevel(loggingLevel);
075        log(message);
076        setLevel(oldLogLevel);
077    }
078
079    /**
080     * Logs the message <b>with</b> checking the {@link #shouldLog()} method first.
081     *
082     * @param message the message to log, if {@link #shouldLog()} returned <tt>true</tt>
083     */
084    public void log(String message) {
085        if (shouldLog(log, level)) {
086            log(log, level, marker, message);
087        }
088    }
089
090    /**
091     * Logs the message <b>without</b> checking the {@link #shouldLog()} method first.
092     * 
093     * @param message the message to log
094     */
095    public void doLog(String message) {
096        log(log, level, marker, message);
097    }
098
099    public void log(String message, Throwable exception, LoggingLevel loggingLevel) {
100        log(log, loggingLevel, marker, message, exception);
101    }   
102    
103    public void log(String message, Throwable exception) {
104        if (shouldLog(log, level)) {
105            log(log, level, marker, message, exception);
106        }
107    }
108
109    public Logger getLog() {
110        return log;
111    }
112
113    public void setLog(Logger log) {
114        this.log = log;
115    }
116
117    public LoggingLevel getLevel() {
118        return level;
119    }
120
121    public void setLevel(LoggingLevel level) {
122        if (level == null) {
123            throw new IllegalArgumentException("Log level may not be null");
124        }
125
126        this.level = level;
127    }
128
129    public void setLogName(String logName) {
130        this.log = LoggerFactory.getLogger(logName);
131    }
132
133    public Marker getMarker() {
134        return marker;
135    }
136
137    public void setMarker(Marker marker) {
138        this.marker = marker;
139    }
140
141    public void setMarker(String marker) {
142        if (ObjectHelper.isNotEmpty(marker)) {
143            this.marker = MarkerFactory.getMarker(marker);
144        } else {
145            this.marker = null;
146        }
147    }
148
149    public static void log(Logger log, LoggingLevel level, String message) {
150        switch (level) {
151        case DEBUG:
152            log.debug(message);
153            break;
154        case ERROR:
155            log.error(message);
156            break;
157        case INFO:
158            log.info(message);
159            break;
160        case TRACE:
161            log.trace(message);
162            break;
163        case WARN:
164            log.warn(message);
165            break;
166        default:
167        }
168    }
169
170    public static void log(Logger log, LoggingLevel level, Marker marker, String message) {
171        if (marker == null) {
172            log(log, level, message);
173            return;
174        }
175
176        // marker must be provided
177        switch (level) {
178        case DEBUG:
179            log.debug(marker, message);
180            break;
181        case ERROR:
182            log.error(marker, message);
183            break;
184        case INFO:
185            log.info(marker, message);
186            break;
187        case TRACE:
188            log.trace(marker, message);
189            break;
190        case WARN:
191            log.warn(marker, message);
192            break;
193        default:
194        }
195    }
196
197    public static void log(Logger log, LoggingLevel level, String message, Throwable th) {
198        switch (level) {
199        case DEBUG:
200            log.debug(message, th);
201            break;
202        case ERROR:
203            log.error(message, th);
204            break;
205        case INFO:
206            log.info(message, th);
207            break;
208        case TRACE:
209            log.trace(message, th);
210            break;
211        case WARN:
212            log.warn(message, th);
213            break;
214        default:
215        }
216    }
217
218    public static void log(Logger log, LoggingLevel level, Marker marker, String message, Throwable th) {
219        if (marker == null) {
220            log(log, level, message, th);
221            return;
222        }
223
224        // marker must be provided
225        switch (level) {
226        case DEBUG:
227            log.debug(marker, message, th);
228            break;
229        case ERROR:
230            log.error(marker, message, th);
231            break;
232        case INFO:
233            log.info(marker, message, th);
234            break;
235        case TRACE:
236            log.trace(marker, message, th);
237            break;
238        case WARN:
239            log.warn(marker, message, th);
240            break;
241        default:
242        }
243    }
244
245    public boolean shouldLog() {
246        return CamelLogger.shouldLog(log, level);
247    }
248
249    public static boolean shouldLog(Logger log, LoggingLevel level) {
250        switch (level) {
251        case DEBUG:
252            return log.isDebugEnabled(); 
253        case ERROR:
254            return log.isErrorEnabled(); 
255        case INFO:
256            return log.isInfoEnabled(); 
257        case TRACE:
258            return log.isTraceEnabled(); 
259        case WARN:
260            return log.isWarnEnabled(); 
261        default:
262        }
263        return false;
264    }
265}