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.util; 018 019import org.apache.camel.LoggingLevel; 020import org.slf4j.Logger; 021import org.slf4j.LoggerFactory; 022import org.slf4j.Marker; 023import 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 */ 033public 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 Marker getMarker() { 135 return marker; 136 } 137 138 public void setMarker(Marker marker) { 139 this.marker = marker; 140 } 141 142 public void setMarker(String marker) { 143 if (ObjectHelper.isNotEmpty(marker)) { 144 this.marker = MarkerFactory.getMarker(marker); 145 } else { 146 this.marker = null; 147 } 148 } 149 150 public static void log(Logger log, LoggingLevel level, String message) { 151 switch (level) { 152 case DEBUG: 153 log.debug(message); 154 break; 155 case ERROR: 156 log.error(message); 157 break; 158 case INFO: 159 log.info(message); 160 break; 161 case TRACE: 162 log.trace(message); 163 break; 164 case WARN: 165 log.warn(message); 166 break; 167 default: 168 } 169 } 170 171 public static void log(Logger log, LoggingLevel level, Marker marker, String message) { 172 if (marker == null) { 173 log(log, level, message); 174 return; 175 } 176 177 // marker must be provided 178 switch (level) { 179 case DEBUG: 180 log.debug(marker, message); 181 break; 182 case ERROR: 183 log.error(marker, message); 184 break; 185 case INFO: 186 log.info(marker, message); 187 break; 188 case TRACE: 189 log.trace(marker, message); 190 break; 191 case WARN: 192 log.warn(marker, message); 193 break; 194 default: 195 } 196 } 197 198 public static void log(Logger log, LoggingLevel level, String message, Throwable th) { 199 switch (level) { 200 case DEBUG: 201 log.debug(message, th); 202 break; 203 case ERROR: 204 log.error(message, th); 205 break; 206 case INFO: 207 log.info(message, th); 208 break; 209 case TRACE: 210 log.trace(message, th); 211 break; 212 case WARN: 213 log.warn(message, th); 214 break; 215 default: 216 } 217 } 218 219 public static void log(Logger log, LoggingLevel level, Marker marker, String message, Throwable th) { 220 if (marker == null) { 221 log(log, level, message, th); 222 return; 223 } 224 225 // marker must be provided 226 switch (level) { 227 case DEBUG: 228 log.debug(marker, message, th); 229 break; 230 case ERROR: 231 log.error(marker, message, th); 232 break; 233 case INFO: 234 log.info(marker, message, th); 235 break; 236 case TRACE: 237 log.trace(marker, message, th); 238 break; 239 case WARN: 240 log.warn(marker, message, th); 241 break; 242 default: 243 } 244 } 245 246 public boolean shouldLog() { 247 return CamelLogger.shouldLog(log, level); 248 } 249 250 public static boolean shouldLog(Logger log, LoggingLevel level) { 251 switch (level) { 252 case DEBUG: 253 return log.isDebugEnabled(); 254 case ERROR: 255 return log.isErrorEnabled(); 256 case INFO: 257 return log.isInfoEnabled(); 258 case TRACE: 259 return log.isTraceEnabled(); 260 case WARN: 261 return log.isWarnEnabled(); 262 default: 263 } 264 return false; 265 } 266}