1 /* 2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"). 5 * You may not use this file except in compliance with the License. 6 * A copy of the License is located at 7 * 8 * http://aws.amazon.com/apache2.0 9 * 10 * or in the "license" file accompanying this file. This file is distributed 11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 * express or implied. See the License for the specific language governing 13 * permissions and limitations under the License. 14 */ 15 16 package software.amazon.awssdk.utils; 17 18 import static software.amazon.awssdk.utils.StringUtils.lowerCase; 19 20 import java.util.function.Supplier; 21 import org.slf4j.LoggerFactory; 22 import org.slf4j.event.Level; 23 import software.amazon.awssdk.annotations.SdkProtectedApi; 24 25 @SdkProtectedApi 26 public final class Logger { 27 private final org.slf4j.Logger log; 28 Logger(org.slf4j.Logger log)29 Logger(org.slf4j.Logger log) { 30 this.log = log; 31 } 32 logger()33 public org.slf4j.Logger logger() { 34 return log; 35 } 36 37 /** 38 * Checks if info is enabled and if so logs the supplied message 39 * @param msg - supplier for the log message 40 */ info(Supplier<String> msg)41 public void info(Supplier<String> msg) { 42 if (log.isInfoEnabled()) { 43 log.info(msg.get()); 44 } 45 } 46 47 /** 48 * Checks if info is enabled and if so logs the supplied message and exception 49 * @param msg - supplier for the log message 50 * @param throwable - a throwable to log 51 */ info(Supplier<String> msg, Throwable throwable)52 public void info(Supplier<String> msg, Throwable throwable) { 53 if (log.isInfoEnabled()) { 54 log.info(msg.get(), throwable); 55 } 56 } 57 58 /** 59 * Checks if error is enabled and if so logs the supplied message 60 * @param msg - supplier for the log message 61 */ error(Supplier<String> msg)62 public void error(Supplier<String> msg) { 63 if (log.isErrorEnabled()) { 64 log.error(msg.get()); 65 } 66 } 67 68 /** 69 * Checks if error is enabled and if so logs the supplied message and exception 70 * @param msg - supplier for the log message 71 * @param throwable - a throwable to log 72 */ error(Supplier<String> msg, Throwable throwable)73 public void error(Supplier<String> msg, Throwable throwable) { 74 if (log.isErrorEnabled()) { 75 log.error(msg.get(), throwable); 76 } 77 } 78 79 /** 80 * Checks if debug is enabled and if so logs the supplied message 81 * @param msg - supplier for the log message 82 */ debug(Supplier<String> msg)83 public void debug(Supplier<String> msg) { 84 if (log.isDebugEnabled()) { 85 log.debug(msg.get()); 86 } 87 } 88 89 /** 90 * Checks if debug is enabled and if so logs the supplied message and exception 91 * @param msg - supplier for the log message 92 * @param throwable - a throwable to log 93 */ debug(Supplier<String> msg, Throwable throwable)94 public void debug(Supplier<String> msg, Throwable throwable) { 95 if (log.isDebugEnabled()) { 96 log.debug(msg.get(), throwable); 97 } 98 } 99 100 /** 101 * Checks if warn is enabled and if so logs the supplied message 102 * @param msg - supplier for the log message 103 */ warn(Supplier<String> msg)104 public void warn(Supplier<String> msg) { 105 if (log.isWarnEnabled()) { 106 log.warn(msg.get()); 107 } 108 } 109 110 /** 111 * Checks if warn is enabled and if so logs the supplied message and exception 112 * @param msg - supplier for the log message 113 * @param throwable - a throwable to log 114 */ warn(Supplier<String> msg, Throwable throwable)115 public void warn(Supplier<String> msg, Throwable throwable) { 116 if (log.isWarnEnabled()) { 117 log.warn(msg.get(), throwable); 118 } 119 } 120 121 /** 122 * Checks if trace is enabled and if so logs the supplied message 123 * @param msg - supplier for the log message 124 */ trace(Supplier<String> msg)125 public void trace(Supplier<String> msg) { 126 if (log.isTraceEnabled()) { 127 log.trace(msg.get()); 128 } 129 } 130 131 /** 132 * Checks if trace is enabled and if so logs the supplied message and exception 133 * @param msg - supplier for the log message 134 * @param throwable - a throwable to log 135 */ trace(Supplier<String> msg, Throwable throwable)136 public void trace(Supplier<String> msg, Throwable throwable) { 137 if (log.isTraceEnabled()) { 138 log.trace(msg.get(), throwable); 139 } 140 } 141 142 /** 143 * Determines if the provided log-level is enabled. 144 * @param logLevel the SLF4J log level enum 145 * @return whether that level is enabled 146 */ isLoggingLevelEnabled(Level logLevel)147 public boolean isLoggingLevelEnabled(Level logLevel) { 148 switch (logLevel) { 149 case TRACE: 150 return log.isTraceEnabled(); 151 case DEBUG: 152 return log.isDebugEnabled(); 153 case INFO: 154 return log.isInfoEnabled(); 155 case WARN: 156 return log.isWarnEnabled(); 157 case ERROR: 158 return log.isErrorEnabled(); 159 default: 160 throw new IllegalStateException("Unsupported log level: " + logLevel); 161 } 162 } 163 164 /** 165 * Determines if the log-level passed is enabled 166 * @param logLevel a string representation of the log level, e.g. "debug" 167 * @return whether or not that level is enable 168 */ isLoggingLevelEnabled(String logLevel)169 public boolean isLoggingLevelEnabled(String logLevel) { 170 String lowerLogLevel = lowerCase(logLevel); 171 switch (lowerLogLevel) { 172 case "debug": 173 return log.isDebugEnabled(); 174 case "trace": 175 return log.isTraceEnabled(); 176 case "error": 177 return log.isErrorEnabled(); 178 case "info": 179 return log.isInfoEnabled(); 180 case "warn": 181 return log.isWarnEnabled(); 182 default: 183 throw new IllegalArgumentException("Unknown log level: " + lowerLogLevel); 184 } 185 } 186 187 /** 188 * Log a message at the given log level (if it is enabled). 189 * 190 * @param logLevel the SLF4J log level 191 * @param msg supplier for the log message 192 */ log(Level logLevel, Supplier<String> msg)193 public void log(Level logLevel, Supplier<String> msg) { 194 switch (logLevel) { 195 case TRACE: 196 trace(msg); 197 break; 198 case DEBUG: 199 debug(msg); 200 break; 201 case INFO: 202 info(msg); 203 break; 204 case WARN: 205 warn(msg); 206 break; 207 case ERROR: 208 error(msg); 209 break; 210 default: 211 throw new IllegalStateException("Unsupported log level: " + logLevel); 212 } 213 } 214 215 /** 216 * Static factory to get a logger instance for a given class 217 * @param clz - class to get the logger for 218 * @return a Logger instance 219 */ loggerFor(Class<?> clz)220 public static Logger loggerFor(Class<?> clz) { 221 return new Logger(LoggerFactory.getLogger(clz)); 222 } 223 224 /** 225 * Static factory to get a logger instance with a specific name. 226 * @param name - The name of the logger to create 227 * @return a Logger instance 228 */ loggerFor(String name)229 public static Logger loggerFor(String name) { 230 return new Logger(LoggerFactory.getLogger(name)); 231 } 232 } 233