1be2fa21dcfa9c0c3d611badd8f158dae7d751e7
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 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
20 /**
21  * <a>Factory to hide a concrete, framework-specific implementation of logger creation.</a>
22  * <p>The service used by this factory must implement {@link LoggerCreationService}. If no
23  * implementation has been configured or could not be instantiated, a <b>no-op logger</b> will be
24  * used, and <b>no events</b> will be logged. This is done to prevent recursion if attempts are
25  * being made to log exceptions that resulted from logger initialization. </p>
26  *
27  * @author evitaliy
28  * @see BaseFactory
29  * @see LoggerCreationService
30  * @since 13/09/2016.
31  */
32 @SuppressWarnings("ThrowableInstanceNeverThrown")
33 public class LoggerFactory extends BaseFactory {
34
35   private static final LoggerCreationService SERVICE;
36
37   static {
38     LoggerCreationService service;
39
40     try {
41       service = locateService(LoggerCreationService.class);
42     } catch (Exception ex) {
43       new RuntimeException("Failed to instantiate logger factory", ex).printStackTrace();
44       // use the no-op service to prevent recursion in case of an attempt to log an exception as a
45       // result of a logger initialization error
46       service = new NoOpLoggerCreationService();
47     }
48
49     SERVICE = service;
50   }
51
52   public static Logger getLogger(String clazzName) {
53     return SERVICE.getLogger(clazzName);
54   }
55
56   public static Logger getLogger(Class<?> clazz) {
57     return SERVICE.getLogger(clazz);
58   }
59
60   private static class NoOpLoggerCreationService implements LoggerCreationService {
61     private static final Logger NO_OP_LOGGER = new NoOpLogger();
62
63     private static class NoOpLogger implements Logger {
64       @Override
65       public String getName() {
66         return "No-Op Logger";
67       }
68
69       @Override
70       public boolean isMetricsEnabled() {
71         return false;
72       }
73
74       @Override
75       public void metrics(String msg) {
76         //this is no_op_method
77       }
78
79       @Override
80       public void metrics(String msg, Object arg) {
81         //this is no_op_method
82       }
83
84       @Override
85       public void metrics(String msg, Object arg1, Object arg2) {
86         //this is no_op_method
87       }
88
89       @Override
90       public void metrics(String msg, Object... arguments) {
91         //this is no_op_method
92       }
93
94       @Override
95       public void metrics(String msg, Throwable t) {
96         //this is no_op_method
97       }
98
99       @Override
100       public boolean isAuditEnabled() {
101         return false;
102       }
103
104       @Override
105       public void audit(String msg) {
106         //this is no_op_method
107       }
108
109       @Override
110       public void audit(String msg, Object arg) {
111         //this is no_op_method
112       }
113
114       @Override
115       public void audit(String msg, Object arg1, Object arg2) {
116         //this is no_op_method
117       }
118
119       @Override
120       public void audit(String msg, Object... arguments) {
121         //this is no_op_method
122       }
123
124       @Override
125       public void audit(String msg, Throwable t) {
126         //this is no_op_method
127       }
128
129       @Override
130       public boolean isDebugEnabled() {
131         return false;
132       }
133
134       @Override
135       public void debug(String msg) {
136         //this is no_op_method
137       }
138
139       @Override
140       public void debug(String msg, Object arg) {
141         //this is no_op_method
142       }
143
144       @Override
145       public void debug(String msg, Object arg1, Object arg2) {
146         //this is no_op_method
147       }
148
149       @Override
150       public void debug(String msg, Object... arguments) {
151         //this is no_op_method
152       }
153
154       @Override
155       public void debug(String msg, Throwable t) {
156         //this is no_op_method
157       }
158
159       @Override
160       public boolean isInfoEnabled() {
161         return false;
162       }
163
164       @Override
165       public void info(String msg) {
166         //this is no_op_method
167       }
168
169       @Override
170       public void info(String msg, Object arg) {
171         //this is no_op_method
172       }
173
174       @Override
175       public void info(String msg, Object arg1, Object arg2) {
176         //this is no_op_method
177       }
178
179       @Override
180       public void info(String msg, Object... arguments) {
181         //this is no_op_method
182       }
183
184       @Override
185       public void info(String msg, Throwable t) {
186         //this is no_op_method
187       }
188
189       @Override
190       public boolean isWarnEnabled() {
191         return false;
192       }
193
194       @Override
195       public void warn(String msg) {
196         //this is no_op_method
197       }
198
199       @Override
200       public void warn(String msg, Object arg) {
201         //this is no_op_method
202       }
203
204       @Override
205       public void warn(String msg, Object... arguments) {
206         //this is no_op_method
207       }
208
209       @Override
210       public void warn(String msg, Object arg1, Object arg2) {
211         //this is no_op_method
212       }
213
214       @Override
215       public void warn(String msg, Throwable t) {
216         //this is no_op_method
217       }
218
219       @Override
220       public boolean isErrorEnabled() {
221         return false;
222       }
223
224       @Override
225       public void error(String msg) {
226         //this is no_op_method
227       }
228
229       @Override
230       public void error(String msg, Object arg) {
231         //this is no_op_method
232       }
233
234       @Override
235       public void error(String msg, Object arg1, Object arg2) {
236         //this is no_op_method
237       }
238
239       @Override
240       public void error(String msg, Object... arguments) {
241         //this is no_op_method
242       }
243
244       @Override
245       public void error(String msg, Throwable t) {
246         //this is no_op_method
247       }
248     }
249
250     @Override
251     public Logger getLogger(String className) {
252       return NO_OP_LOGGER;
253     }
254
255     @Override
256     public Logger getLogger(Class<?> clazz) {
257       return NO_OP_LOGGER;
258     }
259   }
260 }