399fd3717e772e9659a025408d87adf0362c75f8
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
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  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.logging.api;
18
19 import org.openecomp.sdc.logging.spi.LoggerCreationService;
20
21 import java.util.Objects;
22
23 /**
24  * <a>Factory to hide a concrete, framework-specific implementation of logger creation.</a>
25  * <p>The service used by this factory must implement {@link LoggerCreationService}. If no
26  * implementation has been configured or could be instantiated, a <b>no-op logger</b> will be
27  * used, and <b>no events</b> will be logged. This is done to prevent recursion if attempts are
28  * being made to log exceptions that resulted from logger initialization. </p>
29  *
30  * @author evitaliy
31  * @since 13/09/2016.
32  *
33  * @see ServiceBinder
34  * @see LoggerCreationService
35  */
36 public class LoggerFactory {
37
38     // use the no-op service to prevent recursion in case of an attempt to log an exception as a
39     // result of a logger initialization error
40     private static final LoggerCreationService SERVICE = ServiceBinder.getCreationServiceBinding().orElse(
41             new NoOpLoggerCreationService());
42
43     private LoggerFactory() {
44         // prevent instantiation
45     }
46
47     public static Logger getLogger(String clazzName) {
48         return SERVICE.getLogger(clazzName);
49     }
50
51     public static Logger getLogger(Class<?> clazz) {
52         return SERVICE.getLogger(clazz);
53     }
54
55     private static class NoOpLoggerCreationService implements LoggerCreationService {
56
57         private static final Logger NO_OP_LOGGER = new NoOpLogger();
58
59         private static class NoOpLogger implements Logger {
60
61             @Override
62             public String getName() {
63                 return "No-Op Logger";
64             }
65
66             @Override
67             public boolean isMetricsEnabled() {
68                 return false;
69             }
70
71             @Override
72             public void metrics(String msg) {
73                 // no-op
74             }
75
76             @Override
77             public void metrics(String msg, Object arg) {
78                 // no-op
79             }
80
81             @Override
82             public void metrics(String msg, Object arg1, Object arg2) {
83                 // no-op
84             }
85
86             @Override
87             public void metrics(String msg, Object... arguments) {
88                 // no-op
89             }
90
91             @Override
92             public void metrics(String msg, Throwable t) {
93                 // no-op
94             }
95
96             @Override
97             public boolean isAuditEnabled() {
98                 return false;
99             }
100
101             @Override
102             public void audit(AuditData data) {
103                 // no-op
104             }
105
106             @Override
107             public boolean isDebugEnabled() {
108                 return false;
109             }
110
111             @Override
112             public void debug(String msg) {
113                 // no-op
114             }
115
116             @Override
117             public void debug(String msg, Object arg) {
118                 // no-op
119             }
120
121             @Override
122             public void debug(String msg, Object arg1, Object arg2) {
123                 // no-op
124             }
125
126             @Override
127             public void debug(String msg, Object... arguments) {
128                 // no-op
129             }
130
131             @Override
132             public void debug(String msg, Throwable t) {
133                 // no-op
134             }
135
136             @Override
137             public boolean isInfoEnabled() {
138                 return false;
139             }
140
141             @Override
142             public void info(String msg) {
143                 // no-op
144             }
145
146             @Override
147             public void info(String msg, Object arg) {
148                 // no-op
149             }
150
151             @Override
152             public void info(String msg, Object arg1, Object arg2) {
153                 // no-op
154             }
155
156             @Override
157             public void info(String msg, Object... arguments) {
158                 // no-op
159             }
160
161             @Override
162             public void info(String msg, Throwable t) {
163                 // no-op
164             }
165
166             @Override
167             public boolean isWarnEnabled() {
168                 return false;
169             }
170
171             @Override
172             public void warn(String msg) {
173                 // no-op
174             }
175
176             @Override
177             public void warn(String msg, Object arg) {
178                 // no-op
179             }
180
181             @Override
182             public void warn(String msg, Object... arguments) {
183                 // no-op
184             }
185
186             @Override
187             public void warn(String msg, Object arg1, Object arg2) {
188                 // no-op
189             }
190
191             @Override
192             public void warn(String msg, Throwable t) {
193                 // no-op
194             }
195
196             @Override
197             public boolean isErrorEnabled() {
198                 return false;
199             }
200
201             @Override
202             public void error(String msg) {
203                 // no-op
204             }
205
206             @Override
207             public void error(String msg, Object arg) {
208                 // no-op
209             }
210
211             @Override
212             public void error(String msg, Object arg1, Object arg2) {
213                 // no-op
214             }
215
216             @Override
217             public void error(String msg, Object... arguments) {
218                 // no-op
219             }
220
221             @Override
222             public void error(String msg, Throwable t) {
223                 // no-op
224             }
225         }
226
227         @Override
228         public Logger getLogger(String className) {
229             Objects.requireNonNull(className, "Name cannot be null");
230             return NO_OP_LOGGER;
231         }
232
233         @Override
234         public Logger getLogger(Class<?> clazz) {
235             Objects.requireNonNull(clazz, "Class cannot be null");
236             return NO_OP_LOGGER;
237         }
238     }
239 }
240