Update groupId to org.onap.ccsdk.sli
[ccsdk/sli/core.git] / sli / recording / src / main / java / org / openecomp / sdnc / sli / recording / Slf4jRecorder.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.sdnc.sli.recording;
23
24 import java.text.DateFormat;
25 import java.text.SimpleDateFormat;
26 import java.util.Date;
27 import java.util.Map;
28 import java.util.TimeZone;
29
30 import org.openecomp.sdnc.sli.ConfigurationException;
31 import org.openecomp.sdnc.sli.SvcLogicException;
32 import org.openecomp.sdnc.sli.SvcLogicRecorder;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class Slf4jRecorder implements SvcLogicRecorder {
37         
38         
39         public enum Level {
40                 ERROR,
41                 WARN,
42                 INFO,
43                 DEBUG,
44                 TRACE
45         }
46
47         @Override
48         public void record(Map<String, String> parmMap) throws SvcLogicException {
49                 String loggerName = parmMap.get("logger");
50                 if (loggerName == null) {
51                         loggerName = "Log4jRecorder";
52                 }
53                 
54                 String lvl = parmMap.get("level");
55                 if (lvl == null) {
56                         lvl = "INFO";
57                 }
58
59                 Level level = Level.INFO;
60                 
61                 try {
62                         level = Level.valueOf(lvl.toUpperCase());
63                 } catch (Exception e) {}
64                 
65                 
66                 
67                 String record = parmMap.get("record");
68                 if (record == null)
69                 {
70                         String delimiter = parmMap.get("delimiter");
71                         if (delimiter == null)
72                         {
73                                 delimiter = "|";
74                         }
75                         
76                         int idx = 1;
77                         boolean moreFields = true;
78                         while (moreFields)
79                         {
80                                 String curField = parmMap.get("field"+idx++);
81                                 if (curField == null)
82                                 {
83                                         moreFields = false;
84                                 }
85                                 else
86                                 {
87                                         if (record == null)
88                                         {
89                                                 record = delimiter;
90                                         }
91                                         record = record + curField + delimiter;
92                                 }
93                         }
94                 }
95                 
96                 if (record == null)
97                 {
98                         throw new ConfigurationException("No record/fields passed in record node");
99                 }
100                 
101                 Logger logger = LoggerFactory.getLogger(loggerName);
102
103                 Date now = new Date();
104                 TimeZone tz = TimeZone.getTimeZone("UTC");
105                 DateFormat dateFmt = new SimpleDateFormat("yyy-MM-dd'T'HH:mm:ss:SS'+00:00'");
106                 dateFmt.setTimeZone(tz);
107                 if (record.indexOf("__TIMESTAMP__") != -1)
108                 {
109                         record = record.replaceFirst("__TIMESTAMP__", dateFmt.format(now));
110                 }
111                 
112                 switch (level) {
113                 case ERROR:
114                         logger.error(record);
115                         break;
116                 case WARN:
117                         logger.warn(record);
118                         break;
119                 case INFO:
120                         logger.info(record);
121                         break;
122                 case DEBUG:
123                         logger.debug(record);
124                         break;
125                 case TRACE:
126                         logger.trace(record);
127                 }
128         }
129
130 }