AT&T 2.0.19 Code drop, stage 1
[aaf/authz.git] / misc / log4j / src / main / java / org / onap / aaf / misc / env / log4j / LogFileNamer.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
22 package org.onap.aaf.misc.env.log4j;
23
24 import java.io.File;
25 import java.net.URL;
26
27 public class LogFileNamer {
28         public static final int pid = PIDAccess.INSTANCE.getpid();
29         public final String root;
30         private boolean printPID;
31         
32         public LogFileNamer(String root) {
33                 if(root==null || "".equals(root) || root.endsWith("/")) {
34                         this.root = root;
35                 } else {
36                         this.root = root + "-";
37                 }
38                 printPID=true;
39         }
40         
41         public LogFileNamer noPID() {
42                 printPID = false;
43                 return this;
44         }
45         /**
46          * Accepts a String.
47          * If Separated by "|" then first part is the Appender name, and the second is used in the FileNaming
48          * (This is to allow for shortened Logger names, and more verbose file names)
49          * 
50          * @param appender
51          * 
52          * returns the String Appender
53          */
54         public String setAppender(String appender) {
55                 int pipe = appender.indexOf('|');
56                 if(pipe>=0) {
57                         String rv;
58                         System.setProperty(
59                                 "LOG4J_FILENAME_"+(rv=appender.substring(0,pipe)),
60                                 root + appender.substring(pipe+1) + (printPID?('-' + pid):"") + ".log");
61                         return rv;
62                 } else {
63                         System.setProperty(
64                                 "LOG4J_FILENAME_"+appender,
65                                 root + appender + (printPID?('-' + pid):"") + ".log");
66                         return appender;
67                 }
68                 
69         }
70
71         public void configure(String props) {
72                 String fname;
73                 if(new File(fname="etc/"+props).exists()) {
74                         org.apache.log4j.PropertyConfigurator.configureAndWatch(fname,60*1000);
75                 } else {
76                         URL rsrc = ClassLoader.getSystemResource(props);
77                         if(rsrc==null) System.err.println("Neither File: " + fname + " or resource on Classpath " + props + " exist" );
78                         org.apache.log4j.PropertyConfigurator.configure(rsrc);
79                 }
80         }
81 }