Add Notice of aaf/inno source moving to aaf/authz
[aaf/inno.git] / log4j / src / main / java / org / onap / aaf / inno / env / log4j / LogFileNamer.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package org.onap.aaf.inno.env.log4j;\r
24 \r
25 import java.io.File;\r
26 import java.net.URL;\r
27 \r
28 public class LogFileNamer {\r
29         public static final int pid = PIDAccess.INSTANCE.getpid();\r
30         public final String root;\r
31         private boolean printPID;\r
32         \r
33         public LogFileNamer(String root) {\r
34                 if(root==null || "".equals(root) || root.endsWith("/")) {\r
35                         this.root = root;\r
36                 } else {\r
37                         this.root = root + "-";\r
38                 }\r
39                 printPID=true;\r
40         }\r
41         \r
42         public LogFileNamer noPID() {\r
43                 printPID = false;\r
44                 return this;\r
45         }\r
46         /**\r
47          * Accepts a String.\r
48          * If Separated by "|" then first part is the Appender name, and the second is used in the FileNaming\r
49          * (This is to allow for shortened Logger names, and more verbose file names)\r
50          * \r
51          * @param appender\r
52          * \r
53          * returns the String Appender\r
54          */\r
55         public String setAppender(String appender) {\r
56                 int pipe = appender.indexOf('|');\r
57                 if(pipe>=0) {\r
58                         String rv;\r
59                         System.setProperty(\r
60                                 "LOG4J_FILENAME_"+(rv=appender.substring(0,pipe)),\r
61                                 root + appender.substring(pipe+1) + (printPID?('-' + pid):"") + ".log");\r
62                         return rv;\r
63                 } else {\r
64                         System.setProperty(\r
65                                 "LOG4J_FILENAME_"+appender,\r
66                                 root + appender + (printPID?('-' + pid):"") + ".log");\r
67                         return appender;\r
68                 }\r
69                 \r
70         }\r
71 \r
72         public void configure(String props) {\r
73                 String fname;\r
74                 if(new File(fname="etc/"+props).exists()) {\r
75                         org.apache.log4j.PropertyConfigurator.configureAndWatch(fname,60*1000);\r
76                 } else {\r
77                         URL rsrc = ClassLoader.getSystemResource(props);\r
78                         if(rsrc==null) System.err.println("Neither File: " + fname + " or resource on Classpath " + props + " exist" );\r
79                         org.apache.log4j.PropertyConfigurator.configure(rsrc);\r
80                 }\r
81         }\r
82 }\r