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