Keep only clean TestCases, remove 2 license issues
[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 final String root;
29         
30         public LogFileNamer(String root) {
31                 if(root==null || "".equals(root) || root.endsWith("/")) {
32                         this.root = root;
33                 } else {
34                         this.root = root + "-";
35                 }
36         }
37         
38         public LogFileNamer noPID() {
39                 return this;
40         }
41         /**
42          * Accepts a String.
43          * If Separated by "|" then first part is the Appender name, and the second is used in the FileNaming
44          * (This is to allow for shortened Logger names, and more verbose file names)
45          * 
46          * @param appender
47          * 
48          * returns the String Appender
49          */
50         public String setAppender(String appender) {
51                 int pipe = appender.indexOf('|');
52                 if(pipe>=0) {
53                         String rv;
54                         System.setProperty(
55                                 "LOG4J_FILENAME_"+(rv=appender.substring(0,pipe)),
56                                 root + appender.substring(pipe+1) + ".log");
57                         return rv;
58                 } else {
59                         System.setProperty(
60                                 "LOG4J_FILENAME_"+appender,
61                                 root + appender + ".log");
62                         return appender;
63                 }
64                 
65         }
66
67         public void configure(String props) {
68                 String fname;
69                 if(new File(fname="etc/"+props).exists()) {
70                         org.apache.log4j.PropertyConfigurator.configureAndWatch(fname,60*1000L);
71                 } else {
72                         URL rsrc = ClassLoader.getSystemResource(props);
73                         if(rsrc==null) System.err.println("Neither File: " + fname + " or resource on Classpath " + props + " exist" );
74                         org.apache.log4j.PropertyConfigurator.configure(rsrc);
75                 }
76         }
77 }