Mass removal of all Tabs (Style Warnings)
[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 import java.text.SimpleDateFormat;\r
27 import java.util.Date;\r
28 \r
29 public class LogFileNamer {\r
30     private final String root;\r
31     private final String ending;\r
32     private final String dir;\r
33 \r
34     public LogFileNamer(final String dir, final String root) {\r
35         this.dir = dir;\r
36         if (root == null || "".equals(root) || root.endsWith("/")) {\r
37             this.root = root;\r
38         } else {\r
39             this.root = root + "-";\r
40         }\r
41         ending = new SimpleDateFormat("YYYYMMdd").format(new Date());\r
42     }\r
43 \r
44     public LogFileNamer noPID() {\r
45         return this;\r
46     }\r
47 \r
48     private static final String FILE_FORMAT_STR = "%s/%s%s%s_%d.log";\r
49 \r
50     /**\r
51      * Accepts a String. If Separated by "|" then first part is the Appender name,\r
52      * and the second is used in the FileNaming (This is to allow for shortened\r
53      * Logger names, and more verbose file names) ONAP: jna code has license issues.\r
54      * Just do Date + Unique Number\r
55      * \r
56      * @param appender\r
57      * \r
58      *            returns the String Appender\r
59      * @throws IOException\r
60      */\r
61     public String setAppender(String appender) throws IOException {\r
62         int i = 0;\r
63         File f;\r
64         while ((f = new File(String.format(FILE_FORMAT_STR, dir, root, appender, ending, i))).exists()) {\r
65             ++i;\r
66         }\r
67         \r
68         f.createNewFile();\r
69         System.setProperty("LOG4J_FILENAME_" + appender, f.getCanonicalPath());\r
70         return appender;\r
71     }\r
72 \r
73     public void configure(final String path, final String fname, final String log_level) throws IOException {\r
74         final String fullPath = path + '/' + fname;\r
75         if (new File(fullPath).exists()) {\r
76             org.apache.log4j.PropertyConfigurator.configureAndWatch(fullPath, 60 * 1000L);\r
77         } else {\r
78             URL rsrc = ClassLoader.getSystemResource(fname);\r
79             if (rsrc == null) {\r
80                 String msg = "Neither File: " + path + '/' + fname + " nor resource on Classpath " + fname + " exist";\r
81                 throw new IOException(msg);\r
82             }\r
83             org.apache.log4j.PropertyConfigurator.configure(rsrc);\r
84         }\r
85 \r
86     }\r
87 }\r