Add Certs, Docker Build
[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.File;
24 import java.io.IOException;
25 import java.text.SimpleDateFormat;
26
27 import org.apache.log4j.Logger;
28 import org.onap.aaf.cadi.Access.Level;
29 import org.onap.aaf.cadi.PropAccess;
30 import org.onap.aaf.cadi.PropAccess.LogIt;
31 import org.onap.aaf.cadi.config.Config;
32 import org.onap.aaf.misc.env.APIException;
33 import org.onap.aaf.misc.env.log4j.LogFileNamer;
34
35 public class Log4JLogIt implements LogIt {
36         protected static final String AAF_LOG4J_PREFIX = "aaf_log4j_prefix";
37
38         // Sonar says cannot be static... it's ok.  not too many PropAccesses created.
39         private final SimpleDateFormat iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
40         
41         private final String service;
42         private final String audit;
43         private final String init;
44         private final String trace;
45
46         private final Logger lservice;
47         private final Logger laudit;
48         private final Logger linit;
49         private final Logger ltrace;
50
51
52         public Log4JLogIt(final String[] args, final String root) throws APIException {
53                 String propsFile = getArgOrVM(AAF_LOG4J_PREFIX, args, "org.osaaf")+".log4j.props";
54                 String log_dir = getArgOrVM(Config.CADI_LOGDIR,args,"/opt/app/osaaf/logs");
55                 String etc_dir = getArgOrVM(Config.CADI_ETCDIR,args,"/opt/app/osaaf/etc");
56                 String log_level = getArgOrVM(Config.CADI_LOGLEVEL,args,"INFO");
57                 File logs = new File(log_dir);
58                 if(!logs.isDirectory()) {
59                         logs.delete();
60                 }
61                 if(!logs.exists()) {
62                         logs.mkdirs();
63                 }
64
65                 LogFileNamer lfn = new LogFileNamer(log_dir,root);
66                 try {
67                         service=lfn.setAppender("service"); // when name is split, i.e. authz|service, the Appender is "authz", and "service"
68                         audit=lfn.setAppender("audit");     // is part of the log-file name
69                         init=lfn.setAppender("init");
70                         trace=lfn.setAppender("trace");
71
72                         lservice = Logger.getLogger(service);
73                         laudit = Logger.getLogger(audit);
74                         linit = Logger.getLogger(init);
75                         ltrace = Logger.getLogger(trace);
76         
77                         lfn.configure(etc_dir,propsFile, log_level);
78                 } catch (IOException e) {
79                         throw new APIException(e);
80                 }
81         }
82         
83         private static final String getArgOrVM(final String tag, final String args[], final String def) {
84                 String tagEQ = tag + '=';
85                 String value;
86                 for(String arg : args) {
87                         if(arg.startsWith(tagEQ)) {
88                                 return arg.substring(tagEQ.length());
89                         }
90                 }
91                 // check System.properties
92                 value = System.getProperty(tag);
93                 if(value!=null) { 
94                         return value;
95                 }
96                 
97                 return def;
98         }
99
100         @Override
101         public void push(Level level, Object... elements) {
102                 switch(level) {
103                         case AUDIT:
104                                 laudit.warn(PropAccess.buildMsg(audit, iso8601, level, elements));
105                                 break;
106                         case INIT:
107                                 linit.warn(PropAccess.buildMsg(init, iso8601, level, elements));
108                                 break;
109                         case ERROR:
110                                 lservice.error(PropAccess.buildMsg(service, iso8601, level, elements));
111                                 break;
112                         case WARN:
113                                 lservice.warn(PropAccess.buildMsg(service, iso8601, level, elements));
114                                 break;
115                         case INFO:
116                                 lservice.info(PropAccess.buildMsg(service, iso8601, level, elements));
117                                 break;
118                         case DEBUG:
119                                 lservice.debug(PropAccess.buildMsg(service, iso8601, level, elements));
120                                 break;
121                         case TRACE:
122                                 ltrace.trace(PropAccess.buildMsg(service, iso8601, level, elements));
123                                 break;
124                         case NONE:
125                                 break;
126                         default:
127                                 lservice.info(PropAccess.buildMsg(service, iso8601, level, elements));
128                                 break;
129                 
130                 }
131
132         }
133 }