Standalone TCA with EELF Logger
[dcaegen2/analytics/tca-gen2.git] / eelf-logger / eelf-logger-logback-impl / src / main / java / org / onap / dcae / utils / eelf / logger / logback / log / ErrorLogImpl.java
1 /*
2  * ================================================================================
3  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END=========================================================
17  *
18  */
19
20 package org.onap.dcae.utils.eelf.logger.logback.log;
21
22 import org.onap.dcae.utils.eelf.logger.api.info.LogLevelCategory;
23 import org.onap.dcae.utils.eelf.logger.api.log.ErrorLog;
24 import org.onap.dcae.utils.eelf.logger.api.spec.ErrorLogSpec;
25 import org.onap.dcae.utils.eelf.logger.api.spec.OptionalLogSpec;
26 import org.onap.dcae.utils.eelf.logger.logback.utils.LogUtils;
27 import org.slf4j.Logger;
28
29
30 /**
31  * Error Log Logback Implementation
32  *
33  * @author Rajiv Singla
34  */
35 public class ErrorLogImpl implements ErrorLog {
36
37     private final Logger logger;
38     private final Class<?> clazz;
39
40     public ErrorLogImpl(final Logger logger, final Class<?> clazz) {
41         this.logger = logger;
42         this.clazz = clazz;
43     }
44
45     @Override
46     public void log(final LogLevelCategory logLevelCategory, final String message, final ErrorLogSpec errorLogSpec,
47                     final OptionalLogSpec optionalLogSpec, final String... args) {
48
49         // if error log spec or log level category is null throw an exception
50         if (errorLogSpec == null || logLevelCategory == null) {
51             throw new IllegalArgumentException("Error Log Spec and Log level category must not be null");
52         }
53
54         // required fields
55         LogUtils.CUSTOM_MDC_MAP.put(LogUtils.LOG_LEVEL_CATEGORY_KEY, logLevelCategory);
56         LogUtils.CUSTOM_MDC_MAP.put(LogUtils.ERROR_LOG_SPEC_KEY, errorLogSpec);
57         LogUtils.CUSTOM_MDC_MAP.put(LogUtils.LOGGER_CLASS_KEY, clazz);
58         // optional fields
59         LogUtils.CUSTOM_MDC_MAP.put(LogUtils.OPTIONAL_LOG_SPEC_KEY, optionalLogSpec);
60
61         // log with normalized log level category
62         LogUtils.logWithLogLevel(logLevelCategory, logger, LogUtils.ERROR_LOG_MARKER, message, args);
63
64         // clean up
65         LogUtils.CUSTOM_MDC_MAP.remove(LogUtils.LOG_LEVEL_CATEGORY_KEY);
66         LogUtils.CUSTOM_MDC_MAP.remove(LogUtils.ERROR_LOG_SPEC_KEY);
67         LogUtils.CUSTOM_MDC_MAP.remove(LogUtils.LOGGER_CLASS_KEY);
68         LogUtils.CUSTOM_MDC_MAP.remove(LogUtils.OPTIONAL_LOG_SPEC_KEY);
69     }
70
71     @Override
72     public void log(final LogLevelCategory logLevelCategory, final String message, final ErrorLogSpec errorLogSpec,
73                     final String... args) {
74         log(logLevelCategory, message, errorLogSpec, null, args);
75     }
76
77     @Override
78     public void error(final String message, final ErrorLogSpec errorLogSpec, final OptionalLogSpec optionalLogSpec,
79                       final String... args) {
80         log(LogLevelCategory.ERROR, message, errorLogSpec, null, args);
81     }
82
83     @Override
84     public void error(final String message, final ErrorLogSpec errorLogSpec, final String... args) {
85         error(message, errorLogSpec, null, args);
86     }
87
88     @Override
89     public void warn(final String message, final ErrorLogSpec errorLogSpec, final OptionalLogSpec optionalLogSpec,
90                      final String... args) {
91         log(LogLevelCategory.WARN, message, errorLogSpec, null, args);
92     }
93
94     @Override
95     public void warn(final String message, final ErrorLogSpec errorLogSpec, final String... args) {
96         warn(message, errorLogSpec, null, args);
97     }
98 }