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 software.amazon.awssdk.annotations.SdkProtectedApi;
23
24 @SdkProtectedApi
25 public final class Logger {
26     private final org.slf4j.Logger log;
27
28     Logger(org.slf4j.Logger log) {
29         this.log = log;
30     }
31
32     public org.slf4j.Logger logger() {
33         return log;
34     }
35
36     /**
37      * Checks if info is enabled and if so logs the supplied message
38      * @param msg - supplier for the log message
39      */

40     public void info(Supplier<String> msg) {
41         if (log.isInfoEnabled()) {
42             log.info(msg.get());
43         }
44     }
45
46     /**
47      * Checks if info is enabled and if so logs the supplied message and exception
48      * @param msg - supplier for the log message
49      * @param throwable - a throwable to log
50      */

51     public void info(Supplier<String> msg, Throwable throwable) {
52         if (log.isInfoEnabled()) {
53             log.info(msg.get(), throwable);
54         }
55     }
56
57     /**
58      * Checks if error is enabled and if so logs the supplied message
59      * @param msg - supplier for the log message
60      */

61     public void error(Supplier<String> msg) {
62         if (log.isErrorEnabled()) {
63             log.error(msg.get());
64         }
65     }
66
67     /**
68      * Checks if error is enabled and if so logs the supplied message and exception
69      * @param msg - supplier for the log message
70      * @param throwable - a throwable to log
71      */

72     public void error(Supplier<String> msg, Throwable throwable) {
73         if (log.isErrorEnabled()) {
74             log.error(msg.get(), throwable);
75         }
76     }
77
78     /**
79      * Checks if debug is enabled and if so logs the supplied message
80      * @param msg - supplier for the log message
81      */

82     public void debug(Supplier<String> msg) {
83         if (log.isDebugEnabled()) {
84             log.debug(msg.get());
85         }
86     }
87
88     /**
89      * Checks if debug is enabled and if so logs the supplied message and exception
90      * @param msg - supplier for the log message
91      * @param throwable - a throwable to log
92      */

93     public void debug(Supplier<String> msg, Throwable throwable) {
94         if (log.isDebugEnabled()) {
95             log.debug(msg.get(), throwable);
96         }
97     }
98
99     /**
100      * Checks if warn is enabled and if so logs the supplied message
101      * @param msg - supplier for the log message
102      */

103     public void warn(Supplier<String> msg) {
104         if (log.isWarnEnabled()) {
105             log.warn(msg.get());
106         }
107     }
108
109     /**
110      * Checks if warn is enabled and if so logs the supplied message and exception
111      * @param msg - supplier for the log message
112      * @param throwable - a throwable to log
113      */

114     public void warn(Supplier<String> msg, Throwable throwable) {
115         if (log.isWarnEnabled()) {
116             log.warn(msg.get(), throwable);
117         }
118     }
119
120     /**
121      * Checks if trace is enabled and if so logs the supplied message
122      * @param msg - supplier for the log message
123      */

124     public void trace(Supplier<String> msg) {
125         if (log.isTraceEnabled()) {
126             log.trace(msg.get());
127         }
128     }
129
130     /**
131      * Checks if trace is enabled and if so logs the supplied message and exception
132      * @param msg - supplier for the log message
133      * @param throwable - a throwable to log
134      */

135     public void trace(Supplier<String> msg, Throwable throwable) {
136         if (log.isTraceEnabled()) {
137             log.trace(msg.get(), throwable);
138         }
139     }
140
141     /**
142      * Determines if the log-level passed is enabled
143      * @param logLevel a string representation of the log level, e.g. "debug"
144      * @return whether or not that level is enable
145      */

146     public boolean isLoggingLevelEnabled(String logLevel) {
147         String lowerLogLevel = lowerCase(logLevel);
148         switch (lowerLogLevel) {
149             case "debug":
150                 return log.isDebugEnabled();
151             case "trace":
152                 return log.isTraceEnabled();
153             case "error":
154                 return log.isErrorEnabled();
155             case "info":
156                 return log.isInfoEnabled();
157             case "warn":
158                 return log.isWarnEnabled();
159             default:
160                 throw new IllegalArgumentException("Unknown log level: " + lowerLogLevel);
161         }
162     }
163
164     /**
165      * Static factory to get a logger instance for a given class
166      * @param clz - class to get the logger for
167      * @return a Logger instance
168      */

169     public static Logger loggerFor(Class<?> clz) {
170         return new Logger(LoggerFactory.getLogger(clz));
171     }
172
173     /**
174      * Static factory to get a logger instance with a specific name.
175      * @param name - The name of the logger to create
176      * @return a Logger instance
177      */

178     public static Logger loggerFor(String name) {
179         return new Logger(LoggerFactory.getLogger(name));
180     }
181 }
182