Refactor dblib
[ccsdk/sli/core.git] / sli / recording / src / main / java / org / onap / ccsdk / sli / core / sli / recording / Slf4jRecorder.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
4  * ================================================================================
5  * Copyright (C) 2017 ONAP
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.ccsdk.sli.core.sli.recording;
22
23 import java.text.DateFormat;
24 import java.text.SimpleDateFormat;
25 import java.util.Date;
26 import java.util.Map;
27 import java.util.TimeZone;
28
29 import org.onap.ccsdk.sli.core.sli.ConfigurationException;
30 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicRecorder;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class Slf4jRecorder implements SvcLogicRecorder {
36         
37         
38         public enum Level {
39                 ERROR,
40                 WARN,
41                 INFO,
42                 DEBUG,
43                 TRACE
44         }
45
46         @Override
47         public void record(Map<String, String> parmMap) throws SvcLogicException {
48                 String loggerName = parmMap.get("logger");
49                 if (loggerName == null) {
50                         loggerName = "Log4jRecorder";
51                 }
52                 
53                 String lvl = parmMap.get("level");
54                 if (lvl == null) {
55                         lvl = "INFO";
56                 }
57
58                 Level level = Level.INFO;
59                 
60                 try {
61                         level = Level.valueOf(lvl.toUpperCase());
62                 } catch (Exception e) {}
63                 
64                 
65                 
66                 String record = parmMap.get("record");
67                 if (record == null)
68                 {
69                         String delimiter = parmMap.get("delimiter");
70                         if (delimiter == null)
71                         {
72                                 delimiter = "|";
73                         }
74                         
75                         int idx = 1;
76                         boolean moreFields = true;
77                         while (moreFields)
78                         {
79                                 String curField = parmMap.get("field"+idx++);
80                                 if (curField == null)
81                                 {
82                                         moreFields = false;
83                                 }
84                                 else
85                                 {
86                                         if (record == null)
87                                         {
88                                                 record = delimiter;
89                                         }
90                                         record = record + curField + delimiter;
91                                 }
92                         }
93                 }
94                 
95                 if (record == null)
96                 {
97                         throw new ConfigurationException("No record/fields passed in record node");
98                 }
99                 
100                 Logger logger = LoggerFactory.getLogger(loggerName);
101
102                 Date now = new Date();
103                 TimeZone tz = TimeZone.getTimeZone("UTC");
104                 DateFormat dateFmt = new SimpleDateFormat("yyy-MM-dd'T'HH:mm:ss:SS'+00:00'");
105                 dateFmt.setTimeZone(tz);
106                 if (record.indexOf("__TIMESTAMP__") != -1)
107                 {
108                         record = record.replaceFirst("__TIMESTAMP__", dateFmt.format(now));
109                 }
110                 
111                 switch (level) {
112                 case ERROR:
113                         logger.error(record);
114                         break;
115                 case WARN:
116                         logger.warn(record);
117                         break;
118                 case INFO:
119                         logger.info(record);
120                         break;
121                 case DEBUG:
122                         logger.debug(record);
123                         break;
124                 case TRACE:
125                         logger.trace(record);
126                 }
127         }
128
129 }