3ca4a6764792551de6bdcc9aea660517ccf9ac42
[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.context.impl;
18
19 import org.openecomp.sdc.logging.api.Logger;
20 import org.openecomp.sdc.logging.api.LoggerFactory;
21 import org.openecomp.sdc.logging.context.MdcData;
22 import org.openecomp.sdc.logging.messages.DebugMessages;
23 import org.openecomp.sdc.logging.types.DebugConstants;
24 import org.openecomp.sdc.logging.types.LoggerErrorCategory;
25 import org.openecomp.sdc.logging.util.LoggingUtils;
26
27 import java.util.HashMap;
28 import java.util.Map;
29
30 public class MdcDataDebugMessage extends MdcData {
31
32   private Logger logger;
33   private static Map<String, String> mapExitOrEntryToMessage;
34
35   static {
36     mapExitOrEntryToMessage = new HashMap<>();
37     mapExitOrEntryToMessage.put(DebugConstants.ENTER, DebugMessages.ENTER_METHOD);
38     mapExitOrEntryToMessage.put(DebugConstants.ENTER_DEFAULT, DebugMessages.DEFAULT_ENTER_METHOD);
39     mapExitOrEntryToMessage.put(DebugConstants.EXIT, DebugMessages.EXIT_METHOD);
40     mapExitOrEntryToMessage.put(DebugConstants.EXIT_DEFAULT, DebugMessages.DEFAULT_EXIT_METHOD);
41   }
42
43   public MdcDataDebugMessage() {
44     super(LoggerErrorCategory.DEBUG.name(), null);
45   }
46
47   //todo add more explanations as to the first parameter structure in case of multiples and in
48   // case of no params in method
49   /**
50    * Debug entry message.
51    *
52    * @param entityParameter the entity parameter
53    * @param ids             the ids
54    */
55   public void debugEntryMessage(String entityParameter, String... ids) {
56     logDebugMessage(entityParameter, DebugConstants.ENTER, ids);
57   }
58
59   /**
60    * Debug exit message.
61    *
62    * @param entityParameter the entity parameter
63    * @param ids             the ids
64    */
65   public void debugExitMessage(String entityParameter, String... ids) {
66     logDebugMessage(entityParameter, DebugConstants.EXIT, ids);
67   }
68
69   private void logDebugMessage(String entityParameter, String enterOrExit, String... ids) {
70     String methodName = LoggingUtils.getCallingMethodNameForDebugging();
71     String declaringClass = LoggingUtils.getDeclaringClass();
72     logger = LoggerFactory.getLogger(declaringClass);
73     String messageToWrite;
74
75     if (entityParameter == null || ids == null) {
76       messageToWrite = mapExitOrEntryToMessage.get(enterOrExit + "_" + DebugConstants.DEFAULT);
77       logger.debug(String.format(messageToWrite, methodName));
78     } else {
79       messageToWrite = mapExitOrEntryToMessage.get(enterOrExit);
80       logger.debug(String
81           .format(messageToWrite, methodName, entityParameter, String.join(",", ids)));
82     }
83   }
84 }