re base code
[sdc.git] / openecomp-be / lib / openecomp-sdc-logging-lib / openecomp-sdc-logging-api / src / main / java / org / openecomp / sdc / logging / api / LoggerFactory.java
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().orElseGet(
41             NoOpLoggerCreationService::new);
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 class NoOpLogger implements Logger {
58
59             private static final Logger INSTANCE = new NoOpLogger();
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(MetricsData msg) {
73                 // no-op
74             }
75
76             @Override
77             public void metrics(String msg) {
78                 // no-op
79             }
80
81             @Override
82             public boolean isAuditEnabled() {
83                 return false;
84             }
85
86             @Override
87             public void audit(AuditData data) {
88                 // no-op
89             }
90
91             @Override
92             public boolean isDebugEnabled() {
93                 return false;
94             }
95
96             @Override
97             public void debug(String msg) {
98                 // no-op
99             }
100
101             @Override
102             public void debug(String msg, Object arg) {
103                 // no-op
104             }
105
106             @Override
107             public void debug(String msg, Object arg1, Object arg2) {
108                 // no-op
109             }
110
111             @Override
112             public void debug(String msg, Object... arguments) {
113                 // no-op
114             }
115
116             @Override
117             public void debug(String msg, Throwable t) {
118                 // no-op
119             }
120
121             @Override
122             public boolean isInfoEnabled() {
123                 return false;
124             }
125
126             @Override
127             public void info(String msg) {
128                 // no-op
129             }
130
131             @Override
132             public void info(String msg, Object arg) {
133                 // no-op
134             }
135
136             @Override
137             public void info(String msg, Object arg1, Object arg2) {
138                 // no-op
139             }
140
141             @Override
142             public void info(String msg, Object... arguments) {
143                 // no-op
144             }
145
146             @Override
147             public void info(String msg, Throwable t) {
148                 // no-op
149             }
150
151             @Override
152             public boolean isWarnEnabled() {
153                 return false;
154             }
155
156             @Override
157             public void warn(String msg) {
158                 // no-op
159             }
160
161             @Override
162             public void warn(String msg, Object arg) {
163                 // no-op
164             }
165
166             @Override
167             public void warn(String msg, Object... arguments) {
168                 // no-op
169             }
170
171             @Override
172             public void warn(String msg, Object arg1, Object arg2) {
173                 // no-op
174             }
175
176             @Override
177             public void warn(String msg, Throwable t) {
178                 // no-op
179             }
180
181             @Override
182             public boolean isErrorEnabled() {
183                 return false;
184             }
185
186             @Override
187             public void error(String msg) {
188                 // no-op
189             }
190
191             @Override
192             public void error(String msg, Object arg) {
193                 // no-op
194             }
195
196             @Override
197             public void error(String msg, Object arg1, Object arg2) {
198                 // no-op
199             }
200
201             @Override
202             public void error(String msg, Object... arguments) {
203                 // no-op
204             }
205
206             @Override
207             public void error(String msg, Throwable t) {
208                 // no-op
209             }
210         }
211
212         @Override
213         public Logger getLogger(String className) {
214             Objects.requireNonNull(className, "Name cannot be null");
215             return NoOpLogger.INSTANCE;
216         }
217
218         @Override
219         public Logger getLogger(Class<?> clazz) {
220             Objects.requireNonNull(clazz, "Class cannot be null");
221             return NoOpLogger.INSTANCE;
222         }
223     }
224 }
225