2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
21 package org.onap.aaf.auth.server;
24 import java.io.IOException;
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;
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.
39 private final String service;
40 private final String audit;
41 private final String init;
42 private final String trace;
44 private final Logger lservice;
45 private final Logger laudit;
46 private final Logger linit;
47 private final Logger ltrace;
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 logDir = getArgOrVM(Config.CADI_LOGDIR,args,"/opt/app/osaaf/logs");
53 String etcDir = getArgOrVM(Config.CADI_ETCDIR,args,"/opt/app/osaaf/etc");
54 String logLevel = getArgOrVM(Config.CADI_LOGLEVEL,args,"INFO");
55 File logs = new File(logDir);
56 if (!logs.isDirectory()) {
63 if (System.getProperty("log4j.configuration")==null) {
64 System.setProperty("log4j.configuration", etcDir+'/'+propsFile);
66 LogFileNamer lfn = new LogFileNamer(logDir,root);
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");
73 lservice = Logger.getLogger(service);
74 laudit = Logger.getLogger(audit);
75 linit = Logger.getLogger(init);
76 ltrace = Logger.getLogger(trace);
78 lfn.configure(etcDir,propsFile, logLevel);
79 } catch (IOException e) {
80 throw new APIException(e);
84 private static final String getArgOrVM(final String tag, final String args[], final String def) {
85 String tagEQ = tag + '=';
87 for (String arg : args) {
88 if (arg.startsWith(tagEQ)) {
89 return arg.substring(tagEQ.length());
92 // check System.properties
93 value = System.getProperty(tag);
102 public void push(Level level, Object... elements) {
105 laudit.warn(PropAccess.buildMsg(audit, Chrono.utcFmt, level, elements));
108 linit.warn(PropAccess.buildMsg(init, Chrono.utcFmt, level, elements));
111 lservice.error(PropAccess.buildMsg(service, Chrono.utcFmt, level, elements));
114 lservice.warn(PropAccess.buildMsg(service, Chrono.utcFmt, level, elements));
117 lservice.info(PropAccess.buildMsg(service, Chrono.utcFmt, level, elements));
120 lservice.debug(PropAccess.buildMsg(service, Chrono.utcFmt, level, elements));
123 ltrace.trace(PropAccess.buildMsg(service, Chrono.utcFmt, level, elements));
128 lservice.info(PropAccess.buildMsg(service, Chrono.utcFmt, level, elements));