0b91c9fc67971766253630866a747244a7e85dbf
[aaf/authz.git] / auth / auth-core / src / main / java / org / onap / aaf / auth / server / Log4JLogIt.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 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
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.aaf.auth.server;
22
23 import java.io.IOException;
24 import java.text.SimpleDateFormat;
25
26 import org.apache.log4j.Logger;
27 import org.onap.aaf.cadi.Access.Level;
28 import org.onap.aaf.cadi.PropAccess;
29 import org.onap.aaf.cadi.PropAccess.LogIt;
30 import org.onap.aaf.misc.env.APIException;
31 import org.onap.aaf.misc.env.log4j.LogFileNamer;
32
33 public class Log4JLogIt implements LogIt {
34         // Sonar says cannot be static... it's ok.  not too many PropAccesses created.
35         private final SimpleDateFormat iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
36         
37         private final String service;
38         private final String audit;
39         private final String init;
40         private final String trace;
41
42         private final Logger lservice;
43         private final Logger laudit;
44         private final Logger linit;
45         private final Logger ltrace;
46
47
48         public Log4JLogIt(final String log_dir, final String log_level, final String propsFile, final String root) throws APIException {
49                 LogFileNamer lfn = new LogFileNamer(log_dir,root);
50                 try {
51                         service=lfn.setAppender("service"); // when name is split, i.e. authz|service, the Appender is "authz", and "service"
52                         audit=lfn.setAppender("audit");     // is part of the log-file name
53                         init=lfn.setAppender("init");
54                         trace=lfn.setAppender("trace");
55
56                         lservice = Logger.getLogger(service);
57                         laudit = Logger.getLogger(audit);
58                         linit = Logger.getLogger(init);
59                         ltrace = Logger.getLogger(trace);
60         
61                         lfn.configure(propsFile, log_level);
62                 } catch (IOException e) {
63                         throw new APIException(e);
64                 }
65         }
66         
67         @Override
68         public void push(Level level, Object... elements) {
69                 switch(level) {
70                         case AUDIT:
71                                 laudit.warn(PropAccess.buildMsg(audit, iso8601, level, elements));
72                                 break;
73                         case INIT:
74                                 linit.warn(PropAccess.buildMsg(init, iso8601, level, elements));
75                                 break;
76                         case ERROR:
77                                 lservice.error(PropAccess.buildMsg(service, iso8601, level, elements));
78                                 break;
79                         case WARN:
80                                 lservice.warn(PropAccess.buildMsg(service, iso8601, level, elements));
81                                 break;
82                         case INFO:
83                                 lservice.info(PropAccess.buildMsg(service, iso8601, level, elements));
84                                 break;
85                         case DEBUG:
86                                 lservice.debug(PropAccess.buildMsg(service, iso8601, level, elements));
87                                 break;
88                         case TRACE:
89                                 ltrace.trace(PropAccess.buildMsg(service, iso8601, level, elements));
90                                 break;
91                         case NONE:
92                                 break;
93                         default:
94                                 lservice.info(PropAccess.buildMsg(service, iso8601, level, elements));
95                                 break;
96                 
97                 }
98
99         }
100 }