2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.openecomp.policy.drools.eelf.test;
22 import static org.junit.Assert.assertFalse;
23 import static org.junit.Assert.assertTrue;
25 import java.util.ArrayList;
26 import java.util.List;
28 import org.junit.Test;
29 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
30 import org.openecomp.policy.drools.eelf.EelfFeature;
31 import org.openecomp.policy.drools.system.Main;
32 import org.openecomp.policy.drools.system.PolicyEngine;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import com.att.eelf.configuration.Configuration;
37 import com.att.eelf.configuration.EELFLogger;
38 import com.att.eelf.configuration.EELFLogger.Level;
39 import com.att.eelf.configuration.EELFManager;
41 import ch.qos.logback.classic.LoggerContext;
46 public class EElfTest {
49 * logback configuration location
51 public final static String LOGBACK_CONFIGURATION_FILE_DEFAULT = "src/main/feature/config/logback-eelf.xml";
56 private final Logger slf4jLogger = LoggerFactory.getLogger(EElfTest.class);
60 * @return list of all loggers
62 protected List<String> loggers() {
63 List<String> loggers = new ArrayList<String>();
64 LoggerContext context =
65 (LoggerContext) org.slf4j.LoggerFactory.getILoggerFactory();
66 for (org.slf4j.Logger logger: context.getLoggerList()) {
67 loggers.add(logger.getName());
69 slf4jLogger.info(loggers.toString());
74 * Assert Log Levels are the same between an EELF Logger and an SLF4J Logger
76 * @param eelfLogger EELF Logger
77 * @param slf4jLogger SLF4J Logger
79 protected void assertLogLevels(EELFLogger eelfLogger, Logger slf4jLogger) {
80 assertTrue(slf4jLogger.isDebugEnabled() == eelfLogger.isDebugEnabled());
81 assertTrue(slf4jLogger.isInfoEnabled() == eelfLogger.isInfoEnabled());
82 assertTrue(slf4jLogger.isErrorEnabled() == eelfLogger.isErrorEnabled());
83 assertTrue(slf4jLogger.isWarnEnabled() == eelfLogger.isWarnEnabled());
84 assertTrue(slf4jLogger.isTraceEnabled() == eelfLogger.isTraceEnabled());
88 public void slf4jLog() {
90 /* standard slf4j using defaults */
92 slf4jLogger.info("slf4j info");
94 List<String> loggers = loggers();
96 assertFalse(loggers.contains(Configuration.DEBUG_LOGGER_NAME));
97 assertFalse(loggers.contains(Configuration.AUDIT_LOGGER_NAME));
98 assertFalse(loggers.contains(Configuration.ERROR_LOGGER_NAME));
99 assertFalse(loggers.contains(Configuration.METRICS_LOGGER_NAME));
101 /* set logback configuration */
103 System.setProperty(Main.LOGBACK_CONFIGURATION_FILE_SYSTEM_PROPERTY,
104 LOGBACK_CONFIGURATION_FILE_DEFAULT);
106 /* set up eelf throuth common loggings library */
108 EelfFeature feature = new EelfFeature();
109 feature.beforeBoot(PolicyEngine.manager, null);
112 assertTrue(loggers.contains(Configuration.DEBUG_LOGGER_NAME));
113 assertTrue(loggers.contains(Configuration.AUDIT_LOGGER_NAME));
114 assertTrue(loggers.contains(Configuration.ERROR_LOGGER_NAME));
115 assertTrue(loggers.contains(Configuration.METRICS_LOGGER_NAME));
117 EELFLogger eelfAuditLogger = EELFManager.getInstance().getAuditLogger();
118 Logger slf4jAuditLogger = org.slf4j.LoggerFactory.getLogger(Configuration.AUDIT_LOGGER_NAME);
119 org.openecomp.policy.common.logging.flexlogger.Logger flexLogger =
120 FlexLogger.getLogger(EElfTest.class, true);
122 /* generate an error entry */
124 Exception testException = new IllegalStateException("exception test");
125 flexLogger.error("flex-logger exception", testException);
126 EELFManager.getInstance().getErrorLogger().error("eelf-logger exception", testException);
127 org.slf4j.LoggerFactory.getLogger(Configuration.ERROR_LOGGER_NAME).error("slf4j-logger", testException);
130 /* generate an audit entry through all logs */
132 flexLogger.audit("flexlogger audit");
133 eelfAuditLogger.info("eelf audit");
134 slf4jAuditLogger.info("slf4j audit");
136 /* check log levels in eelf and standard slf4j change in both directions */
139 eelfAuditLogger.setLevel(Level.ERROR);
140 assertLogLevels(eelfAuditLogger, slf4jAuditLogger);
142 /* slf4j initiated */
143 ((ch.qos.logback.classic.Logger) slf4jLogger).setLevel((ch.qos.logback.classic.Level.INFO));
144 assertLogLevels(eelfAuditLogger, slf4jAuditLogger);