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 / AuditLogImpl.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
23 import org.onap.dcae.utils.eelf.logger.api.info.LogLevelCategory;
24 import org.onap.dcae.utils.eelf.logger.api.log.AuditLog;
25 import org.onap.dcae.utils.eelf.logger.api.spec.AuditLogSpec;
26 import org.onap.dcae.utils.eelf.logger.api.spec.OptionalLogSpec;
27 import org.onap.dcae.utils.eelf.logger.logback.utils.LogUtils;
28 import org.slf4j.Logger;
29
30
31 /**
32  * Audit Log Logback Implementation
33  *
34  * @author Rajiv Singla
35  */
36 public class AuditLogImpl implements AuditLog {
37
38     private final Logger logger;
39     private final Class<?> clazz;
40
41     public AuditLogImpl(final Logger logger, final Class<?> clazz) {
42         this.logger = logger;
43         this.clazz = clazz;
44     }
45
46     @Override
47     public void log(final LogLevelCategory logLevelCategory, final String message, final AuditLogSpec
48             auditLogSpec, final OptionalLogSpec optionalLogSpec, final String... args) {
49         // if audit log spec or log level category is null throw an exception
50         if (auditLogSpec == null || logLevelCategory == null) {
51             throw new IllegalArgumentException("Audit Log Spec and Log level category must not be null");
52         }
53         // required fields
54         LogUtils.CUSTOM_MDC_MAP.put(LogUtils.LOG_LEVEL_CATEGORY_KEY, logLevelCategory);
55         LogUtils.CUSTOM_MDC_MAP.put(LogUtils.AUDIT_LOG_SPEC_KEY, auditLogSpec);
56         LogUtils.CUSTOM_MDC_MAP.put(LogUtils.LOGGER_CLASS_KEY, clazz);
57         // optional fields
58         LogUtils.CUSTOM_MDC_MAP.put(LogUtils.OPTIONAL_LOG_SPEC_KEY, optionalLogSpec);
59
60         // log with normalized log level category
61         LogUtils.logWithLogLevel(logLevelCategory, logger, LogUtils.AUDIT_LOG_MARKER, message, args);
62
63         // clean up
64         LogUtils.CUSTOM_MDC_MAP.remove(LogUtils.LOG_LEVEL_CATEGORY_KEY);
65         LogUtils.CUSTOM_MDC_MAP.remove(LogUtils.AUDIT_LOG_SPEC_KEY);
66         LogUtils.CUSTOM_MDC_MAP.remove(LogUtils.LOGGER_CLASS_KEY);
67         LogUtils.CUSTOM_MDC_MAP.remove(LogUtils.OPTIONAL_LOG_SPEC_KEY);
68     }
69
70     @Override
71     public void log(final LogLevelCategory logLevelCategory, final String message, final AuditLogSpec auditLogSpec,
72                     final String... args) {
73         log(logLevelCategory, message, auditLogSpec, null, args);
74     }
75
76     @Override
77     public void info(final String message, final AuditLogSpec auditLogSpec, final OptionalLogSpec optionalLogSpec,
78                      final String... args) {
79         log(LogLevelCategory.INFO, message, auditLogSpec, optionalLogSpec, args);
80     }
81
82     @Override
83     public void info(final String message, final AuditLogSpec auditLogSpec, final String... args) {
84         info(message, auditLogSpec, null, args);
85     }
86
87     @Override
88     public void warn(final String message, final AuditLogSpec auditLogSpec, final OptionalLogSpec optionalLogSpec,
89                      final String... args) {
90         log(LogLevelCategory.WARN, message, auditLogSpec, optionalLogSpec, args);
91     }
92
93     @Override
94     public void warn(final String message, final AuditLogSpec auditLogSpec, final String... args) {
95         warn(message, auditLogSpec, null, args);
96     }
97
98     @Override
99     public void error(final String message, final AuditLogSpec auditLogSpec, final OptionalLogSpec optionalLogSpec,
100                       final String... args) {
101         log(LogLevelCategory.ERROR, message, auditLogSpec, optionalLogSpec, args);
102     }
103
104     @Override
105     public void error(final String message, final AuditLogSpec auditLogSpec, final String... args) {
106         error(message, auditLogSpec, null, args);
107     }
108
109     @Override
110     public void fatal(final String message, final AuditLogSpec auditLogSpec, final OptionalLogSpec optionalLogSpec,
111                       final String... args) {
112         log(LogLevelCategory.FATAL, message, auditLogSpec, optionalLogSpec, args);
113     }
114
115     @Override
116     public void fatal(final String message, final AuditLogSpec auditLogSpec, final String... args) {
117         fatal(message, auditLogSpec, null, args);
118     }
119 }