3fb250f92dea98cc24810eb03e91e8a1138d2f2b
[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.aaf")+".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                 if(System.getProperty("log4j.configuration")==null) {
66                         System.setProperty("log4j.configuration", etc_dir+'/'+propsFile);
67                 }
68                 LogFileNamer lfn = new LogFileNamer(log_dir,root);
69                 try {
70                         service=lfn.setAppender("service"); // when name is split, i.e. authz|service, the Appender is "authz", and "service"
71                         audit=lfn.setAppender("audit");     // is part of the log-file name
72                         init=lfn.setAppender("init");
73                         trace=lfn.setAppender("trace");
74
75                         lservice = Logger.getLogger(service);
76                         laudit = Logger.getLogger(audit);
77                         linit = Logger.getLogger(init);
78                         ltrace = Logger.getLogger(trace);
79         
80                         lfn.configure(etc_dir,propsFile, log_level);
81                 } catch (IOException e) {
82                         throw new APIException(e);
83                 }
84         }
85         
86         private static final String getArgOrVM(final String tag, final String args[], final String def) {
87                 String tagEQ = tag + '=';
88                 String value;
89                 for(String arg : args) {
90                         if(arg.startsWith(tagEQ)) {
91                                 return arg.substring(tagEQ.length());
92                         }
93                 }
94                 // check System.properties
95                 value = System.getProperty(tag);
96                 if(value!=null) { 
97                         return value;
98                 }
99                 
100                 return def;
101         }
102
103         @Override
104         public void push(Level level, Object... elements) {
105                 switch(level) {
106                         case AUDIT:
107                                 laudit.warn(PropAccess.buildMsg(audit, iso8601, level, elements));
108                                 break;
109                         case INIT:
110                                 linit.warn(PropAccess.buildMsg(init, iso8601, level, elements));
111                                 break;
112                         case ERROR:
113                                 lservice.error(PropAccess.buildMsg(service, iso8601, level, elements));
114                                 break;
115                         case WARN:
116                                 lservice.warn(PropAccess.buildMsg(service, iso8601, level, elements));
117                                 break;
118                         case INFO:
119                                 lservice.info(PropAccess.buildMsg(service, iso8601, level, elements));
120                                 break;
121                         case DEBUG:
122                                 lservice.debug(PropAccess.buildMsg(service, iso8601, level, elements));
123                                 break;
124                         case TRACE:
125                                 ltrace.trace(PropAccess.buildMsg(service, iso8601, level, elements));
126                                 break;
127                         case NONE:
128                                 break;
129                         default:
130                                 lservice.info(PropAccess.buildMsg(service, iso8601, level, elements));
131                                 break;
132                 
133                 }
134
135         }
136 }