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