Post Init Service Starter
[aaf/authz.git] / misc / log4j / src / main / java / org / onap / aaf / misc / env / log4j / LogFileNamer.java
1 /**\r
2  * ============LICENSE_START====================================================\r
3  * org.onap.aaf\r
4  * ===========================================================================\r
5  * Copyright (c) 2018 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  */\r
21 package org.onap.aaf.misc.env.log4j;\r
22 \r
23 import java.io.File;\r
24 import java.io.IOException;\r
25 import java.net.URL;\r
26 \r
27 public class LogFileNamer {\r
28     private final String root;\r
29     private final String dir;\r
30 \r
31     public LogFileNamer(final String dir, final String root) {\r
32         this.dir = dir;\r
33         if (root == null || "".equals(root) || root.endsWith("/")) {\r
34             this.root = root;\r
35         } else {\r
36             this.root = root + "-";\r
37         }\r
38     }\r
39 \r
40     public LogFileNamer noPID() {\r
41         return this;\r
42     }\r
43 \r
44     private static final String FIRST_FILE_FORMAT_STR = "%s/%s%s.log";\r
45     private static final String FILE_FORMAT_STR = "%s/%s%s.%d.log";\r
46 \r
47     /**\r
48      * Accepts a String. If Separated by "|" then first part is the Appender name,\r
49      * and the second is used in the FileNaming (This is to allow for shortened\r
50      * Logger names, and more verbose file names) ONAP: jna code has license issues.\r
51      * Just do Date + Unique Number\r
52      * \r
53      * @param appender\r
54      * \r
55      *            returns the String Appender\r
56      * @throws IOException\r
57      */\r
58     public String setAppender(String appender) throws IOException {\r
59         File f = new File(String.format(FIRST_FILE_FORMAT_STR, dir, root, appender));\r
60         if(f.exists()) {\r
61                 int i = 0;\r
62                 while ((f = new File(String.format(FILE_FORMAT_STR, dir, root, appender, i))).exists()) {\r
63                     ++i;\r
64                 }\r
65         }\r
66         \r
67         try {\r
68                 f.createNewFile();\r
69         } catch (IOException e) {\r
70                 throw new IOException("Cannot create file '" + f.getCanonicalPath() + '\'', e);\r
71         }\r
72         System.setProperty("LOG4J_FILENAME_" + appender, f.getCanonicalPath());\r
73         return appender;\r
74     }\r
75 \r
76     public void configure(final String path, final String fname, final String log_level) throws IOException {\r
77         final String fullPath = path + '/' + fname;\r
78         if (new File(fullPath).exists()) {\r
79             org.apache.log4j.PropertyConfigurator.configureAndWatch(fullPath, 60 * 1000L);\r
80         } else {\r
81             URL rsrc = ClassLoader.getSystemResource(fname);\r
82             if (rsrc == null) {\r
83                 String msg = "Neither File: " + path + '/' + fname + " nor resource on Classpath " + fname + " exist";\r
84                 throw new IOException(msg);\r
85             }\r
86             org.apache.log4j.PropertyConfigurator.configure(rsrc);\r
87         }\r
88 \r
89     }\r
90 }\r